diff --git a/assets/convert.py b/assets/convert.py index a145b74..6e4016e 100644 --- a/assets/convert.py +++ b/assets/convert.py @@ -1,6 +1,7 @@ import pandas as pd import pprint -import sys +import sys +import getpass sys.path.append('../learnlytics/') from dbmodel import * @@ -32,7 +33,9 @@ groups = { print(df) #init_db('WiSe_24_25.db') -init_postgres('postgresql://admin:admin@100.96.135.91/learnlytics') +name = getpass.getuser() +create_database(name) +init_postgres(name, 'postgres.cyperpunk.de', 'dergrumpf', '1P2h3i4lon$%', 5432) db.drop_tables(tables) db.create_tables(tables) # Create Class diff --git a/learnlytics/dbmodel/__init__.py b/learnlytics/dbmodel/__init__.py index c212616..3d852fa 100644 --- a/learnlytics/dbmodel/__init__.py +++ b/learnlytics/dbmodel/__init__.py @@ -3,6 +3,7 @@ from .utils import ( table_labels, init_local, init_postgres, + create_database, save_as_json, create_from_json ) diff --git a/learnlytics/dbmodel/utils.py b/learnlytics/dbmodel/utils.py index 63fcb94..181ad63 100644 --- a/learnlytics/dbmodel/utils.py +++ b/learnlytics/dbmodel/utils.py @@ -11,10 +11,13 @@ Includes: import sys, inspect, json from datetime import datetime, date from pathlib import Path +from peewee import * from playhouse.shortcuts import model_to_dict, dict_to_model -from playhouse.db_url import connect from .model import * +import psycopg2 +from psycopg2 import sql + class DateTimeEncoder(json.JSONEncoder): ''' Helper Class converting datetime.datetime -> isoformated String @@ -79,11 +82,28 @@ def init_local(name: Path | str) -> None: db.create_tables(tables) # Ensure tables exist -def init_postgres(url: str) -> None: +def create_database(name: str) -> None: + config = { + 'user': 'dergrumpf', + 'password': '1P2h3i4lon$%', + 'host': 'postgres.cyperpunk.de', + 'port': 5432, + 'dbname': 'postgres' + } + con = psycopg2.connect(**config) + con.autocommit = True + cur = con.cursor() + query = sql.SQL(f'CREATE DATABASE {name}') + try: + cur.execute(query) + except psycopg2.errors.DuplicateDatabase: + pass - assert isinstance(url, str), "Provided url isnt a String" + con.close() - database = connect(url) + +def init_postgres(name: str, host: str, user: str, password: str, port: int = 5432) -> None: + database = PostgresqlDatabase(name, host=host, port=port, user=user, password=password) db.initialize(database) db.connect() db.create_tables(tables) diff --git a/learnlytics/main.py b/learnlytics/main.py index 806241b..73ae361 100644 --- a/learnlytics/main.py +++ b/learnlytics/main.py @@ -10,7 +10,8 @@ from gui import ( status_bar ) -from dbmodel import init_postgres +from dbmodel import init_postgres, create_database, init_local +import getpass from pathlib import Path def main() -> None: @@ -24,8 +25,10 @@ def main() -> None: file = str(Path.home() / "learnlytics.db") with open("./pickles/database_location.txt", "w") as f: f.write(file) - - init_postgres('postgres://admin:admin@100.96.135.91:5432/learnlytics') + + name = getpass.getuser() + create_database(name) + init_postgres(name, 'postgres.cyperpunk.de', 'dergrumpf', '1P2h3i4lon$%', 5432) # Set Window Parameters runner_params = hello_imgui.RunnerParams()