def export(): if request.method == 'GET': user = UserController.find_by_id(request.args.get("user_id")) query_sets = CompanyController.get_items(user.user_handle_industry) column_names = CompanyController.get_columns_name() return excel.make_response_from_query_sets(query_sets, column_names, "xlsx")
def update_item(source, item_id): user = UserController.find_by_id(request.args.get("user_id")) # import datetime # updated_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") data = request.get_json() if 'target_id' in data and 'conversation' in data and "status_id" in data: return create_item( source, user.user_id ) # changed from edit conversation to create new conversation every time user update it # email = EmailStatController.find_by_id(source, item_id) # if source == "employee": # email.alu_user_id = user.user_id # to show latest user who edit the record # email.alu_alumnus_id = data['target_id'] # email.alu_conversation = data['conversation'] # email.alu_status_id = data['status_id'] # email.alu_updated_time = updated_time # else: # email.comp_user_id = user.user_id # to show latest user who edit the record # email.comp_comp_id = data['target_id'] # email.comp_conversation = data['conversation'] # email.comp_status_id = data['status_id'] # email.comp_updated_time = updated_time # # error = email.commit() # if type(error) is str: # response = { # 'message': error # } # return jsonify(response), 400 # response = { # 'message': 'Conversation updated.' # } # return jsonify(response), 202 response = {'status': 'error', 'message': 'Bad request body.'} 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(user_id): user = UserController.find_by_id(user_id) error = user.delete() if type(error) is str: response = {'status': 'error', 'message': error} return jsonify(response), 400 response = {'message': 'user deleted'} return jsonify(response), 202
def uploader(): if request.method == 'POST': # check if the post request has the file part # if 'csv' not in request.files: # file = request.files['csv'] # print(file) try: filename = files.save(request.files['csv']) except UploadNotAllowed as err: response = { 'status': 'test', 'message': "File upload is not allowed!" + str(err) } return jsonify(response), 400 # below is the for loop code and creation code user = UserController.find_by_id(request.args.get("user_id")) from app.config import Config with open(Config.UPLOADS_DEFAULT_DEST + "files/" + filename, mode='r') as csv_file: import csv csv_reader = csv.DictReader(csv_file) error_message = "" for row in csv_reader: is_area_check = CommonServices.check_area( user.user_handle_industry, int(row['company_industry_id'])) if not is_area_check: error_message += "\r\n" + row[ 'company_reg_num'] + ": Cannot create company that not belong to logon user\'s industry area. " else: error = CompanyController.create_item( company_reg_num=row['company_reg_num'], company_name=row['company_name'], company_size=row['company_size'], company_industry_id=row['company_industry_id'], company_desc=row['company_desc'], company_office_contact_num=row[ 'company_office_contact_num'], company_address=row['company_address'], company_postcode=row['company_postcode'], company_city=row['company_city'], company_state=row['company_state'], company_country=row['company_country']) if type(error) is str: error_message += "\r\n" + row[ 'company_reg_num'] + ": " + error response = { 'status': 'done', 'message': "All imported" if error_message == "" else error_message } return jsonify(response), 200 if error_message == "" else 400 else: response = { 'status': 'success', 'url': files.url('company_template.csv') } return jsonify(response), 200
def create_item(): data = request.get_json() if 'username' in data and 'user_full_name' in data and 'user_email' in data: existing_user = UserController.find_user_by_username( username=data['username']).first() if existing_user is not None: response = {'message': 'user already exists'} return jsonify(response), 403 error = UserController.create_item( username=data['username'], user_full_name=data['user_full_name'], user_email=data['user_email']) if type(error) is str: response = {'status': 'error', 'message': error} return jsonify(response), 400 response = {'message': "new user registered"} return jsonify(response), 202 response = {'status': 'error', 'message': 'bad request body'} return jsonify(response), 400
def export(source): if request.method == 'GET': user = UserController.find_by_id(request.args.get("user_id")) item_list = EmailStatController.get_items(source, user.user_handle_industry) result_list = [] for item in item_list: result = serialize_field(source, item) result_list.append(result) return excel.make_response_from_records(result_list, "xlsx")
def get_item_details(user_id): user_schema = UserSchema() user = UserController.find_by_id(user_id) if user is None: response = {'message': 'user does not exist'} return jsonify(response), 404 result = user_schema.dump(user) # user_json = 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_items(): user_schema = UserSchema(many=True) # many=True is to get many object users_list = UserController.get_items() if users_list is None: response = {'message': 'no user created'} return jsonify(response), 404 result = user_schema.dump(users_list) # user_json = 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 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 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 login(): # service = request.args.get("service") # ticket = request.args.get("ticket") # print(service, ticket) # root = CASService.get_cas_respond(service, ticket) root = CASService.get_root() # print(root) username = root.find("cas:authenticationSuccess/cas:user", Config.XML_NAMESPACES).text user_schema = UserSchema() user = user_schema.dump(UserController.find_user_by_username(username)) # print(user) response = {'data_response': user, 'status_code': 202} from flask import jsonify return jsonify(response), 202
def update_item(user_id): data = request.get_json() if 'user_role' in data and 'user_handle_industry' in data: user = UserController.find_by_id(user_id) # print(user) user.user_role_id = data['user_role'] user.user_handle_industry_id = data['user_handle_industry'] error = user.commit() if type(error) is str: response = {'message': error} return jsonify(response), 400 response = {'message': 'user updated'} return jsonify(response), 202 response = {'status': 'error', 'message': 'bad request body'} return jsonify(response), 400
def routes(): user = UserController.find_by_id(request.args.get("user_id")) if request.method == 'POST': data = request.get_json() if not data['employee_industry_id'] == '': is_area_check = CommonServices.check_area( user.user_handle_industry, int(data['employee_industry_id'])) if not is_area_check: response = { 'status': 'error', 'message': 'Cannot create employee that not belong to logon user\'s industry area' } return jsonify(response), 400 return create_item() else: return get_items(user.user_handle_industry)
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 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)