2020-04-24 19:54:56 +02:00
|
|
|
import json
|
2020-04-22 22:15:21 +02:00
|
|
|
from flask_login import UserMixin
|
|
|
|
from server import login_manager, db
|
2020-04-23 19:11:23 +02:00
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
2020-04-22 22:15:21 +02:00
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(id):
|
|
|
|
return User.query.get(id)
|
|
|
|
|
|
|
|
class User(UserMixin, db.Model):
|
2020-04-23 19:11:23 +02:00
|
|
|
id = db.Column(db.String(64), primary_key=True)
|
2020-04-22 22:15:21 +02:00
|
|
|
username = db.Column(db.String(64), index=True, unique=True)
|
|
|
|
email = db.Column(db.String(120), index=True, unique=True)
|
|
|
|
profile_pic = db.Column(db.String(256))
|
|
|
|
password_hash = db.Column(db.String(128))
|
2020-04-24 19:54:56 +02:00
|
|
|
calendarJson = db.Column(db.String)
|
|
|
|
google_credentials = db.Column(db.String)
|
2020-04-22 22:15:21 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2020-04-23 19:11:23 +02:00
|
|
|
return '<User {}>'.format(self.username)
|
|
|
|
|
|
|
|
def setPassword(self, password):
|
|
|
|
self.password_hash = generate_password_hash(password)
|
|
|
|
|
|
|
|
def checkPassword(self, password):
|
|
|
|
return check_password_hash(self.password_hash, password)
|
2020-04-22 22:15:21 +02:00
|
|
|
|
2020-04-24 19:54:56 +02:00
|
|
|
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:
|
2020-05-17 22:59:12 +02:00
|
|
|
print("no credentials", flush=True)
|
2020-04-24 19:54:56 +02:00
|
|
|
return None
|
|
|
|
return json.loads(self.google_credentials)
|
|
|
|
|
2020-04-22 22:15:21 +02:00
|
|
|
class Calendar(db.Model):
|
|
|
|
usr_id = db.Column(db.String(21), index=True)
|
|
|
|
calendar_id = db.Column(db.String(256), primary_key=True)
|
|
|
|
name = db.Column(db.String(256), index=True)
|
|
|
|
toggle = db.Column(db.String(8))
|
|
|
|
color = db.Column(db.String(16))
|
|
|
|
|
|
|
|
def getCalendars(self, user_id):
|
|
|
|
calendars = self.query.filter(Calendar.usr_id==user_id)
|
|
|
|
|
|
|
|
return calendars
|
|
|
|
|
|
|
|
def getCalendar(self, user_id, calendar_id):
|
|
|
|
calendars = self.query.filter(self.usr_id==user_id, self.calendar_id==calendar_id)
|
|
|
|
|
|
|
|
calendar = None
|
|
|
|
for c in calendars:
|
|
|
|
calendar = c
|
|
|
|
|
|
|
|
if not calendar:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return calendar
|
|
|
|
|
|
|
|
@staticmethod
|
2020-05-18 23:52:18 +02:00
|
|
|
def updateCalendar(user_id, calendar_id, toggle=None, color=None):
|
2020-04-22 22:15:21 +02:00
|
|
|
|
2020-05-18 23:52:18 +02:00
|
|
|
calendar = Calendar.query.filter(Calendar.usr_id==user_id, Calendar.calendar_id==calendar_id).first()
|
2020-04-22 22:15:21 +02:00
|
|
|
|
|
|
|
|
2020-05-18 23:52:18 +02:00
|
|
|
print("updating", flush=True)
|
2020-04-22 22:15:21 +02:00
|
|
|
if(toggle != None):
|
|
|
|
print(toggle)
|
|
|
|
calendar.toggle = toggle
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
if(color != None):
|
|
|
|
calendar.color = color
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def create(self, user_id, calendar_id, name, color, toggle = 'True'):
|
|
|
|
newcal = Calendar(usr_id=user_id, calendar_id=calendar_id, name=name, toggle=toggle, color=color)
|
|
|
|
|
|
|
|
db.session.add(newcal)
|
2020-05-17 22:59:12 +02:00
|
|
|
db.session.commit()
|
2020-05-21 18:33:58 +02:00
|
|
|
|
|
|
|
class Device(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
deviceId = db.Column(db.String(128), index=True)
|