diff --git a/docker/calendarwatch/Dockerfile b/docker/calendarwatch/Dockerfile index 29db076..1e850cc 100644 --- a/docker/calendarwatch/Dockerfile +++ b/docker/calendarwatch/Dockerfile @@ -2,8 +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 -RUN pip3 install email-validator +RUN pip3 install uwsgi email-validator random-word 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/.routes.py.swp b/server/.routes.py.swp new file mode 100644 index 0000000..75c5b8f Binary files /dev/null and b/server/.routes.py.swp differ diff --git a/server/forms.py b/server/forms.py index 6ac20f1..637d9c8 100644 --- a/server/forms.py +++ b/server/forms.py @@ -1,7 +1,7 @@ from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms.validators import DataRequired, ValidationError, Email, EqualTo -from database.models import User +from database.models import User, Device import email_validator class LoginForm(FlaskForm): @@ -31,3 +31,8 @@ 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: + raise ValidationError('Device not Found') diff --git a/server/routes.py b/server/routes.py index ac518d7..f3e1ddb 100644 --- a/server/routes.py +++ b/server/routes.py @@ -1,7 +1,6 @@ # Python standard libraries import json import os -import sqlite3 # Third-party libraries import flask @@ -14,6 +13,7 @@ from flask_login import ( login_user, logout_user, ) +from random_word import RandomWords import requests import server.googleHandler as google @@ -64,14 +64,10 @@ def devices(): # TODO add this device to the user - do not create new device form = DeviceForm() if form.validate_on_submit(): - print(form.deviceName.data, flush=True) - device = Device() - device.deviceName = form.deviceName.data - device.connection = False - db.session.add(device) + device = db.session.query(Device).filter(Device.deviceName==form.deviceName.data).first() current_user.devices.append(device) - # TODO add device to database here - db.session.commit() + db.session.commit() + return flask.render_template('devices.html', devices=current_user.devices, form=form) @@ -187,17 +183,21 @@ def credentials_to_dict(credentials): 'scopes': credentials.scopes} -@app.route("/userinfo//calendarevents.json") +@app.route("/device//calendarevents.json") def downloader(device): - path = "/home/calendarwatch/userinfo/" + device + "/" - # return flask.send_from_directory(path, "calendarevents.json") - request_user = db.session.query(User).filter(User.userid==device).first() - print(device, flush=True) - if request_user == None: + path = "/home/calendarwatch/device/" + device + "/" + # TODO change search for device (also in tizen) + request_device = db.session.query(Device).filter(Device.deviceName==device).first() + if request_device == None: + return jsonify(kind="not found") + if request_device.user_id == None: return jsonify(kind="unregistered") + request_device.connection=True + db.session.commit() + request_user = db.session.query(User).filter(User.id==request_device.user_id).first() + routine = Routine() - client_token = google.GC.build_credentials(request_user.google_token.token, request_user.google_token.refresh_token) calendarjson = routine.updateCalendar(request_user, client_token) @@ -206,10 +206,28 @@ def downloader(device): @app.route("/devicefingerprint.json") def generateDeviceFingerprint(): # Create Three Random Words - # check not in Device Database - # Save as new Device - # Send to User - return jsonify(deviceName="Carrot-Enamel-Storm") + r = RandomWords() + while True: + fingerprint = "" + length = 3 + randos = r.get_random_words(limit=length, hasDictionaryDef="true", + includePartOfSpeech="noun", minDictionaryCount=1, + maxDictionaryCount=10, minLength=5, maxLength=10) + for i in range(len(randos)): + fingerprint += randos[i] + if i < length-1: + fingerprint += "-" + + # check not in Device Database + if not db.session.query(Device).filter(Device.deviceName==fingerprint).first(): + # Save as new Device + device = Device(deviceName=fingerprint, connection=False) + db.session.add(device) + db.session.commit() + break; + + # Send to Device + return jsonify(deviceName=fingerprint) # POST @@ -221,7 +239,6 @@ def user(): color = request.json.get('color', None) toggle = request.json.get('toggle', None) - print(request.json, flush=True) if color != None: current_user.updateCalendar(calId, color=color) if toggle != None: diff --git a/server/template/devices.html b/server/template/devices.html index fcfd7d4..a9502ac 100644 --- a/server/template/devices.html +++ b/server/template/devices.html @@ -37,6 +37,9 @@
{{ form.deviceName.label }}
{{ form.deviceName(size=24) }}
{{ form.submit() }}
+ {% for error in form.deviceName.errors %} + [{{ error }}] + {% endfor %}