def update_department(): if request.method == "POST": uuid = request.form.get("uuid") name = request.form.get("name") DepartmentService.update_one_department(uuid=uuid, data={"name": name}) flash("Department Updated Successfully") return redirect(url_for('main.get_departments'))
def get(self, uuid: str = None): """ Process GET request on resource. Returns collection of all departments if uuid is not given, and response status code 200 Returns one department object and status code 200 if uuid is given In other cases returns response code - 404 """ # all if not uuid: return DepartmentService.fetch_all(), 200 # one result = DepartmentService.fetch_one(uuid=uuid) if result: return result, 200 return result, 404
def setUp(self): """ Prepare test fixture, create application""" self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() # adding departments self.test_department_1 = Department(name="TestDepartment1") self.test_department_2 = Department(name="TestDepartment2") # adding employee self.test_employee1 = Employee(first_name="John", last_name="Doe", date_of_birth=datetime(1990, 10, 10), phone_number="0970000000", email="*****@*****.**", salary=1000.00, department_id=1) self.test_employee2 = Employee(first_name="Jane", last_name="Doe", date_of_birth=datetime(1990, 10, 10), phone_number="0660000000", email="*****@*****.**", salary=2000.00, department_id=2) db.session.add_all([ self.test_department_1, self.test_department_2, self.test_employee1, self.test_employee2 ]) db.session.commit() self.department_service = DepartmentService() self.employee_service = EmployeeService() # set up flask test client self.client = self.app.test_client()
def get_employees(): # fetch all employees if request.method == 'GET': data = EmployeeService.fetch_all() departments = DepartmentService.fetch_all() return render_template("employees.html", data=data, departments=departments)
def put(self, uuid: str): """ Process PUT request on resource, updating it """ # Check input json_data = request.get_json() if not json_data: return {"message": "No input data provided"}, 400 result, message = DepartmentService.update_one_department( uuid=uuid, data=json_data) if result == 'validation error': return message, 422 if result == "not found": return message, 404 if result == "duplicate": return message, 409 return message, 200
def post(self): """ Process POST request on resource. Returns status code 201 and new resource if succeeded or status code and error message """ # Check input json_data = request.get_json() if not json_data: return {"message": "No input data provided"}, 400 result, message = DepartmentService.add_one(json_data=json_data) if not result: return message, 409 if message.get( "message") == "Such record already exists" else 422 return message, 201
def get_departments(): # fetch all departments if request.method == 'GET': data = DepartmentService.fetch_all_departments_aggregated(db.session) return render_template("index.html", data=data)
def add_department(): if request.method == 'POST': department_name = request.form.get("name") if department_name: DepartmentService.add_one({"name": department_name}) return redirect(url_for('main.get_departments'))