Don't hardcode the database file

This commit is contained in:
Juhani Krekelä 2018-05-10 11:07:49 +03:00
parent c2ee55e5e9
commit c3556b0c71
4 changed files with 33 additions and 1 deletions

View File

@ -15,3 +15,7 @@ ssl = True
[site] [site]
# This is the site name displayed on e.g. the index page # This is the site name displayed on e.g. the index page
name = A random Buranun-based textboard name = A random Buranun-based textboard
[files]
# This is the path of the database file used by Buranun
database = ./buranun.db

View File

@ -4,6 +4,7 @@ def load(filename):
"""Populate the config variables""" """Populate the config variables"""
global port, ssl, url_prefix global port, ssl, url_prefix
global site_name global site_name
global database_file
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(filename) config.read(filename)
@ -13,3 +14,5 @@ def load(filename):
url_prefix = config['server']['url_prefix'] url_prefix = config['server']['url_prefix']
site_name = config['site']['name'] site_name = config['site']['name']
database_file = config['files']['database']

View File

@ -5,6 +5,12 @@ import sqlite3
from passlib.hash import argon2 from passlib.hash import argon2
import config
# ------------------------------------------------------------------
# General
# ------------------------------------------------------------------
class userstatus(enum.Enum): class userstatus(enum.Enum):
# These will be stored in the database, be mindful of not changing the numbers # These will be stored in the database, be mindful of not changing the numbers
deleted = 0 deleted = 0
@ -13,6 +19,15 @@ class userstatus(enum.Enum):
csprng = random.SystemRandom() 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): def add_user(db, *, username, password, email, parent, status):
"""Add a user to the database """Add a user to the database
Will not commit the changes itself, so run .commit() on the database object yourself""" 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() db.commit()
# ------------------------------------------------------------------
# Boards
# ------------------------------------------------------------------
def list_boards(db):
# TODO: Implement this
...
def initialize_boards(db, boards): def initialize_boards(db, boards):
"""Creates a table of boards """Creates a table of boards
This should never be run outside of the initialization script""" This should never be run outside of the initialization script"""

View File

@ -1,9 +1,11 @@
import sqlite3 import sqlite3
import config
import database import database
if __name__ == '__main__': if __name__ == '__main__':
with sqlite3.connect('buranun.db') as db: config.load('buranun.conf')
with database.connect() as db:
username = input('admin username: ') username = input('admin username: ')
password = input('admin password: ') password = input('admin password: ')
database.initialize_users(db, username, password) database.initialize_users(db, username, password)