calendarwatch_frontend/database/models.py

71 lines
2.5 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):
2020-05-27 20:06:43 +02:00
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))
2020-05-27 20:06:43 +02:00
google_token = db.relationship('GoogleToken', uselist=False, backref = 'user')
calendars = db.relationship('Calendar', backref='user', lazy=True)
devices = db.relationship('Device', backref='user')
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)
2020-05-27 20:06:43 +02:00
def updateCalendar(self, calendar_id, toggle=None, color=None):
2020-05-27 20:06:43 +02:00
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()
2020-05-27 20:06:43 +02:00
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):
2020-05-27 20:06:43 +02:00
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)
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)
name = db.Column(db.String(256), index=True)
toggle = db.Column(db.String(8))
color = db.Column(db.String(16))