WIP on models

This commit is contained in:
Dark-Storm 2018-09-29 23:51:45 +02:00
parent d677dc7b81
commit 1066c776cd
Signed by: Darks
GPG Key ID: F61F10FA138E797C
2 changed files with 95 additions and 1 deletions

View File

@ -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):

94
app/models/user.py Normal file
View File

@ -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 '<User {}>'.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')