Compare commits
10 Commits
dev
...
c3dcccb479
Author | SHA1 | Date | |
---|---|---|---|
c3dcccb479 | |||
5ec5ba488d | |||
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'))
|
||||
deviceName = db.Column(db.String(64), unique=True)
|
||||
connection = db.Column(db.Boolean)
|
||||
lastConnection = db.Column(db.BigInteger)
|
||||
|
||||
class Calendar(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
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_type = db.Column(db.String(32), index=True)
|
||||
name = db.Column(db.String(256), index=True)
|
||||
toggle = db.Column(db.String(8))
|
||||
color = db.Column(db.String(16))
|
||||
|
@ -2,9 +2,10 @@ 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 RandomWords
|
||||
RUN pip3 install uwsgi email-validator RandomWords ics
|
||||
RUN pip3 install google google-oauth google-auth-oauthlib google-api-python-client mysqlclient
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
EXPOSE 8084
|
||||
EXPOSE 3001
|
||||
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()
|
||||
if device is None:
|
||||
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):
|
||||
|
||||
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,
|
||||
user.google_token.refresh_token)
|
||||
credentials = google.oauth2.credentials.Credentials(**client_token)
|
||||
|
||||
service = build(GC.API_SERVICE_NAME, GC.API_VERSION, credentials=credentials)
|
||||
service = build(GC.API_SERVICE_NAME, GC.API_VERSION, credentials=credentials)
|
||||
|
||||
all_events = []
|
||||
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,
|
||||
timeMin=startDate,
|
||||
timeMax=endDate,
|
||||
timeMin=startDate.isoformat(),
|
||||
timeMax=endDate.isoformat(),
|
||||
maxResults=10,
|
||||
singleEvents=True,
|
||||
orderBy='startTime').execute()
|
||||
@ -156,7 +160,10 @@ def fetchCalendarEvents(user, calendars, startDate, endDate):
|
||||
|
||||
all_events.append(newEvent)
|
||||
|
||||
colors = service.colors().get().execute()
|
||||
if service != None:
|
||||
colors = service.colors().get().execute()
|
||||
else:
|
||||
colors = None
|
||||
|
||||
return all_events, colors
|
||||
|
||||
@ -178,6 +185,7 @@ def fetchCalendars():
|
||||
calendar_list = service.calendarList().list(pageToken=page_token).execute()
|
||||
for calendar in calendar_list['items']:
|
||||
calendars.append(Calendar(name=calendar['summary'],
|
||||
calType="Google",
|
||||
calendarId=calendar['id'],
|
||||
color=calendar['colorId']))
|
||||
page_token = calendar_list.get('nextPageToken')
|
||||
|
@ -1,6 +1,7 @@
|
||||
# Python standard libraries
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
# Third-party libraries
|
||||
import flask
|
||||
@ -16,10 +17,11 @@ from flask_login import (
|
||||
from random_words import RandomWords
|
||||
import requests
|
||||
|
||||
import backend.icalHandler as ical
|
||||
import server.googleHandler as google
|
||||
|
||||
from server import login_manager, app, db
|
||||
from server.forms import LoginForm, RegistrationForm, DeviceForm
|
||||
from server.forms import LoginForm, RegistrationForm, DeviceForm, CalendarForm
|
||||
import backend
|
||||
from database.models import User, Calendar, Device, GoogleToken
|
||||
|
||||
@ -93,11 +95,58 @@ def devices():
|
||||
form=form)
|
||||
|
||||
|
||||
@app.route("/calendar")
|
||||
@login_required
|
||||
@app.route("/calendar", methods=['GET', 'POST', 'DELETE'])
|
||||
def calendar():
|
||||
if not current_user.is_authenticated:
|
||||
return flask.render_template('login.html')
|
||||
|
||||
calendars = backend.calendarsFromDb(current_user)
|
||||
return flask.render_template('calendar.html', calendars=calendars)
|
||||
|
||||
form = CalendarForm()
|
||||
if request.method == 'POST':
|
||||
|
||||
if request.form.get("submit") == "Remove":
|
||||
calendar = db.session.query(Calendar).filter(Calendar.calendar_id==request.form.get("calendar")).first()
|
||||
db.session.delete(calendar)
|
||||
db.session.commit()
|
||||
return flask.redirect(url_for('calendar'))
|
||||
|
||||
elif form.validate_on_submit():
|
||||
ical.icalToCalendarDb(form.iCalURL.data, form.calName.data, current_user)
|
||||
return flask.redirect(url_for('calendar'))
|
||||
|
||||
|
||||
# otherwise it is a javascript POST
|
||||
else:
|
||||
try:
|
||||
calId = request.json.get('calendar_id')
|
||||
color = request.json.get('color', None)
|
||||
toggle = request.json.get('toggle', None)
|
||||
except:
|
||||
return flask.render_template('calendar.html', calendars=calendars, form=form)
|
||||
|
||||
if color != None:
|
||||
current_user.updateCalendar(calId, color=color)
|
||||
if toggle != None:
|
||||
current_user.updateCalendar(calId, toggle=toggle)
|
||||
# toggle specific calendar of user
|
||||
try:
|
||||
calId = request.json.get('calendar_id')
|
||||
color = request.json.get('color', None)
|
||||
toggle = request.json.get('toggle', None)
|
||||
except:
|
||||
return flask.render_template('calendar.html', calendars=calendars, form=form)
|
||||
|
||||
if color != None:
|
||||
current_user.updateCalendar(calId, color=color)
|
||||
if toggle != None:
|
||||
current_user.updateCalendar(calId, toggle=toggle)
|
||||
# toggle specific calendar of user
|
||||
|
||||
|
||||
return flask.render_template('calendar.html', calendars=calendars, form=form)
|
||||
|
||||
# POST
|
||||
|
||||
@app.route('/login/email', methods=['GET', 'POST'])
|
||||
def emaillogin():
|
||||
@ -204,6 +253,7 @@ def downloader(device):
|
||||
if request_device.user_id == None:
|
||||
return jsonify(kind="unregistered")
|
||||
|
||||
request_device.lastConnection=int(round(time.time()))
|
||||
request_device.connection=True
|
||||
db.session.commit()
|
||||
request_user = db.session.query(User).filter(User.id==request_device.user_id).first()
|
||||
@ -213,6 +263,7 @@ def downloader(device):
|
||||
# TODO only pass along google calendars form user
|
||||
startDate, endDate = backend.getTimeStamps()
|
||||
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)
|
||||
return jsonify(calendarjson)
|
||||
|
||||
@ -233,6 +284,7 @@ def generateDeviceFingerprint():
|
||||
if not db.session.query(Device).filter(Device.deviceName==fingerprint).first():
|
||||
# Save as new Device
|
||||
device = Device(deviceName=fingerprint, connection=False)
|
||||
device.lastConnection = int(round(time.time()))
|
||||
db.session.add(device)
|
||||
db.session.commit()
|
||||
break;
|
||||
@ -240,27 +292,3 @@ def generateDeviceFingerprint():
|
||||
# Send to Device
|
||||
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'
|
||||
|
@ -9,6 +9,13 @@
|
||||
|
||||
{% for item in calendars %}
|
||||
<div class="container">
|
||||
<!--action button-->
|
||||
<div style="width: 4rem; margin: 1rem;">
|
||||
<form action="" method="post">
|
||||
<input type="hidden" name="calendar" value={{ item.calendarId }}>
|
||||
<input type="submit" name="submit" value="Remove">
|
||||
</form>
|
||||
</div>
|
||||
<!--Name-->
|
||||
<div style="width: 15rem; margin: 1rem;">{{ item.name }}</div>
|
||||
|
||||
@ -32,12 +39,23 @@
|
||||
</div>
|
||||
{% endfor %}
|
||||
<div id=calendars class="container">
|
||||
<a class="button" href="login/google">Google Calendar</a>
|
||||
<a class="button" href="#" >Nextcloud Calendar</a>
|
||||
</div>
|
||||
<div class="container">
|
||||
<a class="button addcalendar" href="/login/google" style="width: auto; margin: 4rem">Add Calendar</a>
|
||||
<form action="" method="post">
|
||||
<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>
|
||||
</form>
|
||||
<!--div class="container">
|
||||
<a class="button addcalendar" href="/login/google" style="width: auto; margin: 4rem">Add Calendar</a>
|
||||
</div-->
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
|
Reference in New Issue
Block a user