summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUnto Markkanen <umarkkan@local>2023-12-03 12:13:04 +0200
committerUnto Markkanen <umarkkan@local>2023-12-03 12:13:04 +0200
commite15e4b93c203ba94f413e97955c48d8761a70423 (patch)
treea1400d2de979052cafa3d55697232b375ce32459
parent14365bb113400e576157be9da384c7bf0d80255c (diff)
Moving user related database commands to separate class and file.
-rw-r--r--db/db.py27
-rw-r--r--db/user.py32
-rw-r--r--routes/base.py4
-rw-r--r--routes/tools.py2
4 files changed, 38 insertions, 27 deletions
diff --git a/db/db.py b/db/db.py
index ad6f3ac..8261cf5 100644
--- a/db/db.py
+++ b/db/db.py
@@ -6,35 +6,14 @@ from sqlalchemy.sql import text
from app import app
+from db.user import DBUser
+
class DB:
def __init__(self):
self.db = SQLAlchemy()
self.db.init_app(app)
+ self.user = DBUser(self.db)
- def user_new(self, nick):
- sql = "INSERT \
- INTO users (nick, created) \
- VALUES (:nick, :created) \
- RETURNING id ;"
- result = self.db.session.execute(
- text(sql), { "nick":nick, "created":int(time()) }
- )
- self.db.session.commit()
- return result.fetchone()[0]
-
- def user_get_nick(self, id):
- sql = "SELECT nick \
- FROM users \
- WHERE id=(:id);"
- result = self.db.session.execute(text(sql), { "id":id }).fetchone()
- return result[0] if result else result
-
- def user_exists(self, nick):
- sql = "SELECT COUNT(id) \
- FROM users \
- WHERE nick=(:nick);"
- return self.db.session.execute(text(sql), { "nick":nick }).scalar()
-
def question_new(self, question, neg_ans, pos_ans ):
sql = "INSERT \
INTO questions (question, neg_answer, pos_answer, created) \
diff --git a/db/user.py b/db/user.py
new file mode 100644
index 0000000..a129a13
--- /dev/null
+++ b/db/user.py
@@ -0,0 +1,32 @@
+from time import time
+
+from sqlalchemy.sql import text
+
+class DBUser:
+ def __init__(self, db):
+ self.db = db
+
+ def get_nick(self, id):
+ sql = "SELECT nick \
+ FROM users \
+ WHERE id=(:id);"
+ result = self.db.session.execute(text(sql), { "id":id }).fetchone()
+ return result[0] if result else result
+
+ def new(self, nick):
+ sql = "INSERT \
+ INTO users (nick, created) \
+ VALUES (:nick, :created) \
+ RETURNING id ;"
+ result = self.db.session.execute(
+ text(sql), { "nick":nick, "created":int(time()) }
+ )
+ self.db.session.commit()
+ return result.fetchone()[0]
+
+ def exists(self, nick):
+ sql = "SELECT COUNT(id) \
+ FROM users \
+ WHERE nick=(:nick);"
+ return self.db.session.execute(text(sql), { "nick":nick }).scalar()
+
diff --git a/routes/base.py b/routes/base.py
index d5eae8f..a9c0e19 100644
--- a/routes/base.py
+++ b/routes/base.py
@@ -42,10 +42,10 @@ def new_nick():
if not nick.isalnum():
session["alert"]="Nimimerkissä saa olla vain kirjaimia ja numeroita."
return redirect(next)
- if D.user_exists(nick):
+ if D.user.exists(nick):
session["alert"]="Nimimerkki jonka olet ottamassa on jo varattu."
return redirect(next)
- session["id"] = int(D.user_new(nick))
+ session["id"] = int(D.user.new(nick))
return redirect(next)
diff --git a/routes/tools.py b/routes/tools.py
index 5f6e141..65aff62 100644
--- a/routes/tools.py
+++ b/routes/tools.py
@@ -20,7 +20,7 @@ def get_alert():
def get_nick():
while "id" in session.keys():
- nick = D.user_get_nick(session["id"])
+ nick = D.user.get_nick(session["id"])
if not nick:
del session['id']
if "quiz_id" in session.keys():