From 1066c776cdd745f726c5f46cd4bc12a278225e6e Mon Sep 17 00:00:00 2001 From: Dark-Storm Date: Sat, 29 Sep 2018 23:51:45 +0200 Subject: [PATCH] WIP on models --- app/models.py | 2 +- app/models/user.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 app/models/user.py diff --git a/app/models.py b/app/models.py index 5635627..d290199 100644 --- a/app/models.py +++ b/app/models.py @@ -29,7 +29,7 @@ def load_user(id): class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) - timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) + timestamp = db.Column(db.DateTime, index=True, default=datetime.now) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): diff --git a/app/models/user.py b/app/models/user.py new file mode 100644 index 0000000..b3a2494 --- /dev/null +++ b/app/models/user.py @@ -0,0 +1,94 @@ +from datetime import date, datetime +from app import db, login +from flask_login import UserMixin +from werkzeug.security import generate_password_hash, check_password_hash + +class User(UserMixin, db.Model): + id = db.Column(db.Integer, primary_key=True) + contents = db.relationship('Content') # TODO: add good relation + +class Member(User, db.Model): + # Standalone properties + username = db.Column(db.Unicode(64), index=True, unique=True) + email = db.Column(db.String(120), index=True, unique=True) + password_hash = db.Column(db.String(128)) + contents = db.relationship('Content', backref='author', lazy='dynamic') + xp_points = db.Column(db.Integer) + innovation_points = db.Column(db.Integer) + biography = db.column(db.Text, convert_unicode=True) + signature = db.column(db.Text, convert_unicode=True) + birthday = db.Column(db.Date) + register_date = db.Column(db.Date, default=date.today) + # Relations + trophies = db.relationship('Trophy') # TODO: add good relation + tests = db.relationship('Test') # TODO: add good relation + # Privacy assets + receive_newsletter = db.column(db.Boolean, default=False) + + def __init__(self, username, email, password): + self.username = username + self.email = email + self.set_password(password) + + def update(self, username=None, email=None, password=None, + biography=None, signature=None, birthday=None, + receive_newsletter=None): + # TODO: verify good type of those args, think about the password mgt + if username != None: + self.username = username + if email != None: + self.email = email + if password != None: + self.set_password(password) + if biography != None: + self.biography = biography + if signature != None: + self.signature = signature + if birthday != None: + self.birthday = birthday + if receive_newsletter != None: + self.receive_newsletter = receive_newsletter + + def get_public_data(self, username=False, xp_points=False, + innovation_points=False, biography=None, signature=None, + birthday=None, register_date=False): + out = {} + if username: + out['username'] = username + if xp_points: + out['xp_points'] = xp_points + if innovation_points: + out['innovation_points'] = innovation_points + if biography: + out['biography'] = biography + if signature: + out['signature'] = signature + if birthday: + out['birthday'] = birthday + if register_date: + out['register_date'] = register_date + + def add_xp(self, n): + self.xp_points += n + + def add_innovation(self, n): + self.innovation_points += n + + def set_password(self, password): + self.password_hash = generate_password_hash(password) + + def check_password(self, password): + return check_password_hash(self.password_hash, password) + + def __repr__(self): + return ''.format(self.username) + +class Guest(User, db.Model): + username = db.Column(db.Unicode(64), index=True) + ip = db.column(db.String(47)) # Max IPv6 adress length + last_post = db.Column(db.DateTime, default=datetime.now) + +# Can be implemented if needed +# class Organization(User, db.Model): +# username = db.Column(db.Unicode(64), index=True) +# members = db.relationship('Member') \ No newline at end of file