From adb20dea1410aa1fb7cc63f7c5d28fae971908e4 Mon Sep 17 00:00:00 2001 From: Raphael Maenle Date: Fri, 10 Jul 2020 10:11:31 +0200 Subject: [PATCH] code cleanup and comments --- backend | 2 +- database/db.py | 38 -------------- database/schema.sql | 14 ----- database/user.py | 92 --------------------------------- docker/calendarwatch/Dockerfile | 2 +- server.py | 2 - server/forms.py | 12 ++++- server/googleHandler.py | 4 +- server/routes.py | 17 +++--- server/template/devices.html | 6 +-- 10 files changed, 27 insertions(+), 162 deletions(-) delete mode 100644 database/db.py delete mode 100644 database/schema.sql delete mode 100644 database/user.py diff --git a/backend b/backend index 1a5700e..45cd71c 160000 --- a/backend +++ b/backend @@ -1 +1 @@ -Subproject commit 1a5700e9a0bdc404d0d2a49898dba78d872b53e8 +Subproject commit 45cd71cc4bcddf23f46b1eddcffd8c7fda2b9d41 diff --git a/database/db.py b/database/db.py deleted file mode 100644 index d92ed8e..0000000 --- a/database/db.py +++ /dev/null @@ -1,38 +0,0 @@ -# http://flask.pocoo.org/docs/1.0/tutorial/database/ -import sqlite3 - -import click -from flask import current_app, g -from flask.cli import with_appcontext - -def get_db(): - if "db" not in g: - g.db = sqlite3.connect( - "database/sqlite_db", detect_types=sqlite3.PARSE_DECLTYPES - ) - g.db.row_factory = sqlite3.Row - - return g.db - -def close_db(e=None): - db = g.pop("db/db", None) - - if db is not None: - db.close() - -def init_db(): - db = get_db() - - with current_app.open_resource("database/schema.sql") as f: - db.executescript(f.read().decode("utf8")) - -@click.command("init-db") -@with_appcontext -def init_db_command(): - """Clear the existing data and create new tables.""" - init_db() - click.echo("Initialized the database.") - -def init_app(app): - app.teardown_appcontext(close_db) - app.cli.add_command(init_db_command) diff --git a/database/schema.sql b/database/schema.sql deleted file mode 100644 index 9c23aee..0000000 --- a/database/schema.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE user ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL, - email TEXT UNIQUE NOT NULL, - profile_pic TEXT NOT NULL -); - -CREATE TABLE calendar ( - usr_id TEXT NOT NULL, - calendar_id TEXT PRIMARY KEY, - name TEXT NOT NULL, - toggle TEXT, - color TEXT -); diff --git a/database/user.py b/database/user.py deleted file mode 100644 index 575e94f..0000000 --- a/database/user.py +++ /dev/null @@ -1,92 +0,0 @@ -from flask_login import UserMixin -from pathlib import Path - -from server.database.db import get_db - -class User(UserMixin): - def __init__(self, id_, name, email, profile_pic): - self.id = id_ - self.name = name - self.email = email - self.profile_pic = profile_pic - - @staticmethod - def get(user_id): - db = get_db() - user = db.execute( - "SELECT * FROM user WHERE id = ?", (user_id,) - ).fetchone() - if not user: - return None - - user = User( - id_=user[0], name=user[1], email=user[2], profile_pic=user[3] - ) - return user - - @staticmethod - def create(id_, name, email, profile_pic): - db = get_db() - db.execute( - "INSERT INTO user (id, name, email, profile_pic) " - "VALUES (?, ?, ?, ?)", - (id_, name, email, profile_pic), - ) - db.commit() - - Path(f"userinfo/{id_}").mkdir(parents=True, exist_ok=True) - -class dbCalendar(): - def __init__(self, id_, name, toggle, color): - self.usr_id = id_ - self.name = name - self.toggle = toggle - self.color = color - - @staticmethod - def getCalendars(user_id): - db = get_db() - calendars = db.execute( - "SELECT * FROM calendar WHERE usr_id = ?", (user_id,) - ).fetchall() - - return calendars - - @staticmethod - def getCalendar(user_id, calendar_id): - db = get_db() - calendar = db.execute( - "SELECT * FROM calendar WHERE usr_id = ? AND calendar_id = ?", (user_id, calendar_id) - ).fetchone() - - if not calendar: - return None - - return calendar - - @staticmethod - def updateCalendar(user_id, calendar_name, toggle=None, color=None): - db = get_db() - print("updating") - if(toggle != None): - print(toggle) - db.execute( - "UPDATE calendar SET toggle = ? WHERE usr_id = ? AND name = ?", (toggle, user_id, calendar_name) - ) - db.commit() - - if(color != None): - db.execute( - "UPDATE calendar SET color = ? WHERE usr_id = ? AND name = ?", (color, user_id, calendar_name) - ) - db.commit() - - @staticmethod - def create(user_id, calendar_id, name, color, toggle = 'True'): - db = get_db() - db.execute( - "INSERT INTO calendar (usr_id, calendar_id, name, toggle, color) " - "VALUES (?, ?, ?, ?, ?)", - (user_id, calendar_id, name, toggle, color), - ) - db.commit() diff --git a/docker/calendarwatch/Dockerfile b/docker/calendarwatch/Dockerfile index 1e850cc..812799f 100644 --- a/docker/calendarwatch/Dockerfile +++ b/docker/calendarwatch/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.8-slim-buster RUN apt-get update && apt-get upgrade RUN pip3 install flask Flask-SQLAlchemy flask_migrate flask_login flask_wtf python-dotenv RUN apt-get install gcc libpcre3 libpcre3-dev libmariadbclient-dev -y -RUN pip3 install uwsgi email-validator random-word +RUN pip3 install uwsgi email-validator RandomWords RUN pip3 install google google-oauth google-auth-oauthlib google-api-python-client mysqlclient COPY docker-entrypoint.sh /usr/local/bin/ EXPOSE 8084 diff --git a/server.py b/server.py index 2aa35c4..84de31d 100644 --- a/server.py +++ b/server.py @@ -2,6 +2,4 @@ from server import app if __name__ == "__main__": - context = ('certificate/xip.io.crt', 'certificate/xip.io.key')#certificate and key files -# app.run('0.0.0.0', 8084, ssl_context='adhoc', debug=True) app.run(host='0.0.0.0', port=8084, debug=True) diff --git a/server/forms.py b/server/forms.py index 637d9c8..6f87ad4 100644 --- a/server/forms.py +++ b/server/forms.py @@ -4,12 +4,22 @@ from wtforms.validators import DataRequired, ValidationError, Email, EqualTo from database.models import User, Device import email_validator +''' +LoginForm defines the flask form used for the +email-based login procedure +This Form is based on 'The Flask Mega-Tutorial' +''' class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) remember_me = BooleanField('Remember Me') submit = SubmitField('Sign In') +''' +RegiatrationForm validates username, email and pw +This Form is based on 'The Flask mega-Tutorial' +''' + class RegistrationForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) email = StringField('Email', validators=[DataRequired(), Email()]) @@ -31,7 +41,7 @@ class RegistrationForm(FlaskForm): class DeviceForm(FlaskForm): deviceName=StringField('New Device ID', validators=[DataRequired()]) submit = SubmitField('Add New Device') - +# def validate_deviceName (self, deviceName): device = Device.query.filter_by(deviceName=deviceName.data).first() if device is None: diff --git a/server/googleHandler.py b/server/googleHandler.py index fbf6bc7..095fe23 100644 --- a/server/googleHandler.py +++ b/server/googleHandler.py @@ -25,7 +25,9 @@ import requests from database.models import Calendar as dbCalendar from backend import Calendar, Event -# Configuration +# Configuration class for the google client +# all necessary variables and secrets are defined in this +# aswell as some helper functions class GoogleClient(): def __init__(self): self.CLIENT_SECRETS_FILE = "certificate/client_secret.json" diff --git a/server/routes.py b/server/routes.py index 817cd7d..8308df7 100644 --- a/server/routes.py +++ b/server/routes.py @@ -13,7 +13,7 @@ from flask_login import ( login_user, logout_user, ) -from random_word import RandomWords +from random_words import RandomWords import requests import server.googleHandler as google @@ -74,18 +74,19 @@ def devices(): # if this is a post request from the 'unlink' submittion form # delete this device from the list + form = DeviceForm() if request.method == 'POST': + if request.form.get("submit") == "Unlink": device = db.session.query(Device).filter(Device.deviceName==request.form.get("device")).first() db.session.delete(device) db.session.commit() # if this is part of the device form - form = DeviceForm() - if form.validate_on_submit(): - device = db.session.query(Device).filter(Device.deviceName==form.deviceName.data).first() - current_user.devices.append(device) - db.session.commit() + elif form.validate_on_submit(): + device = db.session.query(Device).filter(Device.deviceName==form.deviceName.data).first() + current_user.devices.append(device) + db.session.commit() return flask.render_template('devices.html', devices=current_user.devices, @@ -222,9 +223,7 @@ def generateDeviceFingerprint(): while True: fingerprint = "" length = 3 - randos = r.get_random_words(limit=length, hasDictionaryDef="true", - includePartOfSpeech="noun", minDictionaryCount=1, - maxDictionaryCount=10, minLength=5, maxLength=10) + randos = r.random_words(count=length) for i in range(len(randos)): fingerprint += randos[i] if i < length-1: diff --git a/server/template/devices.html b/server/template/devices.html index a9502ac..dc7139a 100644 --- a/server/template/devices.html +++ b/server/template/devices.html @@ -34,9 +34,9 @@
{{ form.hidden_tag() }}
-
{{ form.deviceName.label }}
-
{{ form.deviceName(size=24) }}
-
{{ form.submit() }}
+
{{ form.deviceName.label }}
+
{{ form.deviceName(size=24) }}
+
{{ form.submit() }}
{% for error in form.deviceName.errors %} [{{ error }}] {% endfor %}