From 87dedb8e0211684124db4c485f9628684cb51cc5 Mon Sep 17 00:00:00 2001 From: Raphael Maenle Date: Thu, 28 May 2020 15:43:51 +0200 Subject: [PATCH] adds device generation and connection - fingerprint generated first time device connects - saved to database and served to device - connection status set to true, once device requests first package - user can link device via online form --- docker/calendarwatch/Dockerfile | 3 +- server/.routes.py.swp | Bin 0 -> 24576 bytes server/forms.py | 7 +++- server/routes.py | 57 +++++++++++++++++++++----------- server/template/devices.html | 3 ++ 5 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 server/.routes.py.swp 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 0000000000000000000000000000000000000000..75c5b8f022618b5cb4ef89f5787193cfa2f37dcb GIT binary patch literal 24576 zcmeHPdyHIF8NXQGPS8NdED2`Zd)V& z$i2yL?w)hc^LM`U`o6ENufJjYPPVDM!N7B_VI2L{k-e{t7mbUy8isH6+7|E7mldB@ zXPwO#R!ky3ep(e+LF!7&2`de&!(H3*7p<_-t~Gtn4H2#c++W~+#rOIl56ZozW0#z# z$Qj5PSdoE#*j%@1wQ=PY8`e|0k-{2w!TED5X30P14CD;t4CD;t4CD;t4CD;t4CD;_ ze=!jDPBv~sm)@hgwO_qY%Dhij;aT1@l+VK+ZtU zK+ZtUK+ZtUK+ZtUK+ZtUK+ZtUK+eEB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ci9I>yf}@b zRM~X;JPH`}hklZaGUin)HpEg2?E_WAo{5b(u}6xp%B~CZZKBZ#!JUnp^GRXTetL1= zq%+WbDcv0UF5I8ugcL+!gXzFkoQkMQrZK32^hgHKwK)`BG(;SJ5*!jwtwzj|PLg@2 zrbIO%({$b_7E+(J08wM$K8UX~&|LIN#YY)}uP6O1Lr9^wy&P%P7Rw&-9vCdV2F z0_%5$gCp7d7aSYzQH`6I(SB4$4Vzq@h zcM7&t!!ny;l9UV=(fS#!oRQ0*q5+u`t2T8uG1*eiRf%}zRF&+jB*35o=ux996Kj0xF+d=~K*o zXk}>XWgHNfm9N-KlQuiL|DS@pb|0YozyA08-^E@3Uf}aU6?h5v`MZHzfJ49>a20SN z@GsouUjv>39t0MF%Yo-m<}ZLR>GI#@dfL4EIRiNZIRiNZIRiNZIRiNZIRo!X2K42N m-ng#iMyuRtCH>*as{@VzmY*A~{^v$3VS6H_8Vp6TPWeAfU@4&h literal 0 HcmV?d00001 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 %}