class Ticket(db.Model):
    __tablename__ = 'tickets'

    id = db.Column(db.String(40), primary_key=True, default=generate_uuid)
    created_at = db.Column(db.DateTime, nullable=False)
    time = db.Column(db.DateTime, nullable=False)

    availability_id = db.Column(db.String,
                                db.ForeignKey('availabilities.id'),
                                nullable=True)

    user_id = db.Column(db.String, db.ForeignKey('users.id'), nullable=True)
    user = db.relationship("User", back_populates="ticket")

    def __init__(self, time):
        self.time = time
        self.created_at = datetime.now()

    @classmethod
    def find_by_date(cls, date):
        return cls.query.filter_by(time=date)

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

    def delete(self):
        db.session.delete(self)
        db.session.commit()
Exemple #2
0
class Meal(db.Model):
    __tablename__ = 'meals'

    id = db.Column(db.String(40), primary_key=True, default=generate_uuid)
    created_at = db.Column(db.DateTime, nullable=False)
    picture = db.Column(db.LargeBinary, nullable=False)
    title = db.Column(db.String(25), nullable=False)
    description = db.Column(db.String(250), nullable=False)
    date = db.Column(db.Date, nullable=False)

    availabilities = db.relationship('Availability',
                                     backref='meals',
                                     lazy=True)

    def __init__(self, title, description, picture):
        self.title = title
        self.description = description
        self.picture = picture
        self.created_at = datetime.now()

    @classmethod
    def get_by_date(cls, date):
        return cls.query.filter_by(date=date)

    @classmethod
    def get(cls, id):
        return cls.query.get(id)

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

    def delete(self):
        db.session.delete(self)
        db.session.commit()
class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.String(40), primary_key=True, default=generate_uuid)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(
        PasswordType(
            # The returned dictionary is forwarded to the CryptContext
            onload=lambda **kwargs: dict(schemes=flask.current_app.config[
                'PASSWORD_SCHEMES'],
                                         **kwargs), ),
        unique=False,
        nullable=False)
    created_at = db.Column(db.DateTime, nullable=False)

    # ticket_id = db.Column(db.String, db.ForeignKey('tickets.id'), nullable=True)

    ticket = db.relationship("Ticket", uselist=False, back_populates="user")

    ticket_histories = db.relationship('TicketHistory',
                                       backref='user',
                                       lazy=True)

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.created_at = datetime.utcnow()

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

    @classmethod
    def get_all(cls):
        return cls.query.get().all()

    @classmethod
    def find_by_username(cls, username):
        return cls.query.filter_by(username=username).first()

    @classmethod
    def find_by_id(cls, _id):
        return cls.query.filter_by(id=_id).first()
class Availability(db.Model):
    __tablename__ = 'availabilities'

    id = db.Column(db.String(40), primary_key=True, default=generate_uuid)
    created_at = db.Column(db.DateTime, nullable=False)
    time = db.Column(db.DateTime, nullable=False)

    meal_id = db.Column(db.String, db.ForeignKey('meals.id'), nullable=False)

    tickets = db.relationship('Ticket', backref='tickets', lazy=True)

    def __init__(self):
        self.created_at = datetime.now()

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

    def delete(self):
        db.session.delete(self)
        db.session.commit()