Added createsuperuser function, & execute it from configure. This

enables docker users to access the django administration site without
requiring access to a cli
This commit is contained in:
th3r00t
2020-09-03 22:17:07 -04:00
parent c0a4c3e8c1
commit b23cbf17d5
5 changed files with 62 additions and 9 deletions

10
configure vendored
View File

@@ -1,7 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import sys
import json import json
from pathlib import Path
from django.core.management.utils import get_random_secret_key from django.core.management.utils import get_random_secret_key
from src.backend.lib.pyShelf import Admin
def load_config(): def load_config():
with open('config.json',"r") as file: with open('config.json',"r") as file:
@@ -9,11 +13,13 @@ def load_config():
file.close() file.close()
return config return config
def write_config(config): def write_config(config):
with open('config.json',"w") as file: with open('config.json',"w") as file:
json.dump(config, file) json.dump(config, file)
file.close() file.close()
def set_secret(config=load_config()): def set_secret(config=load_config()):
if config["SECRET"] == "": if config["SECRET"] == "":
config["SECRET"] = get_random_secret_key() config["SECRET"] = get_random_secret_key()
@@ -22,4 +28,6 @@ def set_secret(config=load_config()):
else: else:
print(config["SECRET"]) print(config["SECRET"])
set_secret() set_secret()
Admin(Path.cwd()).createsuperuser()

View File

@@ -17,28 +17,37 @@ sys.path.insert(0, PRG_PATH)
tx = None tx = None
async def runImport(): async def RunImport():
"""
Begin live import of books
"""
execute_scan(PRG_PATH, config=config) execute_scan(PRG_PATH, config=config)
MakeCollections(PRG_PATH, config=config) MakeCollections(PRG_PATH, config=config)
return "Import Complete" return "Import Complete"
async def socketio(websocket, path): async def socketio(websocket, path):
"""
Web Socket Controller
"""
async for message in websocket: async for message in websocket:
config.logger.info("Message Processing") config.logger.info("Message Processing")
if message == "ping": if message == "ping":
config.logger.info("<< Ping") config.logger.info("<< Ping")
tx = pong(message) tx = pong()
elif message == "importBooks": elif message == "importBooks":
config.logger.info("Starting Import") config.logger.info("Starting Import")
tx = "Starting Import . . ." tx = "Starting Import . . ."
await websocket.send(tx) await websocket.send(tx)
await runImport() await RunImport()
tx = "complete" tx = "complete"
await websocket.send(tx) await websocket.send(tx)
def pong(message): def pong():
"""
Respond to incoming pings
"""
config.logger.info(">> Pong") config.logger.info(">> Pong")
return "pong" return "pong"

View File

@@ -37,11 +37,11 @@ class Config:
self.db_user = self._data["USER"] self.db_user = self._data["USER"]
self.db_pass = self._data["PASSWORD"] self.db_pass = self._data["PASSWORD"]
self.SECRET = self._data["SECRET"] 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): def get_logger(self):
_logger = logger _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) rotation="2 MB", enqueue=True, colorize=True)
return _logger return _logger

View File

@@ -1,14 +1,23 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio import asyncio
import os import os
import sys
import time import time
import datetime
import websockets import websockets
from .config import Config from .config import Config
from .library import Catalogue from .library import Catalogue
from .storage import Storage 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: class InitFiles:
@@ -88,3 +97,29 @@ class Server:
self.loop.set_debug(True) self.loop.set_debug(True)
await websockets.serve(self.socketio, self.host[0], self.host[1]) await websockets.serve(self.socketio, self.host[0], self.host[1])
await asyncio.sleep(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

View File

@@ -14,6 +14,7 @@ import os
import sys import sys
from pathlib import Path from pathlib import Path
sys.path.insert(0, Path.absolute(Path.cwd()))
from backend.lib.config import Config from backend.lib.config import Config
CUR_DIR = Path.cwd() CUR_DIR = Path.cwd()