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-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)
|
2020-04-22 22:15:21 +02:00
|
|
|
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:56:23 +02:00
|
|
|
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")
|
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-05-27 20:06:43 +02:00
|
|
|
def updateCalendar(self, calendar_id, toggle=None, color=None):
|
2020-04-22 22:15:21 +02:00
|
|
|
|
2020-05-27 20:06:43 +02:00
|
|
|
for calendar in self.calendars:
|
|
|
|
if calendar.calendar_id == calendar_id:
|
|
|
|
break
|
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()
|
|
|
|
|
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))
|
|
|
|
|
2020-05-21 18:33:58 +02:00
|
|
|
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))
|