diff --git a/configure b/configure index 62e60d4..55bbec7 100755 --- a/configure +++ b/configure @@ -1,21 +1,21 @@ #!/usr/bin/env python3 import os -import sys import json from pathlib import Path from django.core.management.utils import get_random_secret_key from src.backend.lib.pyShelf import Admin +from src.backend.lib.storage import Storage def load_config(): - with open('config.json',"r") as file: + with open('config.json', "r") as file: config = json.load(file) file.close() return config def write_config(config): - with open('config.json',"w") as file: + with open('config.json', "w") as file: json.dump(config, file) file.close() @@ -27,10 +27,14 @@ def set_secret(config=load_config()): else: print("Secret already set, skipping.") + def set_book_directory(config=load_config(), *args): if config["BOOKPATH"] == "": - try: config["BOOKPATH"] = args[0] - except IndexError: config["BOOKPATH"] = input("Input Book Directory ") + try: + config["BOOKPATH"] = args[0] + except IndexError: + config["BOOKPATH"] = input("Input Book Directory ") + def init_django_database(): cmds = [ @@ -44,10 +48,15 @@ def init_django_database(): os.system(cmd) os.chdir("../") + config = load_config() set_secret(config) set_book_directory(config) write_config(config) -init_django_database() -Admin(Path.cwd()).createsuperuser() +# TODO:: Refactor here to enable backend to handle database operations. +breakpoint +storage = Storage(config) +storage.create_tables() +# init_django_database() +# Admin(Path.cwd()).createsuperuser() diff --git a/src/backend/lib/storage.py b/src/backend/lib/storage.py index 8feaf0c..733a4e3 100755 --- a/src/backend/lib/storage.py +++ b/src/backend/lib/storage.py @@ -8,11 +8,11 @@ import psycopg2 class Storage: """Contains all methods for system storage""" def __init__(self, config): - self.sql = config.catalogue_db - self.user = config.user - self.password = config.password - self.db_host = config.db_host - self.db_port = config.db_port + self.sql = config["DATABASE"] + self.user = config["USER"] + self.password = config["PASSWORD"] + self.db_host = config["DB_HOST"] + self.db_port = config["DB_PORT"] self.db = psycopg2.connect(database=self.sql, user=self.user, password=self.password, @@ -46,7 +46,7 @@ class Storage: file_name text)""" try: self.cursor.execute(q_check) - except Exception as e: + except psycopg2.errors.UndefinedTable: self.cursor.execute(q_create) def insert_book(self, book):