From b23cbf17d5202601ee295bb002f82a1c307079fa Mon Sep 17 00:00:00 2001 From: th3r00t Date: Thu, 3 Sep 2020 22:17:07 -0400 Subject: [PATCH] Added createsuperuser function, & execute it from configure. This enables docker users to access the django administration site without requiring access to a cli --- configure | 10 +++++++++- pyShelf.py | 17 +++++++++++++---- src/backend/lib/config.py | 4 ++-- src/backend/lib/pyShelf.py | 39 ++++++++++++++++++++++++++++++++++++-- src/frontend/settings.py | 1 + 5 files changed, 62 insertions(+), 9 deletions(-) diff --git a/configure b/configure index 3eb55ea..1b03abc 100755 --- a/configure +++ b/configure @@ -1,7 +1,11 @@ #!/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 + def load_config(): with open('config.json',"r") as file: @@ -9,11 +13,13 @@ def load_config(): file.close() return config + def write_config(config): with open('config.json',"w") as file: json.dump(config, file) file.close() + def set_secret(config=load_config()): if config["SECRET"] == "": config["SECRET"] = get_random_secret_key() @@ -22,4 +28,6 @@ def set_secret(config=load_config()): else: print(config["SECRET"]) + set_secret() +Admin(Path.cwd()).createsuperuser() diff --git a/pyShelf.py b/pyShelf.py index f9f034b..d33bef5 100755 --- a/pyShelf.py +++ b/pyShelf.py @@ -17,28 +17,37 @@ sys.path.insert(0, PRG_PATH) tx = None -async def runImport(): +async def RunImport(): + """ + Begin live import of books + """ execute_scan(PRG_PATH, config=config) MakeCollections(PRG_PATH, config=config) return "Import Complete" async def socketio(websocket, path): + """ + Web Socket Controller + """ async for message in websocket: config.logger.info("Message Processing") if message == "ping": config.logger.info("<< Ping") - tx = pong(message) + tx = pong() elif message == "importBooks": config.logger.info("Starting Import") tx = "Starting Import . . ." await websocket.send(tx) - await runImport() + await RunImport() tx = "complete" await websocket.send(tx) -def pong(message): +def pong(): + """ + Respond to incoming pings + """ config.logger.info(">> Pong") return "pong" diff --git a/src/backend/lib/config.py b/src/backend/lib/config.py index 9fa78b1..1292bc7 100755 --- a/src/backend/lib/config.py +++ b/src/backend/lib/config.py @@ -37,11 +37,11 @@ class Config: self.db_user = self._data["USER"] self.db_pass = self._data["PASSWORD"] self.SECRET = self._data["SECRET"] - self.debug_build_mode = (_data["BUILD_MODE"].casefold() == "debug") + self.debug_build_mode = (self._data["BUILD_MODE"].casefold() == "debug") def get_logger(self): _logger = logger - _logger.add(pathlib.PurePath(self.root, 'data','{time}.log'), + _logger.add(pathlib.PurePath(self.root, 'data','pyshelf.log'), rotation="2 MB", enqueue=True, colorize=True) return _logger diff --git a/src/backend/lib/pyShelf.py b/src/backend/lib/pyShelf.py index 43d9ef4..a1816d3 100755 --- a/src/backend/lib/pyShelf.py +++ b/src/backend/lib/pyShelf.py @@ -1,14 +1,23 @@ #!/usr/bin/env python3 import asyncio import os -import sys import time - +import datetime import websockets from .config import Config from .library import Catalogue from .storage import Storage +from django.conf import settings +import psycopg2 +from django.contrib.auth.hashers import make_password + +PASSWORD_HASHERS = [ + 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', + 'django.contrib.auth.hashers.PBKDF2PasswordHasher', + 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', + 'django.contrib.auth.hashers.Argon2PasswordHasher', +] class InitFiles: @@ -88,3 +97,29 @@ class Server: self.loop.set_debug(True) await websockets.serve(self.socketio, self.host[0], self.host[1]) await asyncio.sleep(1) + + +class Admin: + + def __init__(self, root): + self.config = Config(root) + self.db = Storage(self.config) + settings.configure() + + def createsuperuser(self): + self.db.cursor.execute("SELECT * FROM interface_user") + _user_list = self.db.cursor.fetchall() + if len(_user_list) > 0: + return False + else: + today = datetime.date.today() + date = psycopg2.Date(today.year, today.month, today.day) + self.db.cursor.execute( + 'INSERT INTO interface_user (username, password, is_staff, is_active, is_superuser, ' + 'date_joined, first_name, last_name, ulvl, email ) ' + 'VALUES( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', + ("pyshelf", make_password("pyshelf"), True, True, True, date, "pyshelf", "default", 1, + "change_or@delete.me")) + self.db.commit() + self.db.close() + return True diff --git a/src/frontend/settings.py b/src/frontend/settings.py index 6e1a91d..7e2bce7 100755 --- a/src/frontend/settings.py +++ b/src/frontend/settings.py @@ -14,6 +14,7 @@ import os import sys from pathlib import Path +sys.path.insert(0, Path.absolute(Path.cwd())) from backend.lib.config import Config CUR_DIR = Path.cwd()