class Department(db.Model): """Model for a department.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) def __repr__(self): return f"Department({self.id}, {self.name})"
class Department(db.Model): """ Create a Department table """ __tablename__ = 'departments' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), unique=True) description = db.Column(db.String(200)) employees = db.relationship('Employee', backref='department', lazy='dynamic') def to_dict(self): """ Serializer that returns a dictionary from its fields :return: the department in json format """ return { 'id': self.id, 'name': self.name, 'description': self.description, 'employees': [employee.to_dict() for employee in self.employees] } def __repr__(self): """ Representation of the department :return: a string representing the department by name """ return f'Department: {self.name}'
class Employee(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), index=True, unique=True) dob = db.Column(db.Date, index=True) salary = db.Column(db.Integer) department_id = db.Column(db.Integer, db.ForeignKey('department.id')) def __repr__(self): return f'<Employee {self.name}>' def to_dict(self): data = { 'id': self.id, 'name': self.name, 'dob': self.dob.isoformat(), 'salary': self.salary, 'department_id': self.department_id # TODO _links } return data def from_dict(self, data): for field in ['name', 'salary', 'department_id']: if data[field]: setattr(self, field, data[field]) if data['dob']: self.dob = datetime.strptime(data['dob'], '%Y-%m-%d').date()
class Employee(db.Model): """Model for an employee.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) date_of_birth = db.Column(db.DateTime, nullable=False) salary = db.Column(db.Float, nullable=False) department_id = db.Column(db.Integer, db.ForeignKey("department.id"), nullable=False) department = db.relationship("Department", backref="department", lazy=True) def __repr__(self): return (f"Employee({self.id}, {self.name}, {self.salary},\ {self.date_of_birth}, {self.department}")
class Department(db.Model): __tablename__ = "Department" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(64), nullable=False) employees = db.relationship('Employee', backref='department', lazy=True) def to_dict(self): return { "id": self.id, "department name": self.name, "employees": [employee.name for employee in self.employees] } def __repr__(self): return "department : {}".format(self.name)
class Employee(db.Model): __tablename__ = 'Employee' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(32), nullable=False) surname = db.Column(db.String(32), nullable=False) date_of_birth = db.Column(db.Date, nullable=False) salary = db.Column(db.Float, default=0) department_id = db.Column(db.Integer, db.ForeignKey('Department.id')) def to_dict(self): return { "id": self.id, "name": self.name, "surname": self.surname, "birth date": self.date_of_birth.isoformat(), "salary": self.salary, "department": self.department.name if self.department is not None else "unemployed" } def __repr__(self): return "Name: {}, date of birth: {}, salary: {}".format( self.name, self.date_of_birth, self.salary)
class Department(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), index=True, unique=True) employees = db.relationship('Employee', backref='department', lazy='dynamic') def __repr__(self): return f'<Department {self.title}>' def to_dict(self): data = { 'id': self.id, 'title': self.title # TODO _links } return data def from_dict(self, data): for field in ['title']: if field in data: setattr(self, field, data[field])
class Employee(UserMixin, db.Model): """ Create an Employee table """ # Ensures table will be named in plural and not in singular # as is the name of the model __tablename__ = 'employees' id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(length=60), nullable=False, unique=True) email = db.Column(db.String(length=60), nullable=False, unique=True) first_name = db.Column(db.String(length=60), nullable=False) last_name = db.Column(db.String(length=60), nullable=False) password_hash = db.Column(db.String(length=128), nullable=False) department_id = db.Column(db.Integer, db.ForeignKey('departments.id')) salary = db.Column(db.Integer) birthday = db.Column(db.Date) @property def password(self): """ Prevent password from being accessed """ raise AttributeError('Password is not readable attribute.') @password.setter def password(self, plain_text_password): """ Set password to a hashed password """ self.password_hash = bcrypt.generate_password_hash( plain_text_password).decode('utf-8') def check_password_correction(self, attempted_password): """ Check if hashed password matches actual password :return: `True` or `False` """ return bcrypt.check_password_hash(self.password_hash, attempted_password) # pylint: disable=no-self-use def calculate_age(self, birth): """ Calculate the age of an employee by date of birth :return: the age of the employee """ today = date.today() return today.year - birth.year - ((today.month, today.day) < (birth.month, birth.day)) def to_dict(self): """ Serializer that returns a dictionary from its fields :return: the employee in json format """ return { 'id': self.id, 'username': self.username, 'email': self.email, 'first_name': self.first_name, 'last_name': self.last_name, 'department': Department.query.get_or_404(self.department_id).name, 'salary': self.salary, # pylint: disable=no-member 'birthday': self.birthday.strftime('%m/%d/%Y') } def __repr__(self): """ Representation of the employee :return: a string representing the employee by username """ return f'Employee: {self.username}'