From 94cc18998716b785fc882f3e90104b9ecdbdf7cb Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Tue, 2 Apr 2019 23:14:37 +0100 Subject: [PATCH 1/5] Add files via upload --- app.py | 98 +++++++++++++++++++++++++++++++++++++++++++----- classdatabase.py | 47 +++++++++++++++++++++++ requirements.txt | 1 + test.py | 2 + userdatabase.py | 78 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+), 10 deletions(-) create mode 100644 classdatabase.py create mode 100644 test.py create mode 100644 userdatabase.py diff --git a/app.py b/app.py index 3394a1c..f245c02 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,31 @@ from flask import Flask, render_template, request, flash, redirect, url_for -from database import Database #Custom Database Script +from userdatabase import Database #Custom Database Script +from classdatabase import ClassDatabase from flask_wtf import Form from wtforms import Form, StringField, PasswordField, BooleanField, SubmitField, TextField, validators from wtforms.validators import DataRequired +from flask_login import LoginManager, current_user, login_user, logout_user, login_required, UserMixin DEBUG = True app = Flask(__name__) app.config.from_object(__name__) app.config['SECRET_KEY'] = 'REPLACEWITHSECUREKEYDAN' +###Login### +login_manager = LoginManager() +login_manager.init_app(app) +login_manager.login_view = '' + +@login_manager.user_loader +def load_user(user_id): + return User(user_id) + +class User(UserMixin): + def __init__(self,id): + self.id = id + + + class ReusableForm(Form): firstname = TextField('firstname:', validators=[validators.required()]) lastname = TextField('lastname:', validators=[validators.required()]) @@ -16,25 +33,43 @@ class ReusableForm(Form): password = PasswordField('password:', validators=[validators.required(), validators.Length(min=6)]) passwordconfirm = PasswordField('passwordconfirm:', validators=[validators.required(), validators.Length(min=6)]) +class ReusableClassForm(Form): + day = TextField('day:', validators=[validators.required()]) + name = TextField('name:', validators=[validators.required()]) + time = TextField('time:', validators=[validators.required()]) + location = PasswordField('location:', validators=[validators.required()]) + + """From this point @app.route signifies adress call that triggers templates""" @app.route('/')#Defult view of webapp def index(): - title = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] - return render_template('index.html', title = title) + details = ClassDatabase.classdetails() + if current_user.is_active == True: + return render_template('index.html', loggedin = 1, details = details) + else: + return render_template('index.html', loggedin = 0, details = details) @app.route('/login', methods=['GET', 'POST'])#Login Interface def login(): form = ReusableForm(request.form) if request.method == 'POST': + email=request.form['email'] password=request.form['password'] if Database.check(email, password) == True: - return redirect(url_for('index')) + login_user(User(email)) + return render_template('index.html', loggedin = 1) + #return redirect(url_for('index', loggedin= 1)) else: flash('User Not Found.') - return render_template('loginform.html', form=form) + return render_template('loginform.html', form=form, loggedin = 0) + +@app.route('/logout') +def logout(): + logout_user() + return redirect(url_for('index')) @app.route('/signup', methods=['GET', 'POST'])#Sign Up Interface def signup(): @@ -59,9 +94,9 @@ def signup(): elif len(password) < 6: flash('Try Again - Password Needs To Be Over 6 Characters.') elif form.validate(): - flash('You have signed up!') + flash('You have signed up! Now Login') db = Database(email, firstname, lastname, phone , password) - db.create() + #db.create() db.hashpw() db.add() @@ -73,13 +108,56 @@ def signup(): def forrgot_password(): return render_template('forgot_password.html') +@app.route('/account') +@login_required +def account(): + uuid = Database.uuid(current_user.get_id()) + details = Database.userdetails(uuid) + firstname = details[0] + lastname = details[1] + phone = details[2] + return render_template('account.html', email = current_user.get_id(), firstname = firstname, lastname = lastname, phone = phone, loggedin = 1) + #return current_user.get_id() + @app.route('/about') def aboutme(): - return render_template('about.html') + if current_user.is_active == True: + return render_template('about.html', loggedin = 1) + else: + return render_template('about.html', loggedin = 0) + +@app.route('/admin', methods=['GET', 'POST']) +#@login_required +def admin(): + details = ClassDatabase.classdetails() + length = len(details) + l = [] + for x in details[0]: + l.append(x) + + return render_template('admin.html', loggedin = 1, details = details, length = length) + +@app.route('/admin/add', methods=['GET', 'POST']) +#@login_required +def adminadd(): + form = ReusableClassForm(request.form) + if request.method == 'POST': + day=request.form['day'] + name=request.form['name'] + time=request.form['time'] + location=request.form['location'] + db = ClassDatabase(day,name,time,location) + db.add() + return render_template('adminadd.html', loggedin = 1, form = form) + + @app.route('/location') -def contact(): - return render_template('googlemaps.html') +def location(): + if current_user.is_active == True: + return render_template('googlemaps.html', loggedin = 1) + else: + return render_template('googlemaps.html', loggedin = 0) if __name__ == "__main__": app.run() diff --git a/classdatabase.py b/classdatabase.py new file mode 100644 index 0000000..0ea4981 --- /dev/null +++ b/classdatabase.py @@ -0,0 +1,47 @@ +import sqlite3 +conn = sqlite3.connect('C:/Users/danwi/Desktop/classdatabase.db', check_same_thread=False) +c = conn.cursor() + +class ClassDatabase(): + def __init__(self, day, name, time, location): + self.location = location + self.time = time + self.day = day + self.name = name + + def create(self): + try: + c.execute('''CREATE TABLE classes + (day text, name text, time text, location text)''') + conn.commit() + except: + print("DB already created") + else: + print("Fatal Error") + + def add(self): + c.execute('INSERT INTO classes(day, name, time, location) VALUES(?,?,?,?)', + (self.day, self.name, self.time, self.location)) + conn.commit() + + + @staticmethod + def classdetails(): + try: + c.execute('SELECT * FROM classes') + item = c.fetchall() + return item + except: + return False + """@staticmethod + def classdetails(day): + try: + c.execute('SELECT * FROM classes WHERE day=?',(day,)) + item = c.fetchone() + details = [item[0], item[1], item[2], item[3]] + return details + except: + return False""" + + + diff --git a/requirements.txt b/requirements.txt index 5cc7c45..22dc222 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ MarkupSafe==1.0 passlib==1.7.1 Werkzeug==0.14.1 WTForms==2.2.1 +Flask-Login==0.4.1 diff --git a/test.py b/test.py new file mode 100644 index 0000000..b31015e --- /dev/null +++ b/test.py @@ -0,0 +1,2 @@ +from database import Database +print(Database.details("danwill1210@gmail.com")) diff --git a/userdatabase.py b/userdatabase.py new file mode 100644 index 0000000..785ec94 --- /dev/null +++ b/userdatabase.py @@ -0,0 +1,78 @@ +import sqlite3 +from passlib.hash import sha256_crypt +import uuid +conn = sqlite3.connect( + 'C:/Users/danwi/Desktop/database.db', check_same_thread=False) +c = conn.cursor() +passwordhash = sha256_crypt.hash( + "djhewufhu23r82urjfnjkdshfkjh8ry8yuwhe23rj") # hash + + +class Database(): + def __init__(self, email, firstname, lastname, phone, password): + self.email = email + self.firstname = firstname + self.lastname = lastname + self.phone = phone + self.password = password + + def create(self): + try: + c.execute('''CREATE TABLE users + (userid text, email text, password text)''') + conn.commit() + c.execute('''CREATE TABLE usersinfo + (userid text, firstname text, lastname text, phone text)''') # phone not integer as python doesnt support + conn.commit() + except: + print("DB already created") + else: + print("Fatal Error") + + def hashpw(self): + self.password_hash = sha256_crypt.encrypt(self.password) + + def add(self): + # Making a random uuid using the python uuid moduel, 4 is the only truly random. + userid = str(uuid.uuid4()) + c.execute('SELECT * FROM users WHERE userid=?', (userid,)) + conn.commit() + + c.execute('INSERT INTO users(userid, email, password) VALUES(?,?,?)', + (userid, self.email, self.password_hash)) + c.execute('INSERT INTO usersinfo(userid, firstname, lastname, phone) VALUES(?,?,?,?)', + (userid, self.firstname, self.lastname, self.phone)) + print('User inserted {} {} {}'.format( + userid, self.email, self.password_hash)) + conn.commit() + + @staticmethod + def check(email, password): + try: + c.execute('SELECT * FROM users WHERE email=?',(email,)) + item = c.fetchone() + if sha256_crypt.verify(password, item[2]) == True: + return True + else: + return False + except: + return False + + @staticmethod + def uuid(email): + try: + c.execute('SELECT * FROM users WHERE email=?',(email,)) + item = c.fetchone() + return str(item[0]) + except: + return False + + @staticmethod + def userdetails(uuid): + try: + c.execute('SELECT * FROM usersinfo WHERE userid=?',(uuid,)) + item = c.fetchone() + details = [item[1], item[2], item[3]] + return details + except: + return False From 0f6cfa51cbc4c632376a0be5ebadf5c4c13d5a18 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Tue, 2 Apr 2019 23:16:38 +0100 Subject: [PATCH 2/5] Update classdatabase.py --- classdatabase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classdatabase.py b/classdatabase.py index 0ea4981..6b4a968 100644 --- a/classdatabase.py +++ b/classdatabase.py @@ -1,5 +1,5 @@ import sqlite3 -conn = sqlite3.connect('C:/Users/danwi/Desktop/classdatabase.db', check_same_thread=False) +conn = sqlite3.connect('data/classdatabase.db', check_same_thread=False) c = conn.cursor() class ClassDatabase(): From acaa9b818b6dfde33bb13adbf7195005e1957e60 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Tue, 2 Apr 2019 23:17:10 +0100 Subject: [PATCH 3/5] Add files via upload --- data/classdatabase.db | Bin 0 -> 8192 bytes data/database.db | Bin 1 -> 12288 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 data/classdatabase.db diff --git a/data/classdatabase.db b/data/classdatabase.db new file mode 100644 index 0000000000000000000000000000000000000000..9fedb1ffbcf4bfcde384813593a200308775a554 GIT binary patch literal 8192 zcmeI%ze>a~90%~Ewg`gMoN?=P5!b@1$ED)Pm@iyGAXx4YVof=;90b4!(&m z;(G|DcdfWNxqW{mLSjx+CX`CJ|4jvpC}2;s*Z}d#QNlT0ZFmUW!6?sm^}VTrGsoRJq*QZ{Oy&`%2tT_rrZR z2?7ETfB*y_009U<00Izz00bZafqxR%v7O<-_mYREs?GcTM%{~AOHXW literal 0 HcmV?d00001 diff --git a/data/database.db b/data/database.db index 8b137891791fe96927ad78e64b0aad7bded08bdc..81b71e756e65a3fa3443fe02283ef3293273486b 100644 GIT binary patch literal 12288 zcmeI$O-tiI7zgl4>mn#pZ)Fj2gn$aBn{*nJ9t3L*9<*w=T}2NIW0UC6ywpre)my)b zU&N0lnA+kCvWtfvJp3P$nP+BTp5%9%X*CZ;>QO&VLY`7a7Kx&e9ZCry`N^AgFOkdI znMpqCly~{R(mc_gl7(zFS5QgeA!`s2fB*y_009U<00Izz00bZafp02spPMbNtSG~J z$}a=2FTF&H$d6x>eEqjmYdf@E+dXvX>+~Fb7we;G;nLK*O*g485}8Ik^d{qge;D?z z<7m9J@mHBG7K_So>8tNHuRmV(HXwfi9^*?leizba@%C4uIp+;_Aho@7kkn@==%MwmU`eEl}p|7&3>;` zZ?v0sv)#BmHhy(aZlrmC&Tgw*IH&eNlunKD`Tvm=9;fz$*a!j;fB*y_009U<00Izz o00bZafoTfVvYUjT%d20xN!a6&@Pf1KGC}ZATFhn@#%$Yu26ohqcmMzZ literal 1 Icmd-A000XB3jhEB From b43d7a695345798c3b658622b1ad83223e7fa284 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Tue, 2 Apr 2019 23:22:02 +0100 Subject: [PATCH 4/5] Update userdatabase.py --- userdatabase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/userdatabase.py b/userdatabase.py index 785ec94..a84a22b 100644 --- a/userdatabase.py +++ b/userdatabase.py @@ -2,7 +2,7 @@ from passlib.hash import sha256_crypt import uuid conn = sqlite3.connect( - 'C:/Users/danwi/Desktop/database.db', check_same_thread=False) + 'data/database.db', check_same_thread=False) c = conn.cursor() passwordhash = sha256_crypt.hash( "djhewufhu23r82urjfnjkdshfkjh8ry8yuwhe23rj") # hash From c7a6a6c87f3c17a64955d76a6ae00c2246e916a5 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Tue, 2 Apr 2019 23:26:07 +0100 Subject: [PATCH 5/5] Add files via upload --- templates/about.html | 18 +++++++++++++++++- templates/account.html | 36 ++++++++++++++++++++++++++++++++++++ templates/admin.html | 30 ++++++++++++++++++++++++++++++ templates/adminadd.html | 35 +++++++++++++++++++++++++++++++++++ templates/index.html | 30 ++++++++++++------------------ templates/layout.html | 13 ++++++++++--- 6 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 templates/account.html create mode 100644 templates/admin.html create mode 100644 templates/adminadd.html diff --git a/templates/about.html b/templates/about.html index 7a30096..b4b64a7 100644 --- a/templates/about.html +++ b/templates/about.html @@ -2,6 +2,22 @@ {% block title %}Sign Up{% endblock %} {% block body %}
- +
+

About Me

+

This site has been setup to help me manage my buisness. If you need to contact me, my details are below.

+
+ + + + + +

buisnessemail@fitness.com +
077777777777 +
January 30, 1974

+ +
+
{% endblock %} diff --git a/templates/account.html b/templates/account.html new file mode 100644 index 0000000..c35c1f6 --- /dev/null +++ b/templates/account.html @@ -0,0 +1,36 @@ +{% extends "layout.html" %} +{% block title %}Login{% endblock %} +{% block body %} + + +

+
+
+
+ +
+ +
+

{{firstname}} {{lastname}}

+
Email: {{email}}
+
Mobile: {{phone}}
+
Class Count-
+
More...
+
+ + +
+
+ +{% endblock %} diff --git a/templates/admin.html b/templates/admin.html new file mode 100644 index 0000000..a723078 --- /dev/null +++ b/templates/admin.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} +{% block title %}{% endblock %} +{% block body %} +
+

Upcoming Classes

+
+
+
+ + + + {% for x in details %} + + + + + + + + + + + + + {% endfor %} +
{{x[0]}}
{{x[1]}}{{x[2]}}Location: {{x[3]}}
+ + + +{% endblock %} \ No newline at end of file diff --git a/templates/adminadd.html b/templates/adminadd.html new file mode 100644 index 0000000..0b36cce --- /dev/null +++ b/templates/adminadd.html @@ -0,0 +1,35 @@ + + + +{% extends "layout.html" %} +{% block title %}{% endblock %} +{% block body %} + +
+
+
+

Add Class

+
+
+ {{ form.csrf }} +
+

Enter Class Details

+ +
+
+ +
+
+ +
+
+ +
+ +
+
+
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 6e8e50a..eb92088 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,33 +7,27 @@

Upcoming Classes

-{% for item in title %} -
+
+
+ +
+ {% for x in details %} - + - - - - - - - - - - - - - + + + + {% endfor %}
{{item}}{{x[0]}}
Class NameClass TimeClass Location
MaryMoemary@example.com
JulyDooleyjuly@example.com{{x[1]}}{{x[2]}}Location: {{x[3]}}
-{% endfor %} -{% endblock %} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 4104ffa..e56ccb7 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -16,17 +16,24 @@