Пример #1
0
 def fetch_all_of_department(department_uuid: str):
     """
     Returns tuple first element list of dictionaries af all employees
     of given department uuid, second element string name of department
     """
     department_id = Department.get_by_uuid(department_uuid).id
     department_name = Department.get_by_uuid(department_uuid).name
     if department_id:
         result = Employee.query.filter_by(
             department_id=department_id).all()
         return employees_schema.dump(result), department_name
Пример #2
0
 def add_one(data):
     """
     Takes json data, deserialize it and add to database
     Returns a tuple of added object uuid and dictionary of message
     If object hasn't been added returns None and error message
     """
     # Validate input
     try:
         data = employee_schema.load(data, partial=True)
     except ValidationError as err:
         return None, err.messages
     employee, department_uuid = data
     # Check if data for related department provided
     if not department_uuid:
         return None, {"message:": "department uuid not provided"}
     department_id = Department.get_by_uuid(department_uuid).id
     new_record = Employee(department_id=department_id, **employee)
     # Try to add record to db, if records exists it raise IntegrityError
     db.session.add(new_record)
     try:
         db.session.commit()
     except IntegrityError:
         return None, {"message": "Such record already exists"}
     return new_record.uuid, {
         "message": "Added new employee",
         "uuid": new_record.uuid
     }
Пример #3
0
 def fetch_one(uuid: str):
     """
     Takes uuid of department and returns serialised dictionary for one Department
     object, if uuid not found returns None.
     """
     query_result = Department.get_by_uuid(uuid=uuid)
     if not query_result:
         return None
     return department_schema.dump(query_result)
Пример #4
0
    def delete(self, uuid: str):
        """
        Process DELETE request on resource, deletes record with given uuid.
        Returns status code 204 on successful delete in other case - 404
        """
        db_record = Department.get_by_uuid(uuid)
        if uuid and db_record:
            try:
                Department.delete(db_record)
            except IntegrityError:
                return {}, 404

            return {}, 204
        return {}, 404
Пример #5
0
    def update_one_department(uuid: str, data: dict):
        """
        Takes uuid of department obj and data
        and updated department obj
        returns tuple of status and message
        """
        try:
            data = department_schema.load(data, partial=("uuid", ))
        except ValidationError as err:
            return "validation error", err.messages

        # query existing record in not found - return 404
        db_record = Department.get_by_uuid(uuid)
        if not db_record:
            return "not found", {"message": "record for update not found"}

        # update record if there is updated field
        if name := data.get("name"):
            db_record.name = name
Пример #6
0
        if not db_record:
            return "not found", {"message": "updated record not found"}

        employee, department_uuid = data

        # update record if there is updated field
        if first_name := employee.get("first_name"):
            db_record.first_name = first_name
        if last_name := employee.get("last_name"):
            db_record.last_name = last_name
        if date_of_birth := employee.get("date_of_birth"):
            db_record.date_of_birth = date_of_birth
        if phone_number := employee.get("phone_number"):
            db_record.phone_number = phone_number
        if email := employee.get("email"):
            db_record.email = email
        if salary := employee.get("salary"):
            db_record.salary = salary

        # update if uuid of department was given
        if department_uuid:
            department_id = Department.get_by_uuid(department_uuid).id
            if department_id:
                db_record.department_id = department_id

        try:
            db.session.commit()
        except IntegrityError:
            return "duplicate", {"message": "Such record already exists"}
        return "OK", {"message": "resource updated"}
Пример #7
0
def delete_department(uuid):
    db_record = Department.get_by_uuid(uuid)
    Department.delete(db_record)
    flash("Department Deleted Successfully")
    return redirect(url_for('main.get_departments'))