diff --git a/.gitignore b/.gitignore index bee8a64..6ecb113 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +src/app/sessions/ diff --git a/mysql/sql/init.sql b/mysql/sql/init.sql index c858803..b9903fa 100644 --- a/mysql/sql/init.sql +++ b/mysql/sql/init.sql @@ -1,20 +1,143 @@ -CREATE TABLE `db`.`users` ( - `userid` INT AUTO_INCREMENT, - `username` VARCHAR(45) NULL, - `password` VARCHAR(45) NULL, - PRIMARY KEY (`userid`) +CREATE TABLE users ( + userid INT UNSIGNED AUTO_INCREMENT, + username VARCHAR(45) UNIQUE NOT NULL, + password VARCHAR(45) NOT NULL, + full_name VARCHAR(200) NOT NULL, + company VARCHAR(50), + phone_number VARCHAR(50), + street_address VARCHAR(50), + city VARCHAR(50), + state VARCHAR(50), + postal_code VARCHAR(50), + country VARCHAR(50), + PRIMARY KEY (userid) ); -CREATE TABLE `db`.`guestbook` ( - `entryid` INT AUTO_INCREMENT, - `text` VARCHAR(255) NULL, - PRIMARY KEY (`entryid`) + +CREATE TABLE guestbook ( + entryid INT UNSIGNED AUTO_INCREMENT, + text VARCHAR(255) NOT NULL, + PRIMARY KEY (entryid) +); + +/* +* Project tables +*/ + +CREATE TABLE teams ( + teamid INT UNSIGNED AUTO_INCREMENT, + team_name VARCHAR(200) NOT NULL, + write_permission BOOLEAN, + PRIMARY KEY (teamid) ); -insert into users values (NULL, "admin", "password"); -insert into users values (NULL, "bernt", "inge"); +CREATE TABLE teams_users ( + teamid INT UNSIGNED NOT NULL, + userid INT UNSIGNED NOT NULL, + PRIMARY KEY (teamid, userid), + FOREIGN KEY (teamid) REFERENCES teams(teamid), + FOREIGN KEY (userid) REFERENCES users(userid) +); + +CREATE TABLE project_category ( + categoryid INT UNSIGNED AUTO_INCREMENT, + category_name VARCHAR(200) UNIQUE NOT NULL, + PRIMARY KEY (categoryid) +); + +CREATE TABLE users_categories ( + userid INT UNSIGNED NOT NULL, + categoryid INT UNSIGNED NOT NULL, + PRIMARY KEY (userid, categoryid), + FOREIGN KEY (userid) REFERENCES users(userid), + FOREIGN KEY (categoryid) REFERENCES project_category(categoryid) +); + +CREATE TABLE projects ( + projectid INT UNSIGNED AUTO_INCREMENT, + categoryid INT UNSIGNED NOT NULL, + userid INT UNSIGNED NOT NULL, + title VARCHAR(200) NOT NULL, + project_description VARCHAR(500) NOT NULL, + project_status VARCHAR(16) NOT NULL, -- This should be either open, in progress or finished + PRIMARY KEY (projectid), + FOREIGN KEY (categoryid) REFERENCES project_category(categoryid), + FOREIGN KEY (userid) REFERENCES users(userid) +); + +CREATE TABLE projects_users ( + projectid INT UNSIGNED NOT NULL, + userid INT UNSIGNED NOT NULL, + read_permission BOOLEAN, + write_permission BOOLEAN, + modify_permission BOOLEAN, + PRIMARY KEY (projectid, userid), + FOREIGN KEY (projectid) REFERENCES projects(projectid), + FOREIGN KEY (userid) REFERENCES users(userid) +); + +CREATE TABLE tasks ( + taskid INT UNSIGNED AUTO_INCREMENT, + projectid INT NOT NULL, + teamid INT UNSIGNED NOT NULL, + title VARCHAR(200) NOT NULL, + task_description VARCHAR(500), + budget INT, + task_status VARCHAR(64), -- This should be Waiting for delivery, Delivered and waiting for acceptance, Delivery has been accepted, awaiting payment, Payment for delivery is done or Declined delivery, please revise + feedback VARCHAR(500), + PRIMARY KEY (taskid), + FOREIGN KEY (teamid) REFERENCES teams(teamid) +); + +CREATE TABLE task_files ( + fileid INT NOT NULL AUTO_INCREMENT, + taskid INT UNSIGNED NOT NULL, + filename VARCHAR(45) NOT NULL, + PRIMARY KEY (fileid), + FOREIGN KEY (taskid) REFERENCES tasks(taskid) +); + +CREATE TABLE delivery ( + deliveryid INT UNSIGNED AUTO_INCREMENT, + taskid INT UNSIGNED NOT NULL, + userid INT UNSIGNED NOT NULL, + filename VARCHAR(45) NOT NULL, + comment VARCHAR(500), + delivery_time DATETIME DEFAULT CURRENT_TIMESTAMP, + responding_userid INT NOT NULL, + responding_time DATETIME, + delivery_status VARCHAR(16), -- Should be Accepted, Pending or Declined + feedback VARCHAR(500), + PRIMARY KEY (deliveryid), + FOREIGN KEY (taskid) REFERENCES tasks(taskid), + FOREIGN KEY (userid) REFERENCES users(userid) +); + +CREATE TABLE task_offer ( + offerid INT UNSIGNED AUTO_INCREMENT, + taskid INT UNSIGNED NOT NULL, + title VARCHAR(200) NOT NULL, + price INT, + description VARCHAR(500), + offer_status VARCHAR(16), -- Should be Accepted, Pending or Declined + feedback VARCHAR(500), + PRIMARY KEY (offerid), + FOREIGN KEY (taskid) REFERENCES tasks(taskid) +); + +/* +* Initial data +*/ + +insert into users values (NULL, "admin", "password", "Admin Modsen", "ntnu", "12345678", "street", "trondheim", "trondheim", "1234", "norway"); insert into guestbook values (NULL, "Hello World"); +insert into project_category values (NULL, "Test"); + +/* +Create default database user +*/ + 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/models/database.py b/src/app/models/database.py index dbca4a2..11bbd81 100644 --- a/src/app/models/database.py +++ b/src/app/models/database.py @@ -3,6 +3,6 @@ import mysql.connector db = mysql.connector.connect( user='root', password='root', - host='0.0.0.0', + host='10.5.0.5', database='db' ) diff --git a/src/app/models/project.py b/src/app/models/project.py new file mode 100644 index 0000000..f75a217 --- /dev/null +++ b/src/app/models/project.py @@ -0,0 +1,23 @@ +from models.database import db + +def get_categories(): + cursor = db.cursor() + query = ("SELECT * FROM project_category") + cursor.execute(query) + categories = cursor.fetchall() + cursor.close() + return categories + +def set_project(categoryid, userid, title, project_description, project_status): + cursor = db.cursor() + query = ("INSERT INTO projects values (NULL, \"1\", \"1\", \"sad\", \"desdf\", \"open\")") + #("INSERT INTO projects values (NULL, \"" + + #categoryid + "\", \"" + userid + "\", \"" + title + "\", \"" + + #project_description + "\", \"" + project_status + "\")") + print(query) + cursor.execute(query) + cursor.close() + categories = get_categories() + print(categories) + return categories + diff --git a/src/app/models/register.py b/src/app/models/register.py index c2aab3a..af5172e 100644 --- a/src/app/models/register.py +++ b/src/app/models/register.py @@ -1,8 +1,11 @@ from models.database import db -def set_user(username, password): +def set_user(username, password, full_name, company, phone_number, + street_address, city, state, postal_code, country): cursor = db.cursor() - query = ("INSERT INTO users VALUES (NULL, \"" + username + - "\", \"" + password + "\")") + query = ("INSERT INTO users VALUES (NULL, \"" + username + "\", \"" + + password + "\", \"" + full_name + "\" , \"" + company + "\", \"" + + phone_number + "\", \"" + street_address + "\", \"" + city + "\", \"" + + state + "\", \"" + postal_code + "\", \"" + country + "\")") cursor.execute(query) cursor.close() diff --git a/src/app/templates/project.html b/src/app/templates/project.html new file mode 100644 index 0000000..8682a59 --- /dev/null +++ b/src/app/templates/project.html @@ -0,0 +1,20 @@ +$def with (nav, project_form) + + + Beelance2 + + + + + + + + $:nav + +

Add project!

+
+ $:project_form.render() +
+ + + diff --git a/src/app/templates/register.html b/src/app/templates/register.html index 51644d8..5b66eea 100644 --- a/src/app/templates/register.html +++ b/src/app/templates/register.html @@ -1,7 +1,7 @@ $def with (nav, register_form) - Friends + Beelance2 @@ -11,7 +11,7 @@ $def with (nav, register_form) $:nav -

Become a friend!

+

Register user!

$:register_form.render()
diff --git a/src/app/views/forms.py b/src/app/views/forms.py index 64607db..9b4cdf8 100644 --- a/src/app/views/forms.py +++ b/src/app/views/forms.py @@ -1,4 +1,5 @@ from web import form +from models.project import get_categories # Define the login form login_form = form.Form( @@ -10,10 +11,29 @@ login_form = form.Form( # Define the register form register_form = form.Form( form.Textbox("username", description="Username"), + form.Textbox("full_name", description="Full name"), + form.Textbox("company", description="Company"), + form.Textbox("phone_number", description="Phone Number"), + form.Textbox("street_address", description="Street address"), + form.Textbox("city", description="City"), + form.Textbox("state", description="State"), + form.Textbox("postal_code", description="Postal code"), + form.Textbox("country", description="Country"), form.Password("password", description="Password"), form.Button("Register", type="submit", description="Register"), ) +# Define the project form + +categories = get_categories() + +project_form = form.Form( + form.Textbox("project_title", description="Title"), + form.Textbox("project_description", description="Description"), + form.Dropdown("category_name", args=categories), + form.Button("Submit", type="submit", description="submit") +) + # Define the guestbook form guestbook_form = form.Form( form.Textbox("entry", description="Entry"), diff --git a/src/app/views/index.py b/src/app/views/index.py index 6bd0261..d665de5 100644 --- a/src/app/views/index.py +++ b/src/app/views/index.py @@ -5,6 +5,7 @@ from views.login import Login from views.logout import Logout from views.register import Register from views.admin import Admin +from views.project import Project # Define application routes urls = ( @@ -13,6 +14,7 @@ urls = ( '/logout', 'Logout', '/register', 'Register', '/guestbook', 'Guestbook', + '/project', 'Project', '/admin', 'Admin', ) diff --git a/src/app/views/login.py b/src/app/views/login.py index 243ef3d..153821e 100644 --- a/src/app/views/login.py +++ b/src/app/views/login.py @@ -27,7 +27,8 @@ class Login(): # If there is a matching user/password in the database the user is logged in if len(user): friends = models.login.get_users() - session.username = data.username + session.username = user[0][1] + session.userid = user[0][0] else: friends = [[],[]] nav = get_nav_bar(session) diff --git a/src/app/views/project.py b/src/app/views/project.py new file mode 100644 index 0000000..70829d9 --- /dev/null +++ b/src/app/views/project.py @@ -0,0 +1,26 @@ +import web +from views.forms import project_form +import models.project +from views.utils import get_nav_bar + +# Get html templates +render = web.template.render('templates/') + +class Project: + + # Get the registration form + def GET(self): + session = web.ctx.session + nav = get_nav_bar(session) + return render.project(nav, project_form) + + # Register new user in database + def POST(self): + data = web.input() + session = web.ctx.session + print(data) + print(session.userid) + categories = models.project.set_project(data.category_name, str(session.userid), + data.project_title, data.project_description, "open") + print(categories) + raise web.seeother('/') diff --git a/src/app/views/register.py b/src/app/views/register.py index 8ad31d6..b44e26f 100644 --- a/src/app/views/register.py +++ b/src/app/views/register.py @@ -18,6 +18,8 @@ class Register: # Register new user in database def POST(self): data = web.input() - models.register.set_user(data.username, data.password) + models.register.set_user(data.username, data.password, + data.full_name, data.company, data.phone_number, data.street_address, + data.city, data.state, data.postal_code, data.country) raise web.seeother('/') diff --git a/src/app/views/utils.py b/src/app/views/utils.py index c63adac..e5434a5 100644 --- a/src/app/views/utils.py +++ b/src/app/views/utils.py @@ -9,6 +9,7 @@ def get_nav_bar(session): else: result += '
  • Register
  • ' result += '
  • Login
  • ' + result += '
  • Projects
  • ' result += '
  • Guestbook
  • ' result += ' ' result += ''