class CarBrand(db.Model): __tablename__ = 'carbrands' id = db.Column(db.Integer, primary_key=True) designation = db.Column(db.String(80), unique=True, index=True) carmodels = db.relationship('CarModel', backref='carbrand', lazy=False) cartypes = db.relationship('CarType', secondary='carbrand_types', backref='carbrand', lazy=False) def get_summary(self): carbrands = { 'designation': self.designation, 'brand_logos': [i.file_path for i in self.images] } if len(self.carmodels) > 0: carbrands['models'] = [{ model.designation: model for model in self.carmodels }] # a tester """{'modelname': [model.designation for model in self.carmodels], 'typemodel': [model.type.designation for model in self.carmodels], 'modeldateofprod': [model.date_of_prod for model in self.carmodels]}""" # ['modelname: ' + i.designation + ' | ' + 'typemodel: ' + i.type.designation for i in self.carmodels] return carbrands
class CarBrandImage(FileUpload): carbrand_id = db.Column(db.Integer, db.ForeignKey('carbrands.id'), nullable=True) carbrand = db.relationship('CarBrand', backref='images') __mapper_args__ = { 'polymorphic_identity': 'CarBrandImage' }
class UserImage(FileUpload): user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True) user = db.relationship('User', backref='images') __mapper_args__ = { 'polymorphic_identity': 'UserImage' }
class ProductImage(FileUpload): product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=True) product = db.relationship('Product', backref='images') __mapper_args__ = { 'polymorphic_identity': 'ProductImage' }
class WishList(db.Model): __tablename__ = 'wishlists' id = db.Column(db.Integer, primary_key=True) user_id = db.Column('User', db.Integer, db.ForeignKey('users.id'), nullable=False) product = db.relationship('Product', secondary='wishlistitems', lazy=False)
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) #account_types = Personal, Professional, Business, Premium account_type = db.Column(db.String(120), default='Business') user_type = db.Column(db.String(120), default='Beta') state = db.Column(db.String(120), default=fake.state_abbr()) country = db.Column(db.String(120), default=fake.country_code(representation="alpha-2")) set_path = db.Column(db.String(120), default='default') company = db.Column(db.String(255), default=fake.company()) plan_id = db.Column(db.Integer, db.ForeignKey('plan.id'), default=1) plan = db.relationship("Plan", backref="user") def _set_default_plan(self): return Plan.query.filter_by(name='free').first() def __repr__(self): return '<User {}>'.format(self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def get_email_hash(self): return hashlib.md5(self.email.encode()).hexdigest() def get_ld_user(self): app_version = current_app.config['VERSION'] milliseconds = int(round(time.time() * 1000)) user_key = self.get_email_hash() user = { 'key': user_key, 'email': self.email, "custom": { 'account_type': self.account_type, 'user_type': self.user_type, 'state': self.state, 'country': self.country, 'app_version': app_version, 'company': self.company or 'None', 'date': milliseconds, 'plan': self.plan.name }, 'privateAttributeNames': ['account_type', 'state'], } return user def get_random_ld_user(self): user = {'key': str(uuid.uuid1())} return user
class UserRole(db.Model): __tablename__ = 'users_roles' user_id = db.Column(db.Integer, db.ForeignKey("users.id")) role_id = db.Column(db.Integer, db.ForeignKey("roles.id")) # users = db.relationship("User", foreign_keys=[user_id], backref='roles') user = db.relationship("User", foreign_keys=[user_id], backref='users_roles') role = db.relationship("Role", foreign_keys=[role_id], backref='users_roles') created_at = db.Column(db.DateTime(), nullable=False, default=datetime.utcnow) __mapper_args__ = {'primary_key': [user_id, role_id]}
class PartData(db.Model): __tablename__ = 'partdatas' id = db.Column(db.Integer, primary_key=True) ref_part = db.Column(db.String(255), nullable=False, unique=True, index=True) weight = db.Column(db.Integer, nullable=False) diameter = db.Column(db.Integer, nullable=True) dimension = db.Column(db.String(100), nullable=True) date_of_prod = db.Column(db.DateTime(), nullable=True) num_oem = db.Column(db.String(300), nullable=False, unique=True, index=True) country_of_origin = db.Column(db.String(80), nullable=False) volume_of_part = db.Column(db.Integer) product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=True) car_compatibilities = db.relationship(CarModel, secondary='car_part_compts', backref='partdatas', lazy=False) car_compt = db.Table('car_part_compts', db.Column('partdata_id', db.Integer, db.ForeignKey('partdatas.id')), db.Column('carmodel_id', db.Integer, db.ForeignKey('carmodels.id')))
class CarModel(db.Model): __tablename__ = 'carmodels' id = db.Column(db.Integer, primary_key=True) designation = db.Column(db.String(300), unique=True, index=True, nullable=False) date_of_prod = db.Column(db.DateTime(), nullable=False) date_end_of_prod = db.Column(db.DateTime(), nullable=True) carbrand_id = db.Column(db.Integer, db.ForeignKey('carbrands.id'), nullable=False) type_id = db.Column(db.Integer, db.ForeignKey('cartypes.id'), nullable=False) # carbrand = db.relationship('CarBrand', foreign_keys=[carbrand_id], lazy='dynamic') type = db.relationship('CarType', foreign_keys=[type_id], lazy=False)
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) content = db.Column(db.Text, nullable=False) rating = db.Column(db.Integer, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) # user = relationship('User', backref=db.backref('comments')) user = db.relationship('User', foreign_keys=[user_id], lazy=False) product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=False) # product = relationship('Product', backref=db.backref('comments')) created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) updated_at = db.Column(db.DateTime, nullable=True, default=datetime.utcnow(), onupdate=datetime.utcnow()) def get_summary(self, include_product=False, include_user=False): data = { 'id': self.id, 'content': self.content, 'created_at': self.created_at, } if include_product: data['product'] = { 'id': self.products.id, 'name': self.products.name } if include_user: data['user'] = {'id': self.user_id, 'username': self.user.username} return data
class Quote(db.Model): __tablename__ = 'quotes' id = db.Column(db.Integer, primary_key=True) quote_title = db.Column(db.String(80), nullable=False) slug = db.Column(db.String(80), index=True, unique=True) content = db.Column(db.Text, nullable=False) carModel_information = db.Column(db.String(255), nullable=True) immatricuation = db.Column(db.String(255), nullable=True) num_chassis = db.Column(db.String(255), nullable=False) num_vin = db.Column(db.String(255), nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = db.relationship('User', foreign_keys=[user_id], lazy=False) seller_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) def get_summary(self, include_file=False): quote_summary = { 'title': self.quote_title, 'content': self.content, 'carmodel_inf': self.carModel_information, 'immatriculation': self.immatricuation, 'num_chassis': self.num_chassis, 'num_vin': self.num_vin, 'created_at': self.created_at } if include_file: quote_summary['files'] = [i.file_path for i in self.files] return quote_summary def slug_generator_for_quote(self, username): self.slug = 'quote-0' + str(random.randint(0, 100)) + str( randomString(5)) + '-' + str(username)
class Address(db.Model): __tablename__ = 'addresses' id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(100), nullable=False) last_name = db.Column(db.String(100), nullable=False) city = db.Column(db.String(100), nullable=False) country = db.Column(db.String(100), nullable=False) zip_code = db.Column(db.String(100), nullable=False) street_address = db.Column(db.String(255), nullable=False) phone_number = db.Column( db.String(255), nullable=True) # nullable because I have not implemented it user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True) user = db.relationship('User', backref='addresses') created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) def get_summary(self): data = { 'id': self.id, 'first_name': self.first_name, 'last_name': self.last_name, 'address': self.street_address, 'zip_code': self.zip_code, 'city': self.city, 'country': self.country, 'created_at': self.created_at, 'updated_at': self.updated_at } return data
class QuoteFileUploaded(FileUpload): quote_id = db.Column(db.Integer, db.ForeignKey('quotes.id'), nullable=True) quote = db.relationship('Quote', backref='files') __mapper_args__ = { 'polymorphic_identity': 'QuoteFile'}
class Product(db.Model): __tablename__ = 'products' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) slug = db.Column(db.String(255), index=True, unique=True) description = db.Column(db.Text, nullable=False) availability = db.Column(db.Boolean) quality = db.Column(db.String(100)) price = db.Column(db.Integer, nullable=False) stock = db.Column(db.Integer, nullable=False) rating = db.Column(db.Integer, nullable=True) # promotion_rate = db.Column(db.Integer, nullable=True) // a voir plus tard seller_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) manufacturer = db.Column(db.String(100), nullable=False) created_at = db.Column(db.DateTime(), default=datetime.utcnow, index=True, nullable=False) updated_at = db.Column(db.DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow, nullable=True) publish_on = db.Column(db.DateTime(), index=True, default=datetime.utcnow) categories = db.relationship('Category', secondary=products_categories, backref='products') comments = db.relationship('Comment', backref='products', lazy=False) partdata = db.relationship('PartData', uselist=False) def __repr__(self): return '<Product %r>' % self.name def __str__(self): return '<Product {}>'.format(self.name) def get_summary(self): products_infos = { 'id': self.id, 'name': self.name, 'price': self.price, 'stock': self.stock, 'slug': self.slug, 'availability': STATUS[self.availability], 'rating': self.rating, 'quality': self.quality, 'marque': self.manufacturer, 'comments_count': len(self.comments), 'comments': [CommentDetailsSerializer(c, include_user=True).data for c in self.comments], 'categories': [c.name for c in self.categories], 'image_urls': [i.file_path for i in self.images] } if self.partdata is not None: products_infos['ref_part'] = self.partdata.ref_part, products_infos['weight'] = self.partdata.weight, products_infos['date_of_prod'] = self.partdata.date_of_prod, products_infos['num_oem'] = self.partdata.num_oem, products_infos['country_of_origin'] = self.partdata.country_of_origin, products_infos['volume_of_part'] = self.partdata.volume_of_part, products_infos['manufacturer'] = self.partdata.manufacturer products_infos['dimension'] = self.partdata.dimension products_infos['list_car_compt'] = [car.designation for car in self.partdata.car_compatibilities] return products_infos def slug_generator_for_product(self, seller_id, prd_name): self.slug = 'product-0' + str(random.randint(0, 100)) + '-' + 'sllr-0' + str(seller_id) + '-' + str( randomString(3)) + str(prd_name)