diff --git a/buranun.conf.example b/buranun.conf.example index d685680..3b6f61e 100644 --- a/buranun.conf.example +++ b/buranun.conf.example @@ -15,3 +15,7 @@ ssl = True [site] # This is the site name displayed on e.g. the index page name = A random Buranun-based textboard + +[files] +# This is the path of the database file used by Buranun +database = ./buranun.db diff --git a/config.py b/config.py index 9277680..8992e78 100644 --- a/config.py +++ b/config.py @@ -4,6 +4,7 @@ def load(filename): """Populate the config variables""" global port, ssl, url_prefix global site_name + global database_file config = configparser.ConfigParser() config.read(filename) @@ -13,3 +14,5 @@ def load(filename): url_prefix = config['server']['url_prefix'] site_name = config['site']['name'] + + database_file = config['files']['database'] diff --git a/database.py b/database.py index 383e566..98843ab 100644 --- a/database.py +++ b/database.py @@ -5,6 +5,12 @@ import sqlite3 from passlib.hash import argon2 +import config + +# ------------------------------------------------------------------ +# General +# ------------------------------------------------------------------ + class userstatus(enum.Enum): # These will be stored in the database, be mindful of not changing the numbers deleted = 0 @@ -13,6 +19,15 @@ class userstatus(enum.Enum): csprng = random.SystemRandom() +def connect(): + """Connect to the database + Requires config.load() to have been called beforehand""" + return sqlite3.connect(config.database_file) + +# ------------------------------------------------------------------ +# Users +# ------------------------------------------------------------------ + def add_user(db, *, username, password, email, parent, status): """Add a user to the database Will not commit the changes itself, so run .commit() on the database object yourself""" @@ -68,6 +83,14 @@ def initialize_users(db, admin_user, admin_password): db.commit() +# ------------------------------------------------------------------ +# Boards +# ------------------------------------------------------------------ + +def list_boards(db): + # TODO: Implement this + ... + def initialize_boards(db, boards): """Creates a table of boards This should never be run outside of the initialization script""" diff --git a/initialize.py b/initialize.py index 3cc4ec1..f6dc32a 100644 --- a/initialize.py +++ b/initialize.py @@ -1,9 +1,11 @@ import sqlite3 +import config import database if __name__ == '__main__': - with sqlite3.connect('buranun.db') as db: + config.load('buranun.conf') + with database.connect() as db: username = input('admin username: ') password = input('admin password: ') database.initialize_users(db, username, password)