From 391a89a7e10151c227ee240f50090ea7c9b6de4f Mon Sep 17 00:00:00 2001 From: Rhiannon Austin <austinrp@cardiff.ac.uk> Date: Tue, 23 Nov 2021 08:59:36 +0000 Subject: [PATCH] Login system first draft --- database.db | Bin 0 -> 16384 bytes database.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ login.html | 36 +++++++++++++++++++ main.py | 22 ++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 database.db create mode 100644 database.py create mode 100644 login.html create mode 100644 main.py diff --git a/database.db b/database.db new file mode 100644 index 0000000000000000000000000000000000000000..d64d2deacb19a21923fe2e73e565fe9c410ddf5d GIT binary patch literal 16384 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU|?ooU|?ZD046j(BSH!%i<v>szm^wd z7;h#6-%7r4-b|coM<qr>U^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLtuD>Kw~yDyST0{ zV>4$-Vp2}3V@gV9NoIatVopFteo1~YI-kWk$kj2#RUyRD$;VXzT~<MZi<2`mMIke< zBsD#?NWsrPM8PlA$44O`$kW#`C{n@OHB!MbG{oQ2&pF7|*VQjXhl`W5v?xcRB(<Uh zt{fy-o?nz*T#%TY8jr<1kODXVAXiU!Kd_M+$f`9Jf?VBPgIxWbU4s?EVa60|WTt3p z1~9RSYilz`g8f=tn3GwO8eg1RSelxboQlR}M)4_{q()w1ZmLdkYN2LR2qU|=q9S8U zG1yFmeFzp4ib)7@@<Iz_GiGolrlb_578hem<R)h3#TS6$5>qZEwK%ybvj7y|n3CnG zNyVTpI7ZB*<|bxhiLio<{JhloywcpH)FMpN@(WV)GV{{oGxAG|Ag%<(VRA-la&~53 zd}dy8Nl|Gs$RQ9(O;F#Qk<Xoh&wY4gvr%7+hQMeDjE2By2#kinXb6mkz-S1JhQMeD zjE2By2+%YHn#~yb7-Sn6oAlE??R6a+rJeIrQk@cuQ(?4%Yf5HbQfX0ohK_=<d7^@I zVsS}MszOL=QBh)Yst&TWtB0Y2k*iNeNl8JmmA-yvZen_Bv0iauX<|_-c%(u%IVDdo zIX_oFIX|x?HLpa!%uwId)Wq1**vKH&B*`Gn(kRI^#Vpw**&<2b(A2=x(#X)j$k^E2 zOgF&MBh<ws%-`SJGt$i@A~3+i-(TO<($vt%$kM>XBHlRFIK|W;Ejh_BK3^}ZAl)7^ zF>Y&aYET}Ys$Q0=kd&I5r;u7vkdt4OnU}6ml98%Vo?n!cqL81aP+XE&R8m?{tf!|3 zbA5Svxn4;|W^rb5a(+r`Qetr`$mJP{d3lK$4ww?!CMF7o#)b-}W(EpI1_s8y`FSac zl?u8FZbg|XiIqAEmWjCvx(cQRxgkY~xy8vDsVNG%si2{NVuj+2{L-8h1yG<QCYLC9 zfbCEyErvN0GM)e%z{trgE=f(%1NHxz`0p_Af8@VI(-bml_-F`>hQMeDjE2By2#kin zXb6mkz-S1JhQMeDjE2By2#kgRO+rAJiCLEued!1<BeNnWVr>Md|If%D%fKH?6DN!s zIvN6_Aut*OqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0>eK98Wk8>8Je6qVT%DMUCLl) zY-Vg>ZfRm{Y-pkzZs`^78*CO8VdCN%6&w&6W*%ao@0?#yshyvu9h#SxnNyNll$sI` ZSvaAsYGh%kYGkQbkOyDXU}|7c4gj^kulWD~ literal 0 HcmV?d00001 diff --git a/database.py b/database.py new file mode 100644 index 0000000..156ef95 --- /dev/null +++ b/database.py @@ -0,0 +1,100 @@ +import os +import sqlite3 +from typing import List + +DATABASE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "database.db") + +class Connection: + def __init__(self): + self.connection = sqlite3.connect(DATABASE) + self.cursor = self.connection.cursor() + self.do_rollback = False + + def commit(self): + self.connection.commit() + self.do_rollback = False + + def execute(self, sql: str, tuple = ()): + self.do_rollback = True + self.cursor.execute(sql, tuple) + + def fetch_all(self): + return self.cursor.fetchall() + + def close(self): + if self.do_rollback: + self.connection.rollback() + + self.connection.close() + + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + +with Connection() as conn: + conn.execute('''CREATE TABLE IF NOT EXISTS Workspaces ( + id integer NOT NULL PRIMARY KEY AUTOINCREMENT, + name text NOT NULL, + address text NOT NULL, + main_photo text NOT NULL, + description text NOT NULL, + website text NOT NULL, + email text NOT NULL, + phone_number text NOT NULL, + opening_hours text, + checkin_instructions text)''') + conn.execute('''CREATE TABLE IF NOT EXISTS AdditionalPhotos ( + id integer NOT NULL PRIMARY KEY AUTOINCREMENT, + url text NOT NULL, + workspace_id integer NOT NULL, + FOREIGN KEY (workspace_id) REFERENCES Workspaces(id))''') + conn.commit() + +class Workspace: + def __init__(self, name: str, + address: str, main_photo: str, additional_photos: List[str], description: str, + website: str, email: str, phone_number: str, opening_hours: str, checkin_instructions: str): + self.id = None + self.name = name + self.address = address + self.main_photo = main_photo + self.additional_photos = additional_photos + self.description = description + self.website = website + self.email = email + self.phone_number = phone_number + self.opening_hours = opening_hours + self.checkin_instructions = checkin_instructions + + def from_query(conn, tuple): + (id, name, address, main_photo, description, website, email, phone_number, opening_hours, checkin_instructions) = tuple + additional_photos = [] + + conn.execute("SELECT url FROM AdditionalPhotos WHERE workspace_id = ?", (str(id))) + for x in conn.cursor.fetchall(): + additional_photos.append(x[0]) + + workspace = Workspace(name, address, main_photo, additional_photos, description, website, email, phone_number, opening_hours, checkin_instructions) + workspace.id = id + return workspace + +def add_workspace(workspace: Workspace): + with Connection() as conn: + conn.execute( + "INSERT INTO Workspaces (name, address, main_photo, description, website, email, phone_number, opening_hours, checkin_instructions) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + (workspace.name, workspace.address, workspace.main_photo, workspace.description, workspace.website, workspace.email, workspace.phone_number, workspace.opening_hours, workspace.checkin_instructions) + ) + id = conn.cursor.lastrowid + for url in workspace.additional_photos: + conn.execute("INSERT INTO AdditionalPhotos (url, workspace_id) VALUES (?, ?)", (url, id)) + conn.commit() + return id + +def get_workspaces(): + with Connection() as conn: + conn.execute("SELECT * FROM Workspaces") + return [Workspace.from_query(conn, x) for x in conn.cursor.fetchall()] + +print(get_workspaces()) diff --git a/login.html b/login.html new file mode 100644 index 0000000..439e7d0 --- /dev/null +++ b/login.html @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + + <link rel="stylesheet" href="static/style.css"> + + <!-- Bootstrap CSS --> + <link rel="stylesheet" href= +"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" + integrity= +"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" + crossorigin="anonymous"> + + <title>{% block title %}{% endblock %} - Tramshed Workspaces</title> +</head> +<form class='myForm' method="POST" action=""> + <p>Enter Username: </p> + <br> +<input type = "text" name = "username" placeholder="Username.." ><br> +<button type = "submit" id='mySubmit'> Submit </button> +</form> +<!-- <div class="parallax"> +<body class="body"> + <!--<img src="static/Tramshed_Logo.jpg" alt="Logo" height="50" width="100">--> +<!-- navigation --> --> + <nav> +<ul> + <li><a href="#home">Home</a></li> + <li><a href="#Map">Map</a></li> + <li><a href="#contactUs">Contact Us</a></li> + <li style="float:right"><a class="active" href="#about">About</a></li> +</ul> + </nav> +<!--{% block mainBlock %}{% endblock %}--> + </body> +</html> diff --git a/main.py b/main.py new file mode 100644 index 0000000..e3e3e93 --- /dev/null +++ b/main.py @@ -0,0 +1,22 @@ +from flask import Flask, request, render_template +import database + +app = Flask(__name__) + +@app.route("/", methods = ["GET"]) +def home(): + if request.method == "GET": + workspaces = database.get_workspaces() + return render_template("home.html", workspaces = workspaces) + +@app.route('/login.html', methods = ["post"]) +def login(): + username = request.form.get('username') + if username == "admin": + return redirect("/templates/home.html") + else: + message = "Wrong Username" + return redirect("/login.html") + +if __name__ == "__main__": + app.run(debug = True) -- GitLab