def usuario_delete(id): employee = Employee.query.get(id) if employee is None: return error_handler(404, aeh.SQL_NOT_FOUND, id, message=aeh.NO_EMPLOYEE_TO_DELETE) subordinates = get_employees_under([id], 1) subordinatesID = [s['employeeID'] for s in subordinates] try: changed_employees = get_indirect_changes(employee) sql_result = employee_schema.dump(employee) db.session.delete(employee) db.session.commit() if changed_employees is not None and len(changed_employees) > 0: return jsonify({ 'employee':sql_result, 'indirect_changes':{ 'api_warning':'These employees were also changed to keep database consistency.', 'changes': "Attribute 'managerID' set to NULL.", 'employees':changed_employees } }), 200 return jsonify(sql_result), 200 except Exception as e: db.session.rollback() db.session.flush() return error_handler(400, aeh.SQL_CONSTRAINT_FAILED, message=str(e))
def get_company_employees(id): company = Company.query.get(id) if company is None: return error_handler(404, aeh.SQL_NOT_FOUND, 'company') company_employees = Employee.query.filter(Employee.companyID == id) sql_result = employees_schema.dump(company_employees) if len(sql_result) == 0: return jsonify(sql_result), 404 return jsonify(sql_result), 200
def get_company_structure(id, level): try: level = int(level) except Exception as e: return error_handler(400, aeh.HTTP_PARAM_TYPE, "'level'", 'numeric') employee = Employee.query.get(id) if employee is None: return error_handler(404, aeh.SQL_NOT_FOUND, 'employee') if level <= 0: subordinates = Employee.query.filter(and_(Employee.managerID==employee.managerID, Employee.companyID == employee.companyID)).all() sql_result = employees_schema.dump(subordinates) else: sql_result = get_employees_under([id], int(level)) if len(sql_result) == 0: return jsonify(sql_result), 404 return jsonify(sql_result), 200
def employee_update(id): employee = Employee.query.get(id) if employee is None: return error_handler(404, aeh.SQL_NOT_FOUND, id, message=aeh.NO_EMPLOYEE_TO_EDIT) try: companyID = None if 'companyID' not in request.json or request.json['companyID'] is None else int(request.json['companyID']) managerID = None if 'managerID' not in request.json or request.json['managerID'] is None else int(request.json['managerID']) except: return error_handler(400, aeh.HTTP_PARAM_TYPE, ['companyID', 'managerID'], 'numeric') if 'name' in request.json: employee.name = request.json['name'] if 'email' in request.json: employee.email = request.json['email'] changed_employees = None if 'companyID' in request.json: if companyID is not None: company = Company.query.get(companyID) if company is None: return error_handler(404, aeh.SQL_NOT_FOUND, companyID, message=aeh.NO_COMPANY_TO_ASSOCIATE) if employee.companyID != companyID: changed_employees = get_indirect_changes(employee) employee.companyID = companyID if 'managerID' in request.json: if managerID is not None: manager = Employee.query.get(request.json['managerID']) if manager is None: return error_handler(404, aeh.SQL_NOT_FOUND, request.json['managerID'], message=aeh.NO_EMPLOYEE_TO_ASSIGN) if manager.companyID != employee.companyID: return error_handler(400, aeh.API_NOT_SAME_COMPANY, manager.companyID, employee.companyID) if not valid_company_structure(manager.employeeID, employee.employeeID): return error_handler(400, aeh.API_STRUCTURE_LOOP) employee.managerID = managerID # if all went right so far, commit to database try: db.session.flush() db.session.commit() sql_result = employee_schema.dump(employee) except Exception as e: return error_handler(400, aeh.SQL_CONSTRAINT_FAILED, message=str(e)) if changed_employees is not None and len(changed_employees) > 0: return jsonify({ 'employee':sql_result, 'indirect_changes':{ 'api_warning':'These employees were also changed to keep database consistency.', 'changes': 'Attribute managerID set to NULL.', 'employees':changed_employees } }), 200 return employee_schema.jsonify(employee), 200
def add_company(): required = ['name'] missing = check_missing_parameters(request, required) if len(missing) > 0: return jsonify({ "HTTP Error": "Missing required parameter(s) " + str(missing) + "." }), 400 name = request.json['name'] new_company = Company(name) try: db.session.add(new_company) db.session.flush() sql_result = company_schema.dump(new_company) db.session.commit() return jsonify(sql_result), 200 except Exception as e: return error_handler(400, aeh.SQL_CONSTRAINT_FAILED, message=str(e))
def add_employee(): required = ['name', 'email'] missing = check_missing_parameters(request, required) if len(missing) > 0: return error_handler(400, aeh.HTTP_MISSING_PARAMS, missing) name = request.json['name'] email = request.json['email'] try: companyID = None if 'companyID' not in request.json else int(request.json['companyID']) managerID = None if 'managerID' not in request.json else int(request.json['managerID']) except: return error_handler(400, aeh.HTTP_PARAM_TYPE, ['companyID', 'managerID'], 'numeric') new_employee = Employee(name, email, companyID, managerID) if companyID is not None: company = Company.query.get(request.json['companyID']) if company is None: return error_handler(400, aeh.SQL_NOT_FOUND, request.json['companyID'], message=aeh.NO_COMPANY_TO_ASSOCIATE) if managerID is not None: manager = Employee.query.get(request.json['managerID']) if manager is None: return error_handler(400, aeh.SQL_NOT_FOUND, request.json['managerID'], message=aeh.NO_EMPLOYEE_TO_ASSIGN) if manager.companyID != new_employee.companyID: return error_handler(400, aeh.API_NOT_SAME_COMPANY, manager.companyID, new_employee.companyID) # There's no need to verify for loops since the employee is being created now, therefore has no subordinates. try: db.session.add(new_employee) db.session.flush() sql_result = employee_schema.dump(new_employee) db.session.commit() return jsonify(sql_result), 200 except Exception as e: db.session.rollback() db.session.flush() return error_handler(400, aeh.SQL_CONSTRAINT_FAILED, message=str(e))
def get_company_detail(id): company = Company.query.get(id) if company is None: return error_handler(404, aeh.SQL_NOT_FOUND, 'company') sql_result = company_schema.dump(company) return jsonify(sql_result), 200
def get_employee_detail(id): employee = Employee.query.get(id) if employee is None: return error_handler(404, aeh.SQL_NOT_FOUND, 'employee') sql_result = employee_schema.dump(employee) return jsonify(sql_result), 200