def _add_salary_2(self, values): # Input data validation if not funcs.convert_to_int(values, [2, 4, 6, 8, 9, 10, 11]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[2] < 0 or values[4] < 0 or values[6] < 0 or values[8] < 0 \ or values[9] < 0 or values[10] < 0 or values[11] < 0: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) if values[11] > 0 and (values[2] > 0 or values[4] > 0 or values[6] > 0 or values[8] > 0 or values[9] > 0 or values[10] > 0): return Response(ResponseStatus.fail, strs.NOT_ALLOWED_FIX_AND_OTHER_MSG) employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) response = self._get_employee_wage(values) wage = response.get_data() if response.get_status() == ResponseStatus.fail or wage is None: return Response( ResponseStatus.fail, strs.WAGE_FOR_EMPLOYEE_MISSING_MSG.format(employee=values[0])) values[0] = employee_id values[3] = wage[0].get_day() values[5] = wage[0].get_hour() values[7] = wage[0].get_meal() return self._database_manager.actions(Actions.add_salary_2, values)
def _get_employee_uniform_pieces(self, values): start_date = values[1] end_date = values[2] if start_date > end_date: return Response(ResponseStatus.fail, strs.INVALID_DATES_MSG) result = list() if values[0] == strs.ALL: for piece in self._uniform_pieces: if start_date <= piece.get_date() <= end_date: result.append(piece) else: employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) for piece in self._uniform_pieces: if piece.get_employee_id( ) == employee_id and start_date <= piece.get_date( ) <= end_date: result.append(piece) return Response(ResponseStatus.success, strs.UNIFORM_PIECE_EMP_SUCC_MSG, result)
def _add_wage(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[0], values[1], values[2], values[3]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if not funcs.convert_to_int(values, [1, 2, 3]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[1] < 0 or values[2] < 0 or values[3] < 0: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = employee_id response = self._database_manager.actions(Actions.add_wage, values) if response.get_status() == ResponseStatus.success: self._wages.append(response.get_data()) self._update_wages_employees_names([-1]) return response
def _add_salary_1(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[0], values[1], values[2]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if not funcs.convert_to_int(values, [1, 2]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[1] < 0 or values[2] < 0: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) if values[1] > values[2]: return Response(ResponseStatus.fail, strs.INVALID_NET_GROSS_RATIO_MSG) employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = employee_id return self._database_manager.actions(Actions.add_salary_1, values)
def _add_free_days(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[0], values[4]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if values[3] <= 0: return Response(ResponseStatus.fail, strs.INVALID_DATES_MSG) employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = employee_id response = self._database_manager.actions(Actions.add_free_days, values) if response.get_status() == ResponseStatus.success: self._all_free_days.append(response.get_data()) self._update_free_days_employees_names([-1]) return response
def _update_child(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[1], values[2], values[4]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if values[6] == "" and values[8] == "": return Response(ResponseStatus.fail, strs.CHILD_ONE_PARENT_REQUIRED_MSG) mother_id = self._get_employee_id(values[6]) father_id = self._get_employee_id(values[8]) if mother_id is None and father_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) query_values = [ values[0], values[1], values[2], values[3], values[4], mother_id, father_id ] values[5] = mother_id values[7] = father_id response = self._database_manager.actions(Actions.update_child, query_values) if response.get_status() == ResponseStatus.success: for child in self._children: if child.get_child_id() == values[0]: child.update_data(values) break return response
def _update_uniform_piece(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[1], values[3], values[4], values[6]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if not funcs.convert_to_int(values, [3, 4]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[3] < 0 or values[4] < 0: return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) uniform_name = self._get_uniform_name(values[1]) if uniform_name is None: # TODO write to log return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) response = self._database_manager.actions(Actions.update_uniform_piece, values) if response.get_status() == ResponseStatus.success: for uniform_piece in self._uniform_pieces: if uniform_piece.get_uniform_piece_id() == values[0]: uniform_piece.update_data(values) uniform_piece.set_uniform_name(uniform_name) break return response
def _get_employee_free_days(self, values): result = list() start_date = values[1] end_date = values[2] if start_date > end_date: return Response(ResponseStatus.fail, strs.INVALID_DATES_MSG) if values[0] == strs.ALL: for free_days in self._all_free_days: if start_date <= free_days.get_start_date( ) and free_days.get_end_date() <= end_date: result.append(free_days) else: employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) for free_days in self._all_free_days: if free_days.get_employee_id() == employee_id \ and start_date <= free_days.get_start_date() \ and free_days.get_end_date() <= end_date: result.append(free_days) return Response(ResponseStatus.success, strs.FREE_DAYS_EMP_SUCC_MSG, result)
def _get_employee_salaries_2(self, values): start_date = values[1] end_date = values[2] if start_date > end_date: return Response(ResponseStatus.fail, strs.INVALID_DATES_MSG) if values[0] == strs.ALL: response = self._database_manager.actions( Actions.salaries_2_between_dates, [start_date, end_date]) else: employee_id = self._get_employee_id(values[0]) if employee_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = employee_id response = self._database_manager.actions( Actions.employee_salaries_2, values) if response.get_status() == ResponseStatus.success: result = list() for data in response.get_data(): employee_name = self._get_employee_unique_name(data[1]) result.append(funcs.transform_salary_data(employee_name, data)) response.set_data(result) return response
def _update_salary_2(self, values): # Input data validation if not funcs.convert_to_int(values, [3, 5, 7, 9, 10, 11, 12]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[3] < 0 or values[5] < 0 or values[7] < 0 or values[9] < 0 \ or values[10] < 0 or values[11] < 0 or values[12] < 0: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) return self._database_manager.actions(Actions.update_salary_2, values)
def _get_employee_wage(self, values): if values[0] == strs.ALL: return Response(ResponseStatus.success, strs.WAGE_EMP_SUCC_MSG, self._wages) employee_id = self._get_employee_id(values[0]) for wage in self._wages: if wage.get_employee_id() == employee_id: return Response(ResponseStatus.success, strs.WAGE_EMP_SUCC_MSG, [wage]) return Response(ResponseStatus.fail, strs.WAGE_EMP_FAIL_MSG)
def _update_salary_1(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[2], values[3]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if not funcs.convert_to_int(values, [2, 3]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[2] < 0 or values[3] < 0: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) if values[2] > values[3]: return Response(ResponseStatus.fail, strs.INVALID_NET_GROSS_RATIO_MSG) return self._database_manager.actions(Actions.update_salary_1, values)
def _update_employee(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[1], values[2], values[4], values[7], values[9]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) # Check before m fields before_m = values[11] if not funcs.convert_to_int(before_m, range(3)): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) years = before_m[0] months = before_m[1] days = before_m[2] if years < 0 or months < 0 or months > 11 or days < 0 or days > 30: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_DATE_FORMAT_MSG) values[11] = funcs.to_days(years, months, days) position_id = self._get_position_id(values[7]) # Check position id if position_id is None: # TODO write to log return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[7] = position_id response = self._database_manager.actions(Actions.update_employee, values) if response.get_status() == ResponseStatus.success: for employee in self._employees: if employee.get_employee_id() == values[0]: employee.update_data(values) break return response
def _update_free_days(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[5]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if values[4] <= 0: return Response(ResponseStatus.fail, strs.INVALID_DATES_MSG) response = self._database_manager.actions(Actions.update_free_days, values) if response.get_status() == ResponseStatus.success: for free_days in self._all_free_days: if free_days.get_free_days_id() == values[0]: free_days.update_data(values) break return response
def _add_uniform(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[0]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) response = self._database_manager.actions(Actions.add_uniform, values) if response.get_status() == ResponseStatus.success: self._uniforms.append(response.get_data()) return response
def _update_wage(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[2], values[3], values[4]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) if not funcs.convert_to_int(values, [2, 3, 4]): return Response(ResponseStatus.fail, strs.NOT_INTEGER_MSG) if values[2] < 0 or values[3] < 0 or values[4] < 0: # TODO write to log return Response(ResponseStatus.fail, strs.INVALID_MEASURE_MSG) response = self._database_manager.actions(Actions.update_wage, values) if response.get_status() == ResponseStatus.success: for wage in self._wages: if wage.get_wage_id() == values[0]: wage.update_data(values) break return response
def _update_position(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[1], values[2]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) position_id = self._get_position_id(values[0]) if position_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = position_id response = self._database_manager.actions(Actions.update_position, values) if response.get_status() == ResponseStatus.success: for position in self._positions: if position.get_position_id() == values[0]: position.update_data(values) break return response
def _update_uniform(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[1]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) uniform_id = self._get_uniform_id(values[0]) if uniform_id is None: # TODO write to log return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = uniform_id response = self._database_manager.actions(Actions.update_uniform, values) if response.get_status() == ResponseStatus.success: for uniform in self._uniforms: if uniform.get_uniform_id() == uniform_id: uniform.set_name(values[1]) break return response
def _add_child(self, values): # Input data validation # Check required fields if not funcs.check_required_fields(values[0], values[1], values[3]): return Response(ResponseStatus.fail, strs.REQUIRED_FIELDS_NOT_FILLED_MSG) mother_id = self._get_employee_id(values[4]) father_id = self._get_employee_id(values[5]) if mother_id is None and father_id is None: return Response(ResponseStatus.fail, strs.CHILD_ONE_PARENT_REQUIRED_MSG) values[4] = mother_id values[5] = father_id response = self._database_manager.actions(Actions.add_child, values) if response.get_status() == ResponseStatus.success: self._children.append(response.get_data()) self._update_children_parents([-1]) return response
def _login(self, values): # Input data validation if not funcs.check_required_fields(values[0], values[1]): return Response(ResponseStatus.fail, strs.MISSING_CREDENTIALS_MSG) # Check credentials response = self._database_manager.actions(Actions.login, values) if response.get_status() == ResponseStatus.success: self._user = User.from_values(response.get_data()[0]) response.set_message( strs.SUCCESSFUL_LOGIN_MSG.format( username=self._user.get_username())) return response
def _delete_position(self, values): position_id = self._get_position_id(values[0]) if position_id is None: return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = position_id response = self._database_manager.actions(Actions.delete_position, values) if response.get_status() == ResponseStatus.success: for position in self._positions: if position.get_position_id() == position_id: self._positions.remove(position) break return response
def _delete_uniform(self, values): uniform_id = self._get_uniform_id(values[0]) if uniform_id is None: # TODO Write to log return Response(ResponseStatus.fail, strs.INTERNAL_ERROR_MSG) values[0] = uniform_id response = self._database_manager.actions(Actions.delete_uniform, values) if response.get_status() == ResponseStatus.success: for uniform in self._uniforms: if uniform.get_uniform_id() == uniform_id: self._uniforms.remove(uniform) break return response
def _execute_query(self, query, query_type, values=None): status = ResponseStatus.success data = None conn = None try: params = self.db conn = psycopg2.connect(**params) cur = conn.cursor() cur.execute(query, values) if values else cur.execute(query) if query_type in [QueryType.select, QueryType.insert]: if cur.rowcount == 0: # Do not set status to fail, because some queries are successful even if the rowcount == 0 data = list() elif cur.rowcount == 1: data = [cur.fetchone()] elif cur.rowcount > 1: data = cur.fetchall() message = cur.statusmessage print(f"Query: {query}") print(f"DB Data: {data}") print(f"DB Message: {message}\n") conn.commit() cur.close() except Exception as error: # TODO Write error to a log status = ResponseStatus.fail message = strs.DATABASE_ERROR_MSG.format(error=error) print(message) finally: if conn is not None: conn.close() return Response(status, message, data)