class Location(db.Model): postal_code = db.Column(db.Integer, primary_key=True) city = db.Column(db.String(30), nullable=False) state = db.Column(db.String(30), nullable=False) posts = db.relationship('Post', backref='location', lazy=True) users = db.relationship('User', backref='location', lazy=True) def __repr__(self): return f"Location('{self.postal_code},{self.city},{self.state}')"
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) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) location_id = db.Column(db.Integer, db.ForeignKey('location.postal_code'), nullable=False) comments = db.relationship('Comment', backref='post', lazy=True) category = db.relationship("Category", secondary=cat_association_table, back_populates="posts") def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) items = db.relationship('Item', backref='category') def __repr__(self): return "\nCategory(name='{self.name}',\n" \ "\t\t href='{self.href}')".format(self=self)
class Product(SetPhotoUrlMixin, RatedMixin, db.Model): query_class = ProductQuery id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128)) description = db.Column(db.String(256)) photo_url = db.Column(db.String(256)) price = db.Column(MONEY) quantity = db.Column(db.Integer) times_ordered = db.Column(db.Integer) producer_id = db.Column(db.Integer) category_id = db.Column(db.Integer) measurement_unit = db.Column(db.String(16)) weight = db.Column(db.Float) rating = db.Column(db.Float, default=0) votes = db.Column(db.Integer, default=0) subscribers = db.relationship("Consumer", secondary=subscribe_consumer_product, backref=db.backref('subscribe', lazy=True)) search_vector = db.Column( TSVectorType('name', 'description', weights={ 'name': 'A', 'description': 'B' }, regconfig='pg_catalog.russian')) def __init__(self, price, name, quantity, producer_id, category_id, measurement_unit, weight=None, description='', photo_url='static/img/standard.png'): self.price = float(price) self.name = name self.quantity = quantity self.producer_id = producer_id self.category_id = category_id self.measurement_unit = measurement_unit self.times_ordered = 0 self.weight = weight self.description = get_string_or_default(description) self.photo_url = photo_url def set_description(self, description): self.description = description def get_producer(self): return Producer.query.filter_by(id=self.producer_id).all() def get_category(self): return Category.query.filter_by(id=self.category_id).all()
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) comments = db.relationship('Comment', backref='comment_author', lazy=True) location_id = db.Column(db.Integer, db.ForeignKey('location.postal_code'), nullable=True) phone = db.Column(db.String(13), nullable=True) role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=False, default=2) following = db.relationship( 'User', lambda: user_following, primaryjoin=lambda: User.id == user_following.c.user_id, secondaryjoin=lambda: User.id == user_following.c.following_id, backref='followers') def get_reset_token(self, expires_sec=1800): s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text, nullable=False) description = db.Column(db.Text, nullable=False) posts = db.relationship("Post", secondary=cat_association_table, back_populates="category") def __str__(self): return str(self.name) + ' - ' + str(self.description) def __repr__(self): return f"Category('{self.name},{self.description}')"
class Item(db.Model): __tablename__ = "items" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.String(100)) item_name = db.Column(db.String(200)) item_description = db.Column(db.String(2000)) image_url = db.Column(db.String) price = db.Column(db.Float) date_listed = db.Column(db.DateTime) date_sold = db.Column(db.DateTime) status = db.Column(db.String(20)) # Reference to the tags table tags = db.relationship('Tag', secondary=item_tags, backref=db.backref('items', lazy='dynamic')) def __init__(self, user_id, item_name, item_description, item_url, price, date_listed, date_sold, status): self.user_id = user_id self.item_name = item_name self.item_description = item_description self.image_url = item_url self.price = price self.date_listed = date_listed self.date_sold = date_sold self.status = status def __repr__(self): return '<Item %r>' % self.item_name @property def serialize(self): """ Used for JSONify and render templates """ tag_output = [] for tag in self.tags: tag_output.append(tag.serialize) return { 'id': self.id, 'user_id': self.user_id, 'item_name': self.item_name, 'item_description': self.item_description, 'item_url': self.image_url, 'price': self.price, 'tags': tag_output, 'date_listed': str(self.date_listed), 'date_sold': str(self.date_sold), 'status': self.status }
class Producer(RatedMixin, User): __mapper_args__ = {'polymorphic_identity': 'producer'} name = db.Column(db.String(128), unique=True) person_to_contact = db.Column(db.String(128)) description = db.Column(db.String(256)) rating = db.Column(db.Float, default=0) votes = db.Column(db.Integer, default=0) trello_token = db.Column(db.String(256), nullable=True) trello_board_id = db.Column(db.String(256), nullable=True) categories = db.relationship("Category", secondary=producer_category_association_table, lazy='subquery', backref=db.backref('producers', lazy=True)) def __init__(self, password, email, name, phone_number, address, person_to_contact, description='', photo_url='static/img/standard-profile.jpg'): super().__init__(email, password, 'producer', phone_number, address) self.name = name self.person_to_contact = person_to_contact self.description = get_string_or_default(description) self.photo_url = photo_url def get_products(self): return Product.query.filter_by(producer_id=self.id).all() def get_orders(self): return Product.query.filter_by(producer_id=self.id).all() def set_description(self, description): self.description = description def link_trello_account(self, token, board_id): self.trello_token = token self.trello_board_id = board_id
class Role(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text, nullable=False) description = db.Column(db.Text, nullable=False) users = db.relationship('User', backref='role', lazy=True)