class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) 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) post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) def __repr__(self): return f"Post('{self.content}', '{self.date_posted}')"
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 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 Item(db.Model): __tablename__ = 'items' id = db.Column(db.Integer, primary_key=True) item_id = db.Column(db.Integer, nullable=False, unique=True) title = db.Column(db.String(50), index=True) image_href = db.Column(db.String(255), nullable=False, unique=True) description = db.Column(db.String(255), nullable=True) price = db.Column(db.Integer, nullable=True) category_id = db.Column(db.Integer, db.ForeignKey('categories.id')) def __repr__(self): return "\nItem(item_id='{self.item_id}',\n" \ "\t\t title='{self.title}')\n"\ "\t\t image_href='{self.image_href}')\n" \ "\t\t price='{self.price}')\n" \ "\t\t description='{self.description}')".format(self=self)
class Merchant(Base): # __tablename__ will shown as real table-name on DBMS, not a table's column. __tablename__ = 'merchant' # column name will assigned as unique and non-nullable. name = db.Column(db.String(constants.CONST_STR_128), unique=True, nullable=False) # Owner of Merchant, must be a valid/registered User. # Notes it's a Foreign Key to non-primary key, using column 'user.username' as a FK, not using # 'user.id' as user's original PK username = db.Column(db.String, db.ForeignKey('user.username'), nullable=True) description = db.Column(db.String(constants.CONST_STR_128), unique=False, nullable=False) city = db.Column(db.String(constants.CONST_STR_128), unique=False, nullable=False)
class Product(Base): __tablename__ = 'product' name = db.Column(db.String(constants.CONST_STR_128), unique=False, nullable=False) price = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
if len(user_id) < 6: user = User.query.get(int(user_id)) else: user = m_db.users.find_one({'_id': user_id}) if user: user = UserDict(user) user['is_authenticated'] = True else: user = {'is_authenticated': False} user = UserDict(user) return user cat_association_table = db.Table( 'cat_association', db.Model.metadata, db.Column('post_id', db.Integer, db.ForeignKey('post.id')), db.Column('category_id', db.Integer, db.ForeignKey('category.id'))) user_following = db.Table( 'user_following', db.Model.metadata, db.Column('user_id', db.Integer, db.ForeignKey("user.id"), primary_key=True), db.Column('following_id', db.Integer, db.ForeignKey("user.id"), primary_key=True)) class User(db.Model, UserMixin):
from marketplace import db # Association table for associating items with tags and vice-versa item_tags = db.Table('item_tags', db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')), db.Column('item_id', db.Integer, db.ForeignKey('items.id')) ) from marketplace.models.user import User from marketplace.models.tag import Tag # Must come before Item, because Item refers to Tag for the relationship from marketplace.models.item import Item
except: from content.data_app import db, login from werkzeug.security import generate_password_hash, check_password_hash from datetime import datetime from sqlalchemy.dialects.postgresql import MONEY from flask_login import UserMixin from sqlalchemy_searchable import SearchQueryMixin, make_searchable from sqlalchemy_utils.types.ts_vector import TSVectorType make_searchable(db.metadata) producer_category_association_table = db.Table( 'producers_categories', db.Column('producer_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('category_id', db.Integer, db.ForeignKey('category.id'), primary_key=True)) subscribe_consumer_product = db.Table( 'subscribe_consumer_product', db.Column('product_id', db.Integer, db.ForeignKey('product.id'), primary_key=True), db.Column('consumer_id', db.Integer, db.ForeignKey('user.id'),