ciclos

ref: responsive

src/types/user.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from . import db
from flask_login import UserMixin
from datetime import datetime as dt
from .order import Order
from .cycle import Cycle, CycleStatus

class User(UserMixin, db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(80), unique = True, nullable = False)
    email = db.Column(db.String(220), unique = True, nullable = False)
    password = db.Column(db.String(96), nullable = False)
    first_name = db.Column(db.String(250), nullable = False)
    last_name = db.Column(db.String(250))
    created_at = db.Column(db.DateTime, default = dt.utcnow)
    updated_at = db.Column(db.DateTime, default = dt.utcnow)

    orders = db.relationship("Order", backref = "user", lazy = True)
    
    def __init__(self, email, password, username, first_name, last_name):
        self.email = email
        self.password = password
        self.username = username
        self.first_name = first_name
        self.last_name = last_name

    def create(self):
        db.session.add(self)
        db.session.commit()

    def is_admin(self):
        # TODO: Move this to a field on the database
        return self.email in ["pedrolucasporcellis@gmail.com"]

    def current_order(self):
        return Order.query.join(User).join(Cycle).filter(User.id == self.id, Cycle.status == CycleStatus.published).first()

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    def __repr__(self):
        return "User <id: {}:>".format(self.id)