class StoreStatus(db.Model): """ Maps subclass of declarative_base() to a Python class to table store_statuses """ __tablename__ = 'store_status' id = db.Column(db.Integer) sequence = db.Column(db.Integer, nullable=False) # unique name = db.Column(db.String, nullable=False) # unique description = db.Column(db.String) stores = db.relationship('Store', backref='status', lazy='dynamic') # __table_args__ value must be a tuple, dict, or None __table_args__ = (db.PrimaryKeyConstraint('id', name='pk_store_status'), db.UniqueConstraint('sequence', 'name', name='uq_store_status_1')) def get_name(id): """ Returns name based on id (pk). """ status = StoreStatus.query.filter_by(id=id).first() return status.name def get_all(): """ Returns all store statuses ordered by sequence """ return StoreStatus.query.order_by('sequence').all() def to_dict(self): """ Convert the model to a dictionary that can go into a JSON """ return { 'sequence': self.sequence, 'name': self.name, 'description': self.description }
class DistributionCenter(db.Model): """ Maps subclass of declarative_base() to a Python class to table distribution_centers """ __tablename__ = 'distribution_centers' id = db.Column(db.Integer) country_code = db.Column(db.String(2), nullable=False) # unique number = db.Column(db.Integer, nullable=False) # unique name = db.Column(db.String, nullable=False) # unique tag = db.Column(db.String) created_date = db.Column(db.DateTime(timezone=True), server_default=func.now()) updated_date = db.Column(db.DateTime(timezone=True), onupdate=func.now()) stores = db.relationship('Store', backref='dc', lazy='dynamic') # __table_args__ value must be a tuple, dict, or None __table_args__ = (db.PrimaryKeyConstraint('id', name='pk_distribution_centers'), db.ForeignKeyConstraint(['country_code'], ['countries.country_code']), db.UniqueConstraint('country_code', 'number', name='uq_distribution_centers_1')) def get_id(country_code, number): """ Returns id (pk) based on country_code and number. Some countries (like LU) have no DC, it needs to be handled in imports. """ # In table distribution_centers we can use the unique # country_code, number dc = DistributionCenter.query.filter_by(country_code=country_code, number=number).first() return dc.id def get_name(id): """ Returns name based on id (pk). """ dc = DistributionCenter.query.filter_by(id=id).first() return dc.name def get_all(): """ Returns all distribution centers ordered by country_code, number """ return DistributionCenter.query.order_by('country_code', 'number').all() def to_dict(self): """ Convert the model to a dictionary that can go into a JSON """ return { # id is unnecessary 'country_code': self.country_code, 'number': self.number, 'name': self.name, 'tag': self.tag, 'created_date': self.created_date, 'updated_date': self.updated_date }
class User(UserMixin, db.Model): """ Maps subclass of declarative_base() to a Python class to table users_oauth2. UserMixin provides default implementations for the methods that Flask-Login expects user objects to have: is_active, is_authenticated, is_anonymous, get_id """ __tablename__ = 'users' id = db.Column(db.Integer) provider = db.Column(db.String, nullable=False) social_id = db.Column(db.String, nullable=False) email_address = db.Column(db.String) username = db.Column(db.String) password_hash = db.Column(db.String) created_date = db.Column(db.DateTime(timezone=True), server_default=func.now()) updated_date = db.Column(db.DateTime(timezone=True), onupdate=func.now()) stores = db.relationship('Store', backref='user', lazy='dynamic') # __table_args__ value must be a tuple, dict, or None __table_args__ = (db.PrimaryKeyConstraint('id', name='pk_users'), db.UniqueConstraint('provider', 'social_id', name='uq_users_1')) def generate_social_id(size=20, chars=string.digits): """ Generates a social_id that can be used for local myapp users. It's big enough so that it should be unique. Returns: social_id """ return ''.join(random.SystemRandom().choice(chars) for _ in range(size)) @property def password(self): """ The password property will call generate_argon2_hash and write the result to the password_hash field. Reading this property will return an error. """ raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_argon2_hash(password) def verify_password(self, password): """ Verifies the password against the hashed version stored in the User model. """ return check_argon2_hash(password, self.password_hash) def to_dict(self): """ Convert the model to a dictionary that can go into a JSON """ return { # id is unnecessary 'provider': self.provider, 'social_id': self.social_id, 'email_address': self.email_address, 'username': self.username, 'created_date': self.created_date, 'updated_date': self.updated_date }