From 87497bfb04a3dbc81cb319287844282d6c11bacc Mon Sep 17 00:00:00 2001 From: jakobsn Date: Mon, 7 Oct 2019 11:57:21 +0200 Subject: [PATCH] can write to guestbook when logged in --- mysql/sql/init.sql | 10 +++++++++- src/app/forms.py | 9 ++++++++- src/app/model.py | 17 ++++++++++++++++- src/app/templates/guestbook.html | 22 ++++++++++++++++++++++ src/app/views.py | 23 +++++++++++++++++++---- 5 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 src/app/templates/guestbook.html diff --git a/mysql/sql/init.sql b/mysql/sql/init.sql index c27a059..c858803 100644 --- a/mysql/sql/init.sql +++ b/mysql/sql/init.sql @@ -3,10 +3,18 @@ CREATE TABLE `db`.`users` ( `userid` INT AUTO_INCREMENT, `username` VARCHAR(45) NULL, `password` VARCHAR(45) NULL, - PRIMARY KEY (`userid`)); + PRIMARY KEY (`userid`) +); +CREATE TABLE `db`.`guestbook` ( + `entryid` INT AUTO_INCREMENT, + `text` VARCHAR(255) NULL, + PRIMARY KEY (`entryid`) +); insert into users values (NULL, "admin", "password"); insert into users values (NULL, "bernt", "inge"); +insert into guestbook values (NULL, "Hello World"); + CREATE USER 'root'@'10.5.0.6' IDENTIFIED BY 'root'; GRANT ALL PRIVILEGES ON db.* TO 'root'@'10.5.0.6'; diff --git a/src/app/forms.py b/src/app/forms.py index 5baab1e..64607db 100644 --- a/src/app/forms.py +++ b/src/app/forms.py @@ -12,4 +12,11 @@ register_form = form.Form( form.Textbox("username", description="Username"), form.Password("password", description="Password"), form.Button("Register", type="submit", description="Register"), -) \ No newline at end of file +) + +# Define the guestbook form +guestbook_form = form.Form( + form.Textbox("entry", description="Entry"), + form.Button("Submit", type="submit", description="submit") +) + diff --git a/src/app/model.py b/src/app/model.py index a3e5d6e..4a49384 100644 --- a/src/app/model.py +++ b/src/app/model.py @@ -23,8 +23,23 @@ def match_user(username, password): user = cursor.fetchall() return user -def register_user(username, password): +def set_user(username, password): cursor = db.cursor() query = ("INSERT INTO users VALUES (NULL, (%s), (%s))") cursor.execute(query, (username, password)) cursor.close() + +def get_guestbook_entries(): + cursor = db.cursor() + query = ("SELECT entryid, text FROM guestbook") + cursor.execute(query) + entries = cursor.fetchall() + cursor.close() + return entries + +def set_guestbook_entry(entry): + cursor = db.cursor() + query = ("INSERT INTO guestbook VALUES (NULL, \"" + entry + "\")") + cursor.execute(query) + cursor.close() + diff --git a/src/app/templates/guestbook.html b/src/app/templates/guestbook.html new file mode 100644 index 0000000..a2eaecc --- /dev/null +++ b/src/app/templates/guestbook.html @@ -0,0 +1,22 @@ +$def with (entries, guesbook_form) + + + Friends + + + + + + +

Guestbook

+ + $if session.username: +

Write an entry

+
+ $:guesbook_form.render() +
+ \ No newline at end of file diff --git a/src/app/views.py b/src/app/views.py index 09c0fb6..ad06d41 100644 --- a/src/app/views.py +++ b/src/app/views.py @@ -1,5 +1,5 @@ import web -from forms import login_form, register_form +from forms import login_form, register_form, guestbook_form import model # Define application routes @@ -7,6 +7,7 @@ urls = ( '/', 'index', '/logout', 'logout', '/register', 'register', + '/guestbook', 'guestbook', ) # Initialize application using the web py framework @@ -25,6 +26,7 @@ else: # Add session to global variables render._add_global(session, 'session') + class index(): # Get main page @@ -47,6 +49,7 @@ class index(): session.username = data.username return render.index(login_form, friends) + class register: # Get the registration form @@ -56,13 +59,25 @@ class register: # Register new user in database def POST(self): data = web.input() - model.register_user(data.username, data.password) - return render.register(register_form) + model.set_user(data.username, data.password) + raise web.seeother('/') + + +class guestbook: + # Get guestbook entries + def GET(self): + entries = model.get_guestbook_entries() + return render.guestbook(entries, guestbook_form) + + def POST(self): + data = web.input() + model.set_guestbook_entry(data.entry) + return web.seeother("/guestbook") class logout: # Kill session def GET(self): session.kill() - return "Logged Out" + raise web.seeother('/')