calendarwatch_frontend/database/models.py

74 lines
2.7 KiB
Python

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.Integer, primary_key=True, autoincrement=True)
userid = db.Column(db.String(64), index=True, unique=True)
username = db.Column(db.String(64), index=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))
google_token = db.relationship('GoogleToken', uselist=False, backref = 'user', cascade="all, delete-orphan")
calendars = db.relationship('Calendar', backref='user', lazy=True, cascade="all, delete-orphan")
devices = db.relationship('Device', backref='user', cascade="all, delete-orphan")
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 updateCalendar(self, calendar_id, toggle=None, color=None):
for calendar in self.calendars:
if calendar.calendar_id == calendar_id:
break
print("updating", flush=True)
if(toggle != None):
print(toggle)
calendar.toggle = toggle
db.session.commit()
if(color != None):
calendar.color = color
db.session.commit()
def hasCalendar(self, calendar_id):
for calendar in self.calendars:
if calendar.calendar_id == calendar_id:
return True
return False
class GoogleToken(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
token = db.Column(db.String(256))
refresh_token = db.Column(db.String(256))
class Device(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
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))