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..6b4a968 --- /dev/null +++ b/classdatabase.py @@ -0,0 +1,47 @@ +import sqlite3 +conn = sqlite3.connect('data/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/data/classdatabase.db b/data/classdatabase.db new file mode 100644 index 0000000..9fedb1f Binary files /dev/null and b/data/classdatabase.db differ diff --git a/data/database.db b/data/database.db index 8b13789..81b71e7 100644 Binary files a/data/database.db and b/data/database.db differ 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/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 @@ 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..a84a22b --- /dev/null +++ b/userdatabase.py @@ -0,0 +1,78 @@ +import sqlite3 +from passlib.hash import sha256_crypt +import uuid +conn = sqlite3.connect( + 'data/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