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
This commit is contained in:
Raphael Maenle 2020-05-28 15:43:51 +02:00
parent 7b82086ff0
commit 87dedb8e02
5 changed files with 47 additions and 23 deletions

View File

@ -2,8 +2,7 @@ FROM python:3.8-slim-buster
RUN apt-get update && apt-get upgrade RUN apt-get update && apt-get upgrade
RUN pip3 install flask Flask-SQLAlchemy flask_migrate flask_login flask_wtf python-dotenv 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 apt-get install gcc libpcre3 libpcre3-dev libmariadbclient-dev -y
RUN pip3 install uwsgi RUN pip3 install uwsgi email-validator random-word
RUN pip3 install email-validator
RUN pip3 install google google-oauth google-auth-oauthlib google-api-python-client mysqlclient RUN pip3 install google google-oauth google-auth-oauthlib google-api-python-client mysqlclient
COPY docker-entrypoint.sh /usr/local/bin/ COPY docker-entrypoint.sh /usr/local/bin/
EXPOSE 8084 EXPOSE 8084

BIN
server/.routes.py.swp Normal file

Binary file not shown.

View File

@ -1,7 +1,7 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, ValidationError, Email, EqualTo from wtforms.validators import DataRequired, ValidationError, Email, EqualTo
from database.models import User from database.models import User, Device
import email_validator import email_validator
class LoginForm(FlaskForm): class LoginForm(FlaskForm):
@ -31,3 +31,8 @@ class RegistrationForm(FlaskForm):
class DeviceForm(FlaskForm): class DeviceForm(FlaskForm):
deviceName=StringField('New Device ID', validators=[DataRequired()]) deviceName=StringField('New Device ID', validators=[DataRequired()])
submit = SubmitField('Add New Device') 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')

View File

@ -1,7 +1,6 @@
# Python standard libraries # Python standard libraries
import json import json
import os import os
import sqlite3
# Third-party libraries # Third-party libraries
import flask import flask
@ -14,6 +13,7 @@ from flask_login import (
login_user, login_user,
logout_user, logout_user,
) )
from random_word import RandomWords
import requests import requests
import server.googleHandler as google import server.googleHandler as google
@ -64,14 +64,10 @@ def devices():
# TODO add this device to the user - do not create new device # TODO add this device to the user - do not create new device
form = DeviceForm() form = DeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
print(form.deviceName.data, flush=True) device = db.session.query(Device).filter(Device.deviceName==form.deviceName.data).first()
device = Device()
device.deviceName = form.deviceName.data
device.connection = False
db.session.add(device)
current_user.devices.append(device) current_user.devices.append(device)
# TODO add device to database here
db.session.commit() db.session.commit()
return flask.render_template('devices.html', return flask.render_template('devices.html',
devices=current_user.devices, devices=current_user.devices,
form=form) form=form)
@ -187,17 +183,21 @@ def credentials_to_dict(credentials):
'scopes': credentials.scopes} 'scopes': credentials.scopes}
@app.route("/userinfo/<path:device>/calendarevents.json") @app.route("/device/<path:device>/calendarevents.json")
def downloader(device): def downloader(device):
path = "/home/calendarwatch/userinfo/" + device + "/" path = "/home/calendarwatch/device/" + device + "/"
# return flask.send_from_directory(path, "calendarevents.json") # TODO change search for device (also in tizen)
request_user = db.session.query(User).filter(User.userid==device).first() request_device = db.session.query(Device).filter(Device.deviceName==device).first()
print(device, flush=True) if request_device == None:
if request_user == None: return jsonify(kind="not found")
if request_device.user_id == None:
return jsonify(kind="unregistered") return jsonify(kind="unregistered")
routine = Routine() 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, client_token = google.GC.build_credentials(request_user.google_token.token,
request_user.google_token.refresh_token) request_user.google_token.refresh_token)
calendarjson = routine.updateCalendar(request_user, client_token) calendarjson = routine.updateCalendar(request_user, client_token)
@ -206,10 +206,28 @@ def downloader(device):
@app.route("/devicefingerprint.json") @app.route("/devicefingerprint.json")
def generateDeviceFingerprint(): def generateDeviceFingerprint():
# Create Three Random Words # Create Three Random Words
# check not in Device Database r = RandomWords()
# Save as new Device while True:
# Send to User fingerprint = ""
return jsonify(deviceName="Carrot-Enamel-Storm") 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 # POST
@ -221,7 +239,6 @@ def user():
color = request.json.get('color', None) color = request.json.get('color', None)
toggle = request.json.get('toggle', None) toggle = request.json.get('toggle', None)
print(request.json, flush=True)
if color != None: if color != None:
current_user.updateCalendar(calId, color=color) current_user.updateCalendar(calId, color=color)
if toggle != None: if toggle != None:

View File

@ -37,6 +37,9 @@
<div style="width: 7rem; margin: 1rem">{{ form.deviceName.label }}</div> <div style="width: 7rem; margin: 1rem">{{ form.deviceName.label }}</div>
<div style="width: 12rem; margin: 1rem">{{ form.deviceName(size=24) }}</div> <div style="width: 12rem; margin: 1rem">{{ form.deviceName(size=24) }}</div>
<div style="with: 7rem; margin: 1rem">{{ form.submit() }}</div> <div style="with: 7rem; margin: 1rem">{{ form.submit() }}</div>
{% for error in form.deviceName.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</div> </div>
</form> </form>