calendarwatch_frontend/server/models.py

91 lines
2.8 KiB
Python
Raw Normal View History

import json
from flask_login import UserMixin
from server import login_manager, db
from werkzeug.security import generate_password_hash, check_password_hash
@login_manager.user_loader
def load_user(id):
return User.query.get(id)
class User(UserMixin, db.Model):
id = db.Column(db.String(64), primary_key=True)
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))
calendarJson = db.Column(db.String)
google_credentials = db.Column(db.String)
def __repr__(self):
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)
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:
print("no credentials", flush=True)
return None
return json.loads(self.google_credentials)
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
def updateCalendar(user_id, calendar_name, toggle=None, color=None):
calendar = Calendar.query.filter(Calendar.usr_id==user_id, Calendar.name==calendar_name).first()
print("updating")
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)
db.session.commit()