class Employee(Person): __tablename__ = 'employees' # __table_args__ = {'extend_existing': True} id = db.Column(db.String(10), db.ForeignKey('people.id'), primary_key=True) salary = db.Column(db.Integer) job_percentages = db.Column(db.Integer, default=100) bank_account_details = db.Column(db.String(250), nullable=True) is_employed = db.Column(db.Boolean, default=True) __mapper_args__ = { 'polymorphic_identity': Role.Employee, } def __init__(self, params): super(Employee, self).__init__(params) self.update(params) def update(self, params): super(Employee, self).update(params) self.salary = params["salary"] job_percentages = int(params["job_percentages"]) if not (0 <= job_percentages <= 100): raise ValueError("Job percentages must be between 0 to 100") self.job_percentages = job_percentages self.bank_account_details = params.get("bank_account_details") self.is_employed = Cast.to_bool(params.get("is_employed")) @property def is_manager(self): return self.role == Role.Manager
class Manager(Employee, UserMixin): __tablename__ = 'managers' id = db.Column(db.String(10), db.ForeignKey('employees.id'), primary_key=True) password = db.Column(db.String(100)) __mapper_args__ = { 'polymorphic_identity': Role.Manager, } def __init__(self, params): super(Manager, self).__init__(params) self.update(params) self.role = Role.Manager def update(self, params): super(Manager, self).update(params) if "password" in params: self.set_password(params["password"]) def fire(self, employee_id): employee = Employee.query.get(employee_id) if not employee: raise ValueError(f"Employee ID {employee_id} was not found") employee.is_employed = False db.session.commit() print(f'{employee.name.title()} ({employee_id}) have been fired') def execution(self, animal_id): animal = Animal.query.get(animal_id) if not animal: raise ValueError(f"Animal ID {animal_id} was not found") db.session.delete(animal) db.session.commit() print(f'{animal.id} was executed') def set_password(self, password): self.password = generate_password_hash(password, method='sha256') @staticmethod def pay_salary(employee: Employee): print(f'Salary have been payed for: {employee.name}')
class Dog(Animal): __tablename__ = "dogs" id = db.Column(db.Integer, db.ForeignKey('animals.id'), primary_key=True) chip = db.Column(db.String(50)) is_dangerous = db.Column(db.Boolean) __mapper_args__ = { 'polymorphic_identity': AnimalKind.Dog } def __init__(self, params): super(Dog, self).__init__(params) self.kind = AnimalKind.Dog self.update(params) def update(self, params): super(Dog, self).update(params) self.is_dangerous = Cast.to_bool(params.get('dangerous', False)) self.chip = params.get('chip')
class Cat(Animal): __tablename__ = "cats" id = db.Column(db.Integer, db.ForeignKey('animals.id'), primary_key=True) is_playful = db.Column(db.Boolean, default=False) is_hairy = db.Column(db.Boolean, default=False) is_loudly = db.Column(db.Boolean, default=False) __mapper_args__ = {'polymorphic_identity': AnimalKind.Cat} def __init__(self, params): super(Cat, self).__init__(params) self.kind = AnimalKind.Cat self.update(params) def update(self, params): super(Cat, self).update(params) self.is_playful = Cast.to_bool(params.get('is_playful', False)) self.is_hairy = Cast.to_bool(params.get('is_hairy', False)) self.is_loudly = Cast.to_bool(params.get('is_loudly', False))
class Person(db.Model): __tablename__ = "people" id = db.Column(db.String(10), primary_key=True, unique=True) name = db.Column(db.String(50)) gender = db.Column(db.Enum(Gender, name="person_gender")) role = db.Column(db.Enum(Role)) birth_date = db.Column(db.DateTime, nullable=True) phone_number = db.Column(db.String, nullable=True) email = db.Column(db.String, nullable=True) __mapper_args__ = { 'polymorphic_identity': 'people', 'polymorphic_on': role } def __init__(self, params): self.update(params) def update(self, params): self.id = params["id"] self.name = params["name"] self.gender = Gender(params["gender"]) self.role = Role(params["role"]) self.birth_date = Cast.to_datetime(params.get("birth_date")) self.phone_number = params.get("phone_number") email = params.get("email") self.email = email.lower() if email else None def __eq__(self, other): return type(self) is type(other) and self.id == other.id @property def age(self): if not self.birth_date: return None today = date.today() return today.year - self.birth_date.year - ( (today.month, today.day) < (self.birth_date.month, self.birth_date.day))
class Animal(db.Model): __tablename__ = "animals" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) gender = db.Column(db.Enum(AnimalGender)) birth_date = db.Column(db.DateTime, nullable=True) kind = db.Column(db.Enum(AnimalKind)) breed = db.Column(db.String(100)) size = db.Column(db.Enum(AnimalSize)) weight = db.Column(db.Float) cage = db.Column(db.Integer, nullable=True) arrival_date = db.Column(db.DateTime, default=datetime.now()) date_of_sterilization = db.Column(db.DateTime, nullable=True) fruitful = db.Column(db.Boolean, default=True) is_trained = db.Column(db.Boolean, default=False) friendly = db.Column(db.Boolean, default=True) adopted = db.Column(db.Boolean, default=False) details = db.Column(db.String(250), nullable=True) profile_image = db.Column(db.String(100)) __mapper_args__ = { 'polymorphic_identity': 'animals', 'polymorphic_on': kind } def __init__(self, params): self.update(params) def update(self, params): self.name = params['name'] self.kind = AnimalKind(params['kind']) self.gender = AnimalGender(params['gender']) self.breed = params['breed'] self.size = AnimalSize(params['size']) self.weight = float(params['weight']) self.cage = int(params['cage']) self.birth_date = Cast.to_datetime(params.get('birth_date')) self.arrival_date = Cast.to_datetime(params.get('arrival_date')) self.fruitful = Cast.to_bool(params.get('fruitful')) self.is_trained = Cast.to_bool(params.get('is_trained')) self.friendly = Cast.to_bool(params.get('friendly')) self.adopted = Cast.to_bool(params.get('adopted')) self.details = params.get('details') if 'profile_image' in params: self.profile_image = params['profile_image']