Skip to content

Commit

Permalink
Log user database access
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Jan 19, 2024
1 parent bd30bac commit 4df5a48
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/howitz/users/db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging
import sqlite3

from .model import User


logger = logging.getLogger("__name__")


class UserDB:
class DBException(Exception):
pass
Expand All @@ -29,6 +33,7 @@ def initdb(self):
connection.close()

def connect(self):
logger.debug('Connecting to %s', self.database_file)
connection = sqlite3.connect(self.database_file, check_same_thread=False)
connection.row_factory = self.user_factory
return connection
Expand All @@ -44,6 +49,7 @@ def change_and_return_user(self, username, querystring, params):
return self.get(username, connection)

def get(self, username, connection=None):
logger.debug('Getting user %s', username)
if not connection:
connection = self.connect()
querystring = "SELECT * from user where username=?"
Expand All @@ -52,8 +58,10 @@ def get(self, username, connection=None):
result = query.fetchall()
connection.close()
if not result:
logger.warn('User %s not in database', username)
return None
if len(result) > 1:
logger.error('Multiple %s in database!', username)
raise self.DBException("More than one with that username, b0rked database")
return result[0]

Expand Down

0 comments on commit 4df5a48

Please sign in to comment.