Compare commits
8 Commits
dev
...
9ecaf0211f
Author | SHA1 | Date | |
---|---|---|---|
9ecaf0211f | |||
1f48689a32 | |||
752c7c5577 | |||
04c5410c41 | |||
524d2f1e1d | |||
2add28fa00 | |||
38e16f92e8 | |||
0284eb2fa8 |
2
backend
2
backend
Submodule backend updated: 45cd71cc4b...f939127a0c
14
database/__init__.py
Normal file
14
database/__init__.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from server import db
|
||||||
|
import time
|
||||||
|
from database.models import Device
|
||||||
|
|
||||||
|
def cleanDevices():
|
||||||
|
allDevs = db.session.query(Device)
|
||||||
|
devices = allDevs.filter(Device.lastConnection <= int(round(time.time())) - 60*60*24*30).all()
|
||||||
|
devices += allDevs.filter(Device.lastConnection == None)
|
||||||
|
for device in devices:
|
||||||
|
print(device.deviceName)
|
||||||
|
db.session.delete(device)
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
|
|
30
database/migrations/versions/9882522aafa9_.py
Normal file
30
database/migrations/versions/9882522aafa9_.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 9882522aafa9
|
||||||
|
Revises: e5ef5e4a807b
|
||||||
|
Create Date: 2020-07-25 09:34:07.987380
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '9882522aafa9'
|
||||||
|
down_revision = 'e5ef5e4a807b'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('calendar', sa.Column('calendar_type', sa.String(length=32), nullable=True))
|
||||||
|
op.create_index(op.f('ix_calendar_calendar_type'), 'calendar', ['calendar_type'], unique=False)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_index(op.f('ix_calendar_calendar_type'), table_name='calendar')
|
||||||
|
op.drop_column('calendar', 'calendar_type')
|
||||||
|
# ### end Alembic commands ###
|
@ -61,11 +61,13 @@ class Device(db.Model):
|
|||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||||
deviceName = db.Column(db.String(64), unique=True)
|
deviceName = db.Column(db.String(64), unique=True)
|
||||||
connection = db.Column(db.Boolean)
|
connection = db.Column(db.Boolean)
|
||||||
|
lastConnection = db.Column(db.BigInteger)
|
||||||
|
|
||||||
class Calendar(db.Model):
|
class Calendar(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True, nullable=False)
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True, nullable=False)
|
||||||
calendar_id = db.Column(db.String(256), primary_key=True)
|
calendar_id = db.Column(db.String(256), primary_key=True)
|
||||||
|
calendar_type = db.Column(db.String(32), index=True)
|
||||||
name = db.Column(db.String(256), index=True)
|
name = db.Column(db.String(256), index=True)
|
||||||
toggle = db.Column(db.String(8))
|
toggle = db.Column(db.String(8))
|
||||||
color = db.Column(db.String(16))
|
color = db.Column(db.String(16))
|
||||||
|
@ -2,9 +2,10 @@ 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 email-validator RandomWords
|
RUN pip3 install uwsgi email-validator RandomWords ics
|
||||||
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
|
||||||
EXPOSE 3001
|
EXPOSE 3001
|
||||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||||
|
# CMD tail -f /dev/null
|
||||||
|
@ -46,3 +46,13 @@ class DeviceForm(FlaskForm):
|
|||||||
device = Device.query.filter_by(deviceName=deviceName.data).first()
|
device = Device.query.filter_by(deviceName=deviceName.data).first()
|
||||||
if device is None:
|
if device is None:
|
||||||
raise ValidationError('Device not Found')
|
raise ValidationError('Device not Found')
|
||||||
|
|
||||||
|
|
||||||
|
class CalendarForm(FlaskForm):
|
||||||
|
iCalURL = StringField('New ical URL', validators=[DataRequired()])
|
||||||
|
calName = StringField('Calendar Name', validators=[DataRequired()])
|
||||||
|
submit = SubmitField('Add URL')
|
||||||
|
|
||||||
|
def validate_iCalURL (self, iCalURL):
|
||||||
|
return None
|
||||||
|
|
||||||
|
@ -121,19 +121,23 @@ def deleteAccount(user):
|
|||||||
|
|
||||||
|
|
||||||
def fetchCalendarEvents(user, calendars, startDate, endDate):
|
def fetchCalendarEvents(user, calendars, startDate, endDate):
|
||||||
|
|
||||||
|
service = None
|
||||||
|
if user.google_token is not None:
|
||||||
|
client_token = GC.build_credentials(user.google_token.token,
|
||||||
|
user.google_token.refresh_token)
|
||||||
|
credentials = google.oauth2.credentials.Credentials(**client_token)
|
||||||
|
|
||||||
client_token = GC.build_credentials(user.google_token.token,
|
service = build(GC.API_SERVICE_NAME, GC.API_VERSION, credentials=credentials)
|
||||||
user.google_token.refresh_token)
|
|
||||||
credentials = google.oauth2.credentials.Credentials(**client_token)
|
|
||||||
|
|
||||||
service = build(GC.API_SERVICE_NAME, GC.API_VERSION, credentials=credentials)
|
|
||||||
|
|
||||||
all_events = []
|
all_events = []
|
||||||
for calendar in calendars:
|
for calendar in calendars:
|
||||||
if calendar.toggle == "True":
|
if (calendar.toggle == "True" and
|
||||||
|
calendar.calendar_type == "Google" and
|
||||||
|
service != None):
|
||||||
event_result = service.events().list(calendarId=calendar.calendar_id,
|
event_result = service.events().list(calendarId=calendar.calendar_id,
|
||||||
timeMin=startDate,
|
timeMin=startDate.isoformat(),
|
||||||
timeMax=endDate,
|
timeMax=endDate.isoformat(),
|
||||||
maxResults=10,
|
maxResults=10,
|
||||||
singleEvents=True,
|
singleEvents=True,
|
||||||
orderBy='startTime').execute()
|
orderBy='startTime').execute()
|
||||||
@ -156,7 +160,10 @@ def fetchCalendarEvents(user, calendars, startDate, endDate):
|
|||||||
|
|
||||||
all_events.append(newEvent)
|
all_events.append(newEvent)
|
||||||
|
|
||||||
colors = service.colors().get().execute()
|
if service != None:
|
||||||
|
colors = service.colors().get().execute()
|
||||||
|
else:
|
||||||
|
colors = None
|
||||||
|
|
||||||
return all_events, colors
|
return all_events, colors
|
||||||
|
|
||||||
@ -178,6 +185,7 @@ def fetchCalendars():
|
|||||||
calendar_list = service.calendarList().list(pageToken=page_token).execute()
|
calendar_list = service.calendarList().list(pageToken=page_token).execute()
|
||||||
for calendar in calendar_list['items']:
|
for calendar in calendar_list['items']:
|
||||||
calendars.append(Calendar(name=calendar['summary'],
|
calendars.append(Calendar(name=calendar['summary'],
|
||||||
|
calType="Google",
|
||||||
calendarId=calendar['id'],
|
calendarId=calendar['id'],
|
||||||
color=calendar['colorId']))
|
color=calendar['colorId']))
|
||||||
page_token = calendar_list.get('nextPageToken')
|
page_token = calendar_list.get('nextPageToken')
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# Python standard libraries
|
# Python standard libraries
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
# Third-party libraries
|
# Third-party libraries
|
||||||
import flask
|
import flask
|
||||||
@ -16,10 +17,11 @@ from flask_login import (
|
|||||||
from random_words import RandomWords
|
from random_words import RandomWords
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
import backend.icalHandler as ical
|
||||||
import server.googleHandler as google
|
import server.googleHandler as google
|
||||||
|
|
||||||
from server import login_manager, app, db
|
from server import login_manager, app, db
|
||||||
from server.forms import LoginForm, RegistrationForm, DeviceForm
|
from server.forms import LoginForm, RegistrationForm, DeviceForm, CalendarForm
|
||||||
import backend
|
import backend
|
||||||
from database.models import User, Calendar, Device, GoogleToken
|
from database.models import User, Calendar, Device, GoogleToken
|
||||||
|
|
||||||
@ -93,11 +95,54 @@ def devices():
|
|||||||
form=form)
|
form=form)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/calendar")
|
@app.route("/calendar", methods=['GET', 'POST', 'DELETE'])
|
||||||
@login_required
|
@login_required
|
||||||
def calendar():
|
def calendar():
|
||||||
calendars = backend.calendarsFromDb(current_user)
|
calendars = backend.calendarsFromDb(current_user)
|
||||||
return flask.render_template('calendar.html', calendars=calendars)
|
|
||||||
|
form = CalendarForm()
|
||||||
|
if request.method == 'POST':
|
||||||
|
|
||||||
|
if form.validate_on_submit():
|
||||||
|
# try to add the submitted ical as a calendar
|
||||||
|
print(form.iCalURL.data, flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
ical.icalToCalendarDb(form.iCalURL.data, form.calName.data, current_user)
|
||||||
|
|
||||||
|
# otherwise it is a javascript POST
|
||||||
|
else:
|
||||||
|
calId = request.json.get('calendar_id')
|
||||||
|
color = request.json.get('color', None)
|
||||||
|
toggle = request.json.get('toggle', None)
|
||||||
|
|
||||||
|
if color != None:
|
||||||
|
current_user.updateCalendar(calId, color=color)
|
||||||
|
if toggle != None:
|
||||||
|
current_user.updateCalendar(calId, toggle=toggle)
|
||||||
|
# toggle specific calendar of user
|
||||||
|
calId = request.json.get('calendar_id')
|
||||||
|
color = request.json.get('color', None)
|
||||||
|
toggle = request.json.get('toggle', None)
|
||||||
|
|
||||||
|
if color != None:
|
||||||
|
current_user.updateCalendar(calId, color=color)
|
||||||
|
if toggle != None:
|
||||||
|
current_user.updateCalendar(calId, toggle=toggle)
|
||||||
|
# toggle specific calendar of user
|
||||||
|
|
||||||
|
return 'OK'
|
||||||
|
|
||||||
|
elif request.method == 'DELETE':
|
||||||
|
# do nothing
|
||||||
|
return 'NONE'
|
||||||
|
else:
|
||||||
|
# POST Error 405
|
||||||
|
print("405")
|
||||||
|
|
||||||
|
return flask.render_template('calendar.html', calendars=calendars, form=form)
|
||||||
|
|
||||||
|
# POST
|
||||||
|
|
||||||
@app.route('/login/email', methods=['GET', 'POST'])
|
@app.route('/login/email', methods=['GET', 'POST'])
|
||||||
def emaillogin():
|
def emaillogin():
|
||||||
@ -204,6 +249,7 @@ def downloader(device):
|
|||||||
if request_device.user_id == None:
|
if request_device.user_id == None:
|
||||||
return jsonify(kind="unregistered")
|
return jsonify(kind="unregistered")
|
||||||
|
|
||||||
|
request_device.lastConnection=int(round(time.time()))
|
||||||
request_device.connection=True
|
request_device.connection=True
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
request_user = db.session.query(User).filter(User.id==request_device.user_id).first()
|
request_user = db.session.query(User).filter(User.id==request_device.user_id).first()
|
||||||
@ -213,6 +259,7 @@ def downloader(device):
|
|||||||
# TODO only pass along google calendars form user
|
# TODO only pass along google calendars form user
|
||||||
startDate, endDate = backend.getTimeStamps()
|
startDate, endDate = backend.getTimeStamps()
|
||||||
events, colors = google.fetchCalendarEvents(request_user, request_user.calendars, startDate, endDate)
|
events, colors = google.fetchCalendarEvents(request_user, request_user.calendars, startDate, endDate)
|
||||||
|
events.extend(ical.fetchCalendarEvents(request_user.calendars, startDate, endDate))
|
||||||
calendarjson = backend.generateJsonFromCalendarEntries(events, colors)
|
calendarjson = backend.generateJsonFromCalendarEntries(events, colors)
|
||||||
return jsonify(calendarjson)
|
return jsonify(calendarjson)
|
||||||
|
|
||||||
@ -233,6 +280,7 @@ def generateDeviceFingerprint():
|
|||||||
if not db.session.query(Device).filter(Device.deviceName==fingerprint).first():
|
if not db.session.query(Device).filter(Device.deviceName==fingerprint).first():
|
||||||
# Save as new Device
|
# Save as new Device
|
||||||
device = Device(deviceName=fingerprint, connection=False)
|
device = Device(deviceName=fingerprint, connection=False)
|
||||||
|
device.lastConnection = int(round(time.time()))
|
||||||
db.session.add(device)
|
db.session.add(device)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
break;
|
break;
|
||||||
@ -240,27 +288,3 @@ def generateDeviceFingerprint():
|
|||||||
# Send to Device
|
# Send to Device
|
||||||
return jsonify(deviceName=fingerprint)
|
return jsonify(deviceName=fingerprint)
|
||||||
|
|
||||||
# POST
|
|
||||||
|
|
||||||
@app.route('/calendar', methods = ['POST', 'DELETE'])
|
|
||||||
@login_required
|
|
||||||
def user():
|
|
||||||
if request.method == 'POST':
|
|
||||||
calId = request.json.get('calendar_id')
|
|
||||||
color = request.json.get('color', None)
|
|
||||||
toggle = request.json.get('toggle', None)
|
|
||||||
|
|
||||||
if color != None:
|
|
||||||
current_user.updateCalendar(calId, color=color)
|
|
||||||
if toggle != None:
|
|
||||||
current_user.updateCalendar(calId, toggle=toggle)
|
|
||||||
# toggle specific calendar of user
|
|
||||||
|
|
||||||
elif request.method == 'DELETE':
|
|
||||||
# do nothing
|
|
||||||
return 'NONE'
|
|
||||||
else:
|
|
||||||
# POST Error 405
|
|
||||||
print("405")
|
|
||||||
|
|
||||||
return 'OK'
|
|
||||||
|
@ -32,12 +32,23 @@
|
|||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<div id=calendars class="container">
|
<div id=calendars class="container">
|
||||||
<a class="button" href="login/google">Google Calendar</a>
|
|
||||||
<a class="button" href="#" >Nextcloud Calendar</a>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<form action="" method="post">
|
||||||
<a class="button addcalendar" href="/login/google" style="width: auto; margin: 4rem">Add Calendar</a>
|
<div class="container grey" style="margin-top: 3rem;">
|
||||||
|
<div>{{ form.hidden_tag() }}</div>
|
||||||
|
<div style="margin: 1rem">{{ form.calName.label }}</div>
|
||||||
|
<div style="margin: 1rem">{{ form.calName(size=24) }}</div>
|
||||||
|
<div style="margin: 1rem">{{ form.iCalURL.label }}</div>
|
||||||
|
<div style="margin: 1rem">{{ form.iCalURL(size=24) }}</div>
|
||||||
|
<div style="with: 8rem; margin: 1rem">{{ form.submit() }}</div>
|
||||||
|
{% for error in form.iCalURL.errors %}
|
||||||
|
<span style="color: red;">[{{ error }}]</span>
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
</form>
|
||||||
|
<!--div class="container">
|
||||||
|
<a class="button addcalendar" href="/login/google" style="width: auto; margin: 4rem">Add Calendar</a>
|
||||||
|
</div-->
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user