diff --git a/database.py b/database.py index 60f28a8..d39a95b 100644 --- a/database.py +++ b/database.py @@ -14,7 +14,8 @@ class userstatus(enum.Enum): csprng = random.SystemRandom() 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""" global csprgn assert type(username) == str @@ -26,6 +27,9 @@ def add_user(db, *, username, password, email, parent, status): # Generate a user ID. SQLite uses 64 bit signed ints, so generate at max 2⁶³-1 userid = csprng.randrange(2**63) + # Unicode normalize the username + username = unicodedata.normalize('NFKC', username) + # First unicode normalize the password, then hash it with argon2 password = unicodedata.normalize('NFKC', password) password = argon2.hash(password) @@ -36,10 +40,9 @@ def add_user(db, *, username, password, email, parent, status): # Add the user into the database cursor = db.cursor() cursor.execute('INSERT INTO users VALUES (?, ?, ?, ?, ?, ?, ?);', (userid, parent, status, password, username, email, '')) - db.commit() def initialize_users(db, admin_user, admin_password): - """Creates a bare-bones database with only admin user + """Creates a bare-bones user table with only admin user This should never be run outside of the initialization script""" cursor = db.cursor() @@ -58,6 +61,25 @@ def initialize_users(db, admin_user, admin_password): comment text NOT NULL );''') + add_user(db, username = admin_user, password = admin_password, email = '', parent = None, status = userstatus.admin) + db.commit() - add_user(db, username = admin_user, password = admin_password, email = '', parent = None, status = userstatus.admin) +def initialize_boards(db, boards): + """Creates a table of boards + This should never be run outside of the initialization script""" + + cursor = db.cursor() + + cursor.execute('''CREATE TABLE boards ( + id integer NOT NULL PRIMARY KEY, + name text NOT NULL + );''') + + # .executemany() wants them in the format [("board1",), ("board2",), …] + boards = [(board_name,) for board_name in boards] + + # Use NULL to have SQLite generate the IDs automatically + cursor.executemany('INSERT INTO boards VALUES (NULL, ?);', boards) + + db.commit() diff --git a/generate_html.py b/generate_html.py index 64e2eb0..774a3af 100644 --- a/generate_html.py +++ b/generate_html.py @@ -9,6 +9,7 @@ def generate_nav(*, soup): # TODO: Don't generate link to a board if we're at the index nav_tag = soup.new_tag('nav') + # TODO: Read these from the database for board in ['a', 'b', 'his', 'g']: url = config.url_prefix + '/' + urllib.parse.quote(board, safe = '') + '/' a_tag = soup.new_tag('a', href = url) diff --git a/initialize.py b/initialize.py index 4a1a83c..3cc4ec1 100644 --- a/initialize.py +++ b/initialize.py @@ -7,3 +7,6 @@ if __name__ == '__main__': username = input('admin username: ') password = input('admin password: ') database.initialize_users(db, username, password) + + boards = input('boards: ').split() + database.initialize_boards(db, boards)