summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md15
-rw-r--r--TABLES5
-rw-r--r--app.py3
-rw-r--r--db_actions.py34
-rw-r--r--nick.py30
-rw-r--r--routes.py29
-rw-r--r--templates/analyse.html1
-rw-r--r--templates/answer.html1
-rw-r--r--templates/create.html1
-rw-r--r--templates/info.html1
-rw-r--r--templates/moderate.html1
-rw-r--r--templates/nick.html5
12 files changed, 109 insertions, 17 deletions
diff --git a/README.md b/README.md
index e055a58..65f957e 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,21 @@
# kyselma
Kyselmä - kysele, vastaile ja tutki tuloksia
+DONE:
+- Tietokantayhteys
+- Nimierkkitaulu
+- Nimimerkin käyttöönotto ja tarkistus
+- Virheviestien kuljetus
+- Nettisivurunko
+
+TODO:
+- Kysymystaulu
+- Kyselytaulu
+- Kysymyksen lisäys
+- Kysymyksen näyttö
+- Ulkoasu
+...
+
Tarkoitus on luoda sivu jossa voi luoda kysymyksiä ja kyselyitä, joita
täytetään anonyymillä nimimerkillä.
diff --git a/TABLES b/TABLES
new file mode 100644
index 0000000..1c5c372
--- /dev/null
+++ b/TABLES
@@ -0,0 +1,5 @@
+CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ nick TEXT,
+ created INT
+);
diff --git a/app.py b/app.py
index e38d434..3e99f07 100644
--- a/app.py
+++ b/app.py
@@ -1,8 +1,11 @@
from flask import Flask
from os import getenv
+from db_actions import db
app = Flask(__name__, static_url_path='')
app.secret_key = getenv("SECRET_KEY")
+app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres@localhost/postgres"
+db.init_app(app)
import routes
import nick \ No newline at end of file
diff --git a/db_actions.py b/db_actions.py
new file mode 100644
index 0000000..5ee2f16
--- /dev/null
+++ b/db_actions.py
@@ -0,0 +1,34 @@
+from time import time
+
+from flask import Flask
+from flask_sqlalchemy import SQLAlchemy
+from sqlalchemy.orm import DeclarativeBase
+from sqlalchemy.sql import text
+
+class Base(DeclarativeBase):
+ pass
+
+db = SQLAlchemy(model_class=Base)
+
+def user_new(nick):
+ sql = "INSERT \
+ INTO users (nick, created) \
+ VALUES (:nick, :created) \
+ RETURNING id ;"
+ result = db.session.execute(
+ text(sql), { "nick":nick, "created":int(time()) }
+ )
+ db.session.commit()
+ return result.fetchone()[0]
+
+def user_get_nick(id):
+ sql = "SELECT nick \
+ FROM users \
+ WHERE id=(:id);"
+ return db.session.execute(text(sql), { "id":id }).fetchone()[0]
+
+def user_exists(nick):
+ sql = "SELECT COUNT(id) \
+ FROM users \
+ WHERE nick=(:nick);"
+ return db.session.execute(text(sql), { "nick":nick }).scalar()
diff --git a/nick.py b/nick.py
index e5f7186..438dc8e 100644
--- a/nick.py
+++ b/nick.py
@@ -1,10 +1,28 @@
from app import app
from flask import render_template, session, request, redirect
+import db_actions as D
-@app.route("/nick",methods=["POST"])
-def set_nick():
+
+@app.route("/new_nick",methods=["POST"])
+def new_nick():
nick = request.form["nick"]
- session["nick"] = nick
- return redirect("/")
-
-
+ if "id" in session.keys():
+ msg = "You already have a nick."
+ elif D.user_exists(nick):
+ msg = "Nick is already reserved."
+ elif msg := invalid_nick(nick):
+ pass
+ else:
+ session["id"] = D.user_new(nick)
+ return redirect("/")
+ session["alert"]="Nick in not created: "+msg
+ return redirect("/#create")
+
+
+def invalid_nick(nick):
+ if len(nick)<4:
+ return "Nick is too short"
+ if not nick.isalnum():
+ return "Only letters and numbers are allowed"
+ return 0
+ \ No newline at end of file
diff --git a/routes.py b/routes.py
index 09ba156..fde054f 100644
--- a/routes.py
+++ b/routes.py
@@ -1,30 +1,41 @@
from app import app
from flask import render_template,session
+def get_alert():
+ if "alert" in session:
+ alert = session["alert"]
+ del session["alert"]
+ return f"{alert}"
+ return ""
+
+
+
@app.route("/")
def index():
return app.send_static_file("index.html")
@app.route("/pages/info.html")
def info():
- return render_template("info.html")
+ return render_template("info.html", alert=get_alert() )
@app.route("/pages/create.html")
def create():
- if "nick" not in session:
- return render_template("nick.html")
- return render_template("create.html")
+ if "id" not in session:
+ return render_template("nick.html", alert=get_alert() )
+ return render_template("create.html", alert=get_alert() )
@app.route("/pages/answer.html")
def answer():
- if "nick" not in session:
- return render_template("nick.html")
- return render_template("answer.html")
+ if "id" not in session:
+ return render_template("nick.html", alert=get_alert() )
+ return render_template("answer.html", alert=get_alert() )
@app.route("/pages/analyse.html")
def analyse():
- return render_template("analyse.html")
+ if "id" not in session:
+ return render_template("nick.html", alert=get_alert() )
+ return render_template("analyse.html", alert=get_alert() )
@app.route("/pages/moderate.html")
def moderate():
- return render_template("moderate.html")
+ return render_template("moderate.html", alert=get_alert() )
diff --git a/templates/analyse.html b/templates/analyse.html
index ad50883..3220412 100644
--- a/templates/analyse.html
+++ b/templates/analyse.html
@@ -1 +1,2 @@
+<div id="alertbox">{{ alert }}</div>
<h1>analyse</h1>
diff --git a/templates/answer.html b/templates/answer.html
index 70bdfb9..808dde8 100644
--- a/templates/answer.html
+++ b/templates/answer.html
@@ -1 +1,2 @@
+<div id="alertbox">{{ alert }}</div>
<h1>answer</h1>
diff --git a/templates/create.html b/templates/create.html
index 8335934..15af509 100644
--- a/templates/create.html
+++ b/templates/create.html
@@ -1 +1,2 @@
+<div id="alertbox">{{ alert }}</div>
<h1>create</h1>
diff --git a/templates/info.html b/templates/info.html
index 73fbfe0..3f984a6 100644
--- a/templates/info.html
+++ b/templates/info.html
@@ -1 +1,2 @@
+<div id="alertbox">{{ alert }}</div>
<h1>info</h1>
diff --git a/templates/moderate.html b/templates/moderate.html
index 2a491af..116f2ff 100644
--- a/templates/moderate.html
+++ b/templates/moderate.html
@@ -1 +1,2 @@
+<div id="alertbox">{{ alert }}</div>
<h1>moderate</h1>
diff --git a/templates/nick.html b/templates/nick.html
index 72c641d..f3e1696 100644
--- a/templates/nick.html
+++ b/templates/nick.html
@@ -1,5 +1,6 @@
-<form action="/nick" method="POST">
+<div id="alertbox">{{ alert }}</div>
+<form action="/new_nick" method="POST">
Anna itsellesi nimimerkki ensin:
-<textarea name="nick" rows="1" cols="40"></textarea>
+<input type="text" name="nick" rows="1" cols="40"></textarea>
<input type="submit" value="Lähetä">
</form>