adds backend script which can be run as a cronjob every n minutes to generate new json files for google calendars

- database updated to save google credentials
- database updated to save json calendar information
- json still saved as a json file under userinfo/<user.id>/calendarevents.json
This commit is contained in:
Raphael Maenle 2020-04-24 17:54:56 +00:00
parent a071193959
commit c89ecd7134
8 changed files with 84 additions and 7 deletions

BIN
app.db

Binary file not shown.

3
app.py
View File

@ -1,5 +1,8 @@
# Configuration # Configuration
from server import app from server import app
from backend import routine
routine.start()
if __name__ == "__main__": if __name__ == "__main__":
context = ('certificate/xip.io.crt', 'certificate/xip.io.key')#certificate and key files context = ('certificate/xip.io.crt', 'certificate/xip.io.key')#certificate and key files

View File

@ -0,0 +1,28 @@
"""empty message
Revision ID: 66f62f457a22
Revises: 7bbc2215d87d
Create Date: 2020-04-24 17:12:45.275636
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '66f62f457a22'
down_revision = '7bbc2215d87d'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('google_credentials', sa.String(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'google_credentials')
# ### end Alembic commands ###

View File

@ -0,0 +1,28 @@
"""empty message
Revision ID: 7bbc2215d87d
Revises: 92db2e496087
Create Date: 2020-04-24 11:36:43.600038
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '7bbc2215d87d'
down_revision = '92db2e496087'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('calendarJson', sa.String(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'calendarJson')
# ### end Alembic commands ###

View File

@ -1,4 +1,3 @@
import os import os
import sqlite3 import sqlite3

View File

@ -86,7 +86,7 @@ def verifyResponse():
flask.session['credentials'] = credentials_to_dict(credentials) flask.session['credentials'] = credentials_to_dict(credentials)
session = flow.authorized_session() session = flow.authorized_session()
return session return session, credentials_to_dict(credentials)
def get_google_provider_cfg(): def get_google_provider_cfg():
@ -103,7 +103,7 @@ def calendarsFromDb():
pyCalendars = [] pyCalendars = []
for calendar in calendars: for calendar in calendars:
name = calendar.name name = calendar.name
calId = calendar.calendar_id # calId = calendar.calendar_id
toggle = calendar.toggle toggle = calendar.toggle
color = calendar.color color = calendar.color

View File

@ -1,3 +1,4 @@
import json
from flask_login import UserMixin from flask_login import UserMixin
from server import login_manager, db from server import login_manager, db
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
@ -12,6 +13,8 @@ class User(UserMixin, db.Model):
email = db.Column(db.String(120), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True)
profile_pic = db.Column(db.String(256)) profile_pic = db.Column(db.String(256))
password_hash = db.Column(db.String(128)) password_hash = db.Column(db.String(128))
calendarJson = db.Column(db.String)
google_credentials = db.Column(db.String)
def __repr__(self): def __repr__(self):
return '<User {}>'.format(self.username) return '<User {}>'.format(self.username)
@ -22,6 +25,23 @@ class User(UserMixin, db.Model):
def checkPassword(self, password): def checkPassword(self, password):
return check_password_hash(self.password_hash, password) return check_password_hash(self.password_hash, password)
def setJson(self, jsonObject):
self.calendarJson = json.dumps(jsonObject)
db.session.commit()
def getJson(self):
return json.loads(self.calendarJson)
def setGoogleCredentials(self, credentials):
self.google_credentials = json.dumps(credentials)
db.session.commit()
def getGoogleCredentials(self):
if self.google_credentials is None:
return None
return json.loads(self.google_credentials)
class Calendar(db.Model): class Calendar(db.Model):
usr_id = db.Column(db.String(21), index=True) usr_id = db.Column(db.String(21), index=True)
calendar_id = db.Column(db.String(256), primary_key=True) calendar_id = db.Column(db.String(256), primary_key=True)

View File

@ -82,8 +82,7 @@ def googlelogin():
@app.route("/login/google/callback") @app.route("/login/google/callback")
def callback(): def callback():
session = google.verifyResponse() session, credentials = google.verifyResponse()
userinfo = session.get('https://www.googleapis.com/userinfo/v2/me').json() userinfo = session.get('https://www.googleapis.com/userinfo/v2/me').json()
# Create a user in your db with the information provided # Create a user in your db with the information provided
@ -107,7 +106,7 @@ def callback():
print("login:" + user.id) print("login:" + user.id)
login_user(user) login_user(user)
user.setGoogleCredentials(credentials)
return flask.redirect(flask.url_for('index')) return flask.redirect(flask.url_for('index'))
@app.route("/logout") @app.route("/logout")
@ -128,7 +127,7 @@ def credentials_to_dict(credentials):
@app.route("/userinfo/<path:user>/calendarevents.json") @app.route("/userinfo/<path:user>/calendarevents.json")
def downloader(user): def downloader(user):
print(user) print(user)
path = "/home/raphael/dev/website_ws/website/userinfo/" + user path = "userinfo/" + user
return flask.send_from_directory(path, "calendarevents.json") return flask.send_from_directory(path, "calendarevents.json")
# POST # POST