| @@ -3,10 +3,18 @@ CREATE TABLE `db`.`users` ( | |||||
| `userid` INT AUTO_INCREMENT, | `userid` INT AUTO_INCREMENT, | ||||
| `username` VARCHAR(45) NULL, | `username` VARCHAR(45) NULL, | ||||
| `password` 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, "admin", "password"); | ||||
| insert into users values (NULL, "bernt", "inge"); | insert into users values (NULL, "bernt", "inge"); | ||||
| insert into guestbook values (NULL, "Hello World"); | |||||
| CREATE USER 'root'@'10.5.0.6' IDENTIFIED BY 'root'; | CREATE USER 'root'@'10.5.0.6' IDENTIFIED BY 'root'; | ||||
| GRANT ALL PRIVILEGES ON db.* TO 'root'@'10.5.0.6'; | GRANT ALL PRIVILEGES ON db.* TO 'root'@'10.5.0.6'; | ||||
| @@ -12,4 +12,11 @@ register_form = form.Form( | |||||
| form.Textbox("username", description="Username"), | form.Textbox("username", description="Username"), | ||||
| form.Password("password", description="Password"), | form.Password("password", description="Password"), | ||||
| form.Button("Register", type="submit", description="Register"), | form.Button("Register", type="submit", description="Register"), | ||||
| ) | |||||
| ) | |||||
| # Define the guestbook form | |||||
| guestbook_form = form.Form( | |||||
| form.Textbox("entry", description="Entry"), | |||||
| form.Button("Submit", type="submit", description="submit") | |||||
| ) | |||||
| @@ -23,8 +23,23 @@ def match_user(username, password): | |||||
| user = cursor.fetchall() | user = cursor.fetchall() | ||||
| return user | return user | ||||
| def register_user(username, password): | |||||
| def set_user(username, password): | |||||
| cursor = db.cursor() | cursor = db.cursor() | ||||
| query = ("INSERT INTO users VALUES (NULL, (%s), (%s))") | query = ("INSERT INTO users VALUES (NULL, (%s), (%s))") | ||||
| cursor.execute(query, (username, password)) | cursor.execute(query, (username, password)) | ||||
| cursor.close() | 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() | |||||
| @@ -0,0 +1,22 @@ | |||||
| $def with (entries, guesbook_form) | |||||
| <head> | |||||
| <title>Friends</title> | |||||
| <meta charset="utf-8"> | |||||
| <link rel="stylesheet" type="text/css" href="static/stylesheet.css"> | |||||
| </head> | |||||
| <body> | |||||
| <h1>Guestbook</h1> | |||||
| <ul> | |||||
| $for entry in entries: | |||||
| <li id="entry[0]">$entry[1]</li> | |||||
| </ul> | |||||
| $if session.username: | |||||
| <h2>Write an entry<h2> | |||||
| <form method="POST"> | |||||
| $:guesbook_form.render() | |||||
| </form> | |||||
| </body> | |||||
| @@ -1,5 +1,5 @@ | |||||
| import web | import web | ||||
| from forms import login_form, register_form | |||||
| from forms import login_form, register_form, guestbook_form | |||||
| import model | import model | ||||
| # Define application routes | # Define application routes | ||||
| @@ -7,6 +7,7 @@ urls = ( | |||||
| '/', 'index', | '/', 'index', | ||||
| '/logout', 'logout', | '/logout', 'logout', | ||||
| '/register', 'register', | '/register', 'register', | ||||
| '/guestbook', 'guestbook', | |||||
| ) | ) | ||||
| # Initialize application using the web py framework | # Initialize application using the web py framework | ||||
| @@ -25,6 +26,7 @@ else: | |||||
| # Add session to global variables | # Add session to global variables | ||||
| render._add_global(session, 'session') | render._add_global(session, 'session') | ||||
| class index(): | class index(): | ||||
| # Get main page | # Get main page | ||||
| @@ -47,6 +49,7 @@ class index(): | |||||
| session.username = data.username | session.username = data.username | ||||
| return render.index(login_form, friends) | return render.index(login_form, friends) | ||||
| class register: | class register: | ||||
| # Get the registration form | # Get the registration form | ||||
| @@ -56,13 +59,25 @@ class register: | |||||
| # Register new user in database | # Register new user in database | ||||
| def POST(self): | def POST(self): | ||||
| data = web.input() | 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: | class logout: | ||||
| # Kill session | # Kill session | ||||
| def GET(self): | def GET(self): | ||||
| session.kill() | session.kill() | ||||
| return "Logged Out" | |||||
| raise web.seeother('/') | |||||