class Transaction(db.Model, BaseModel): id = db.Column( db.Integer, primary_key=True ) timestamp = db.Column( db.DateTime, default=datetime.utcnow ) value = db.Column( db.Float, nullable=False ) user_id = db.Column( db.Integer, db.ForeignKey('user.id') ) def to_dict(self): return dict( id=self.id, timestamp=self.timestamp.isoformat(), value=self.value, user_id=self.user_id ) @classmethod def find_transaction_from_user(cls, user_id): return cls.query.filter_by(user_id=user_id)
class User(db.Model): __table_name__ = 'user' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), nullable=False) create_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) update_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) def __init__(self, name, email): self.name = name self.email = email def to_dict(self): return { 'id': self.id, 'name': self.name, 'email': self.email, 'create_time': self.create_time, 'update_time': self.update_time }
class Post(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(30), nullable=False) content = db.Column(db.Text(), nullable=False) user_id = db.Column(db.Integer(), db.ForeignKey("user.id"), nullable=False) def __repr__(self): return f'Post({self.id} {self.title})'
class Playlist(db.Model): id = db.Column(db.Integer, primary_key=True) playlist = db.Column(db.String(), unique=True, nullable=False) def __repr__ (self): return f"Playlist: ({self.playlist})" def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) contact_id = db.Column(db.Integer, nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class User(db.Model): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(30), nullable=False) email = db.Column(db.String(50), nullable=False) is_admin = db.Column(db.Boolean, default=False) password = db.Column(db.String(60), nullable=False) posts = db.relationship("Post", backref="author", cascade="all, delete-orphan", lazy=True) def __repr__(self): return f"User({self.id} {self.username})"
class Client(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False) comment = db.Column(db.Text, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') stars = db.Column(db.Integer, nullable=False) tour_id = db.Column(db.Integer, db.ForeignKey('tour.id'), nullable=False) def __repr__(self): return f"Post ('{self.name}', '{self.stars}')" def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class User(db.Model, BaseModel): id = db.Column( db.Integer, primary_key=True ) email = db.Column( db.String(100), nullable=False ) password = db.Column( db.String(100), nullable=False ) doc_number = db.Column( db.String(100), nullable=False ) balance = db.Column( db.Float, default=0.0 ) is_active = db.Column( db.Boolean, default=True ) transactions = db.relationship( 'Transaction', backref='owner' ) __to_dict_fields__ = { 'id': id, 'email': email, 'doc_number': doc_number, 'balance': balance } def to_dict(self): return dict( id=self.id, email=self.email, doc_number=self.doc_number, balance=self.balance ) def find_self_transactions(self): return Transaction.find_transaction_from_user(self.id) def update_object_on_db(self, payload): self.email = payload.get('email') or self.email self.doc_number = payload.get('doc_number') or self.doc_number if payload.get('password'): self.password = User.generate_hash(payload.get('password')) db.session.commit() def delete_from_db(self): self.is_active = False db.session.commit() def update_balance(self, value): self.balance += value db.session.commit() @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash) @classmethod def find_by_email(cls, email): return cls.query \ .filter_by(email=email) \ .filter_by(is_active=True) \ .first() @classmethod def find_by_doc_number(cls, doc_number): return cls.query\ .filter_by(doc_number=doc_number) \ .filter_by(is_active=True) \ .first()
class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) def __repr__(self): return '<Product %r>' % self.name
class Tour(db.Model): id = db.Column(db.Integer, primary_key=True) tour_name = db.Column(db.String(20), unique=True, nullable=False) place = db.Column(db.String(20), nullable=False) activities = db.Column(db.String(40), nullable=False) description = db.Column(db.Text, nullable=True) price = db.Column(db.Float, nullable=False) number_days = db.Column(db.Integer, nullable=False) number_people = db.Column(db.Integer, nullable=False, default=2) isSummer = db.Column(db.Boolean, nullable=False, default=True) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') sale = db.Column(db.Boolean, nullable=False, default=False) clients = db.relationship('Client', backref='tour', lazy=True) def __repr__(self): return f"Tour ('{self.tour_name}', '{self.place}', '{self.image_file}')" def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns}