def export(): if request.method == 'GET': user = UserController.find_by_id(request.args.get("user_id")) query_sets = EmployeeController.get_items(user.user_handle_industry) column_names = EmployeeController.get_columns_name() return excel.make_response_from_query_sets(query_sets, column_names, "xlsx")
def get_jobs(item_id, company_id=None): user = UserController.find_by_id(request.args.get("user_id")) item_schema = EmpCompSchema(many=True) item = EmployeeController.find_by_id(item_id) is_area_check = True if item is not None: if item.employee_industry_id is not None: is_area_check = CommonServices.check_area( user.user_handle_industry, int(item.employee_industry_id)) if is_area_check: if request.method == 'GET': jobs = EmployeeController.get_jobs(item_id) result = item_schema.dump(jobs) # json_response = json.loads(result) response = { 'data_response': result, } return jsonify(response), 200 elif request.method == 'DELETE': item = EmployeeController.find_job(item_id, company_id) db.session.delete(item) error = item.commit() if type(error) is str: response = {'message': error} return jsonify(response), 400 response = {'message': 'Job deleted.'} return jsonify(response), 202 elif request.method == 'POST': data = request.get_json() if not str(data['employee_company_Id']) == '': job_error = EmployeeController.add_working_job( item_id, data['employee_company_Id'], data['job_designation'], data['job_department'], data['job_hired_date'], data['is_current_job']) if type(job_error) is str: response = {'status': 'error', 'message': job_error} return jsonify(response), 400 response = {'message': 'Job Added.'} return jsonify(response), 200 else: response = { 'status': 'error', 'message': 'Cannot perform action on employee that not belong to logon user\'s industry area' } return jsonify(response), 400 response = {'status': 'error', 'message': 'Employee not found!'} return jsonify(response), 400
def item_details(item_id): user = UserController.find_by_id(request.args.get("user_id")) item = EmployeeController.find_by_id(item_id) is_area_check = True if item is not None: if item.employee_industry_id is not None: is_area_check = CommonServices.check_area( user.user_handle_industry, int(item.employee_industry_id)) if is_area_check: if request.method == 'GET': return get_item_details(item_id) elif request.method == 'PUT': return update_item(item_id) elif request.method == 'DELETE': return delete_item(item_id) else: response = { 'status': 'error', 'message': 'Cannot perform action on employee that not belong to logon user\'s industry area' } return jsonify(response), 400 response = {'status': 'error', 'message': 'Employee not found!'} return jsonify(response), 400
def delete_item(item_id): item = EmployeeController.find_by_id(item_id) item.is_hide = True error = item.commit() if type(error) is str: response = {'message': error} return jsonify(response), 400 response = {'message': 'Employee soft deleted.'} return jsonify(response), 202
def get_items(industry_area): item_schema = EmployeeSchema(many=True) item_list = EmployeeController.get_items(industry_area) if item_list is None: response = {'message': 'No employee created.'} return jsonify(response), 404 result = item_schema.dump(item_list) # json_response = json.loads(result) response = { 'data_response': result, } return jsonify(response), 200
def check_reg_num(email): item_schema = EmployeeSchema() item = EmployeeController.find_by_contact(email) if item is None: response = {'message': 'Employee does not exist.'} return jsonify( response), 200 # avoid error message on frontend, so 200 result = item_schema.dump(item) # json_response = json.loads( # result) # load back as json object so the response won't treat it as db.String and escape it response = { 'data_response': result, } return jsonify(response), 200
def get_unassigned(): user = UserController.find_by_id(request.args.get("user_id")) item_schema = EmployeeSchema(many=True) item_list = EmployeeController.get_unassigned_employees( user.user_handle_industry) if item_list is None: response = {'message': 'No employee created.'} return jsonify(response), 404 print(item_list) result = item_schema.dump(item_list) # json_response = json.loads(result) response = { 'data_response': result, } return jsonify(response), 200
def get_item_details(item_id, isCheck=False): item_schema = EmployeeSchema() item = EmployeeController.find_by_id(item_id) if item is None: response = {'message': 'Employee does not exist.'} if isCheck: return jsonify(response), 200 else: return jsonify(response), 404 result = item_schema.dump(item) # json_response = json.loads( # result) # load back as json object so the response won't treat it as db.String and escape it response = { 'data_response': result, } return jsonify(response), 200
def serialize_field(source, item): result = {} if source == "employee": user = UserController.find_by_id(item.alu_user_id) employee = EmployeeController.find_by_id(item.alu_alumnus_id) status = EmailStatus.query.filter_by( status_id=item.alu_status_id).first() result["email_id"] = item.alu_email_id result["user_full_name"] = user.user_full_name result["user_id"] = item.alu_user_id result["target_name"] = employee.employee_full_name result["target_id"] = item.alu_alumnus_id result["open_time"] = item.alu_open_time result["sum_open"] = item.alu_sum_open result["status_name"] = status.status_name result["status_id"] = item.alu_status_id result["industry_area_name"] = employee.employee_industry.industry_name result["industry_area_id"] = employee.employee_industry_id result["conversation"] = item.alu_conversation result["updated_time"] = item.alu_updated_time else: user = UserController.find_by_id(item.comp_user_id) company = CompanyController.find_by_id(item.comp_comp_id) status = EmailStatus.query.filter_by( status_id=item.comp_status_id).first() result["email_id"] = item.comp_email_id result["user_full_name"] = user.user_full_name result["user_id"] = item.comp_user_id result["target_name"] = company.company_name result["target_id"] = item.comp_comp_id result["open_time"] = item.comp_open_time result["sum_open"] = item.comp_sum_open result["status_name"] = status.status_name result["status_id"] = item.comp_status_id result["industry_area_name"] = company.company_industry.industry_name result["industry_area_id"] = company.company_industry_id result["conversation"] = item.comp_conversation result["updated_time"] = item.comp_updated_time return result
def item_details(source, item_id): user = UserController.find_by_id(request.args.get("user_id")) item = EmailStatController.find_by_id(source, item_id) if item is not None: if source == "employee": employee = EmployeeController.find_by_id(item.alu_alumnus_id) if employee is None: response = { 'status': 'error', 'message': 'Target id not found' } return jsonify(response), 400 industry_id = employee.employee_industry_id else: company = CompanyController.find_by_id(item.comp_comp_id) if company is None: response = { 'status': 'error', 'message': 'Target id not found' } return jsonify(response), 400 industry_id = company.company_industry_id is_area_check = CommonServices.check_area(user.user_handle_industry, int(industry_id)) if is_area_check: if request.method == 'GET': return get_item_details(source, item_id) elif request.method == 'PUT': return update_item(source, item_id) else: response = { 'status': 'error', 'message': 'Cannot perform action on conversation that not belong to logon user\'s industry area' } return jsonify(response), 400 response = {'status': 'error', 'message': 'Conversation not found!'} return jsonify(response), 400
def get_contacts(item_id, employee_id=None): print("adding contact") item = CompanyController.find_by_id(item_id) from app.modules.EmployeeModule.EmployeeController import EmployeeController employee = EmployeeController.find_by_id(employee_id) if item is not None: if employee is not None: if request.method == 'DELETE': result = CompanyController.contact_action( item, employee, 'delete') response = {'message': result} return jsonify(response), 202 elif request.method == 'POST': result = CompanyController.contact_action( item, employee, 'add') response = {'message': result} return jsonify(response), 200 else: response = {'status': 'error', 'message': 'Employee not found!'} return jsonify(response), 400 response = {'status': 'error', 'message': 'Company not found!'} return jsonify(response), 400
def routes(source): user = UserController.find_by_id(request.args.get("user_id")) if request.method == 'POST': data = request.get_json() if data["target_id"] == "": response = {'status': 'error', 'message': 'Target user is empty'} return jsonify(response), 400 if source == "employee": employee = EmployeeController.find_by_id(data["target_id"]) if employee is None: response = { 'status': 'error', 'message': 'Target id not found' } return jsonify(response), 400 industry_id = employee.employee_industry_id else: company = CompanyController.find_by_id(data["target_id"]) if company is None: response = { 'status': 'error', 'message': 'Target id not found' } return jsonify(response), 400 industry_id = company.company_industry_id is_area_check = CommonServices.check_area(user.user_handle_industry, int(industry_id)) if not is_area_check: response = { 'status': 'error', 'message': 'Cannot create conversation that not belong to logon user\'s industry area' } return jsonify(response), 400 return create_item(source, user.user_id) else: return get_items(source, user.user_handle_industry)
def create_item(): data = request.get_json() # print(data) for field in employee_fields: if field not in data: response = {'status': 'error', 'message': 'Bad request body.'} return jsonify(response), 400 error = EmployeeController.create_item( employee_full_name=data['employee_full_name'], employee_industry_id=data['employee_industry_id'], employee_alumnus=data['employee_alumnus'], employee_address=data['employee_address'], employee_postcode=data['employee_postcode'], employee_city=data['employee_city'], employee_state=data['employee_state'], employee_country=data['employee_country'], employee_contact_num=data['employee_contact_num'], employee_email=data['employee_email'], employee_intake_code=data['employee_intake_code'], employee_grad_time=data['employee_grad_time']) if type(error) is str: response = {'status': 'error', 'message': error} return jsonify(response), 400 # if employee_current_company_Id is not null, then insert into employeecompany table # if not str(data['employee_current_company_Id']) == '': # job_error = EmployeeController.add_working_job(error.employee_id, data['employee_current_company_Id'], # data['current_job_designation'], data['current_job_department'], # data['current_job_hired_date'], True) # if type(job_error) is str: # response = { # 'status': 'error', # 'message': error + ", but the employee info still created successfully." # } # return jsonify(response), 400 response = {'message': "New employee registered."} return jsonify(response), 202
def update_item(item_id): data = request.get_json() for field in employee_fields: if field not in data: response = {'status': 'error', 'message': 'Bad request body.'} return jsonify(response), 400 item = EmployeeController.find_by_id(item_id) is_error = False error_message = "" if data['employee_full_name'] is not None and data[ 'employee_full_name'] == '': is_error = True error_message += "Must provide full name" if not type(data['employee_alumnus']) == bool: is_error = True if error_message is not "": error_message += ", " error_message += "Must set is APU alumnus or not" if data['employee_contact_num'] is not None and not data[ 'employee_contact_num'] == '': if not re.match('^(\\+?6?0)[0-9]{1,2}-*[0-9]{7,8}$', str(data['employee_contact_num'])): is_error = True if error_message is not "": error_message += ", " error_message += "Contact number must be in correct format! Received value: " + str( data['employee_contact_num']) if data['employee_postcode'] is not None and not data[ 'employee_postcode'] == '': if not re.match('^[0-9]{1,6}$', str(data['employee_postcode'])): is_error = True if error_message is not "": error_message += ", " error_message += "Postcode must be in correct format! Received value: " + str( data['employee_postcode']) if not str(data['employee_grad_time']) == "": try: if data['employee_grad_time'] is not None: datetime.datetime.strptime(data['employee_grad_time'], '%Y-%m-%d') except ValueError as ex: is_error = True if error_message is not "": error_message += ", " error_message += "Graduation Date format error: " + str( data['employee_grad_time']) if not is_error: if not data['employee_full_name'] == "": item.employee_address = data['employee_full_name'] if not data['employee_alumnus'] == "": item.employee_alumnus = data['employee_alumnus'] if not data['employee_address'] == "": item.employee_address = data['employee_address'] if not data['employee_industry_id'] == "": item.employee_industry_id = data['employee_industry_id'] if not data['employee_postcode'] == "": item.employee_postcode = data['employee_postcode'] if not data['employee_city'] == "": item.employee_city = data['employee_city'] if not data['employee_state'] == "": item.employee_state = data['employee_state'] if not data['employee_country'] == "": item.employee_country = data['employee_country'] if not data['employee_contact_num'] == "": item.employee_contact_num = data['employee_contact_num'] if not data['employee_grad_time'] == "": item.employee_grad_time = data['employee_grad_time'] if not data['employee_email'] == "": item.employee_email = data['employee_email'] if not data['employee_intake_code'] == "": item.employee_intake_code = data['employee_intake_code'] # if not data['employee_current_company_Id'] == "": # item.employee_current_company_Id = data['employee_current_company_Id'] error = item.commit() if type(error) is str: response = {'message': error} return jsonify(response), 400 response = {'message': 'Employee updated.'} return jsonify(response), 202 else: response = {'status': 'error', 'message': error_message} return jsonify(response), 400