From 88075ae4a038d7c16a45d6c8789e78e341b94c2f Mon Sep 17 00:00:00 2001 From: odex Date: Tue, 16 Jul 2024 15:06:42 +0300 Subject: [PATCH] fix --- .../odex_mobile/controllers/overtime.py | 2 +- .../controllers/rest_api_v2/leave.py | 4 +- .../controllers/rest_api_v2/overtime.py | 213 ++++++++++++------ .../controllers/rest_api_v2/permission.py | 79 +++++-- odex25_mobile/odex_mobile/http_helper.py | 3 +- .../controllers/controllers.py | 4 +- 6 files changed, 211 insertions(+), 94 deletions(-) diff --git a/odex25_mobile/odex_mobile/controllers/overtime.py b/odex25_mobile/odex_mobile/controllers/overtime.py index b43502df7..aae6f3921 100644 --- a/odex25_mobile/odex_mobile/controllers/overtime.py +++ b/odex25_mobile/odex_mobile/controllers/overtime.py @@ -200,7 +200,7 @@ class OverTimeController(http.Controller): return http_helper.response(message=_("Overtime created successfully"), data={'overtimes': [value],}) except (UserError, AccessError, ValidationError, Warning) as e: - error = str(e.name) + "\n" + str(e.value) + error = str(e.name) + "\n" + str(e) return http_helper.response(code=400, message=str(error), success=False) except Exception as e: http.request._cr.rollback() diff --git a/odex25_mobile/odex_mobile/controllers/rest_api_v2/leave.py b/odex25_mobile/odex_mobile/controllers/rest_api_v2/leave.py index 0db98c1e4..972b628f2 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/leave.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/leave.py @@ -35,6 +35,7 @@ class LeaveController(http.Controller): return li def get_return_data(self, hol, approvel=None): + value = {'id': hol.id, 'type': hol.holiday_status_id.name, 'type_value': hol.holiday_status_id.id, @@ -123,7 +124,8 @@ class LeaveController(http.Controller): records = balance.filtered(lambda r: r.holiday_status_id == s) value = { "id": s.id, - "name": s.with_context({"employee_id": employee.id}).name_get()[0][1] or "", + # "name": s.with_context({"employee_id": employee.id}).name_get()[0][1] or "", + "name": s.name or "", "ticket": s.issuing_ticket, "balance": records[0].remaining_leaves if records else 0, "alternative_chick": s.alternative_chick, diff --git a/odex25_mobile/odex_mobile/controllers/rest_api_v2/overtime.py b/odex25_mobile/odex_mobile/controllers/rest_api_v2/overtime.py index a7c90ce16..24f6b7e6e 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/overtime.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/overtime.py @@ -3,7 +3,7 @@ import werkzeug from odoo import http,tools from odoo.http import request, Response from odoo.addons.auth_signup.models.res_users import SignupError -from odoo.exceptions import UserError +from odoo.exceptions import UserError, AccessError, ValidationError, Warning import base64 from ...validator import validator from ...http_helper import http_helper @@ -17,10 +17,12 @@ from odoo.tools.translate import _ class OverTimeController(http.Controller): # overtime get - + @http.route(['/rest_api/v2/overtimes/data'], type='http', auth='none', csrf=False, methods=['GET']) def get_overtime_datas(self,transfer_type=None, **kw): try: + http_method, body, headers, token = http_helper.parse_request() + user = validator.verify(token) data = {} if transfer_type == 'accounting': account = request.env['account.account'].sudo().search([]) @@ -35,9 +37,12 @@ class OverTimeController(http.Controller): else: return http_helper.response(message=_("Data Not Found"), data=data) + except (UserError, AccessError, ValidationError, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: _logger.error(str(e)) - message = validator.get_server_error(e, request.env.user) + message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) @http.route(['/rest_api/v2/overtimes'], type='http', auth='none', csrf=False, methods=['GET']) @@ -69,8 +74,18 @@ class OverTimeController(http.Controller): over = [] if overtime: for s in overtime: - value = {'id': s.id, 'transfer_type': s.transfer_type,'request_date':str(s.request_date),'date_from':str(s.date_from), - 'date_to':str(s.date_to), 'state_name':s.state,'state':validator.get_state_name(s,s.state),'reason': s.reason, 'overtime_plase': s.overtime_plase,} + value = { + "id": s.id, + "transfer_type": http_helper.get_lable_selection(s,'transfer_type',s.transfer_type),#s.transfer_type, + "request_date": str(s.request_date), + "date_from": str(s.date_from), + "date_to": str(s.date_to), + "state_name": s.state, + "state": validator.get_state_name(s, s.state), + "reason": s.reason, + "reason_msg": s.reason or "", + "overtime_plase": s.overtime_plase, + } if approvel: value.update({ 'account_id': s.sudo().account_id.name if s.sudo().account_id else False, @@ -113,65 +128,90 @@ class OverTimeController(http.Controller): return http_helper.response(message="Data Found", data=data) + except (UserError, AccessError, ValidationError, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: _logger.error(str(e)) message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) + # overtime create - + @http.route(['/rest_api/v2/overtimes'], type='http', auth='none', csrf=False, methods=['POST']) def create_overtime(self, **kw): - data = kw.get('lines', {}) - if data: - data = json.loads(data) - http_method, body, headers, token = http_helper.parse_request() - result = validator.verify_token(token) - if not result['status']: - return http_helper.errcode(code=result['code'], message=result['message']) - user = validator.verify(token) - if not user: - return http_helper.response(code=400, - message=_("You are not allowed to perform this operation. please check with one of your team admins"), - success=False) - employee = http.request.env['hr.employee'].search([('user_id', '=', user.id)], limit=1) - if not employee: - return http_helper.response(code=400, - message=_("You Have issue in your employee profile. please check with one of your team admins"), - success=False) - if not body.get('reason') : - return http_helper.response(code=400, - message=_("You need to enter reason"), - success=False) - if not body.get('transfer_type') : - return http_helper.response(code=400, - message=_("You need to enter transfer type"), - success=False) - - if not body.get('overtime_plase') : - return http_helper.response(code=400, - message=_("You need to enter overtime plase"), - success=False) - - - if not body.get('date_from') or not body.get('date_to') or not body.get('request_date'): - return http_helper.response(code=400, - message=_("You need to enter request date ,Date from and Date to"), - success=False) - if not data: - return http_helper.response(code=400, message=_("Enter Lines for Overtime"), success=False) try: - s = http.request.env['employee.overtime.request'].sudo().create( - {'transfer_type': body['transfer_type'], 'request_date':body['request_date'], 'date_from': body['date_from'], - 'date_to': body['date_to'],'reason': body['reason'], 'overtime_plase': body['overtime_plase'], - 'line_ids_over_time':[ (0, 0, {'employee_id':employee.id, - 'over_time_workdays_hours':l['over_time_workdays_hours'], - 'over_time_vacation_hours':l['over_time_vacation_hours'], - })for l in data] - }) + data = kw.get('lines', {}) + if data: + data = json.loads(data) + data = json.loads(data["lines"]) + http_method, body, headers, token = http_helper.parse_request() + result = validator.verify_token(token) + if not result['status']: + return http_helper.errcode(code=result['code'], message=result['message']) + user = validator.verify(token) + if not user: + return http_helper.response(code=400, + message=_("You are not allowed to perform this operation. please check with one of your team admins"), + success=False) + employee = http.request.env['hr.employee'].search([('user_id', '=', user.id)], limit=1) + if not employee: + return http_helper.response(code=400, + message=_("You Have issue in your employee profile. please check with one of your team admins"), + success=False) + if not body.get('reason') : + return http_helper.response(code=400, + message=_("You need to enter reason"), + success=False) + if not body.get('transfer_type') : + return http_helper.response(code=400, + message=_("You need to enter transfer type"), + success=False) + + if not body.get('overtime_plase') : + return http_helper.response(code=400, + message=_("You need to enter overtime plase"), + success=False) + + if not body.get('date_from') or not body.get('date_to') or not body.get('request_date'): + return http_helper.response(code=400, + message=_("You need to enter request date ,Date from and Date to"), + success=False) + if not data: + return http_helper.response(code=400, message=_("Enter Lines for Overtime"), success=False) + s = http.request.env["employee.overtime.request"].sudo().create( + { + "transfer_type": body["transfer_type"], + "request_date": body["request_date"], + "date_from": body["date_from"], + "date_to": body["date_to"], + "reason": body["reason"], + "overtime_plase": body["overtime_plase"], + "line_ids_over_time": [(0,0, + { + "employee_id": employee.id, + "over_time_workdays_hours": l[ "over_time_workdays_hours"], + "over_time_vacation_hours": l["over_time_vacation_hours"], + }, + ) + for l in data + ], + } + ) + s.line_ids_over_time.get_max_remain_hours() if s: - value = {'id': s.id, 'transfer_type': s.transfer_type,'request_date':str(s.request_date),'date_from':str(s.date_from), - 'date_to':str(s.date_to), 'state_name':s.state,'state':validator.get_state_name(s,s.state),'reason': s.reason, 'overtime_plase': s.overtime_plase} + value = { + "id": s.id, + "transfer_type": http_helper.get_lable_selection(s,'transfer_type',s.transfer_type),#s.transfer_type, + "request_date": str(s.request_date), + "date_from": str(s.date_from), + "date_to": str(s.date_to), + "state_name": s.state, + "state": validator.get_state_name(s, s.state), + "reason": s.reason, + "overtime_plase": s.overtime_plase, + } li = [] if s.line_ids_over_time: for r in s.line_ids_over_time: @@ -188,15 +228,18 @@ class OverTimeController(http.Controller): return http_helper.response(message=_("Overtime created successfully"), data={'overtimes': [value],}) + except (UserError, AccessError, ValidationError, Exception, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: - http.request._cr.rollback() _logger.error(str(e)) message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) + # overtime edit - @http.route(['/rest_api/v2/overtimes/'], type='http', auth='none', csrf=False, methods=['PUT']) + @http.route(['/rest_api/v2/overtimes/'], type='http', auth='none', csrf=False, methods=['PUT']) def edit_overtime(self,id,approvel=None, **kw): data = kw.get('lines', {}) if data: @@ -236,11 +279,18 @@ class OverTimeController(http.Controller): # if not data: # return http_helper.response(code=400, message=_("Enter Lines for Overtime"), success=False) try: + s = http.request.env['employee.overtime.request'].search([('id', '=', id)]) if s: - vals = {'transfer_type': body['transfer_type'], 'request_date':body['request_date'], 'date_from': body['date_from'], - 'date_to': body['date_to'],'reason': body['reason'], 'overtime_plase': s.overtime_plase, - } + vals = { + "transfer_type": body["transfer_type"], + "request_date": body["request_date"], + "date_from": body["date_from"], + "date_to": body["date_to"], + "reason": body["reason"], + "overtime_plase": s.overtime_plase, + } + if approvel: if s.transfer_type == 'accounting' and 'account' in body and 'journal' in body: vals.update({ @@ -257,8 +307,17 @@ class OverTimeController(http.Controller): if data: for t in data: self.get_overtime_line(s.employee_id, s, t) - value = {'id': s.id, 'transfer_type': s.transfer_type,'request_date':str(s.request_date),'date_from':str(s.date_from), 'overtime_plase': s.overtime_plase, - 'date_to':str(s.date_to), 'state_name':s.state,'state':validator.get_state_name(s,s.state),'reason': s.reason} + value = { + "id": s.id, + "transfer_type": http_helper.get_lable_selection(s,'transfer_type',s.transfer_type),# s.transfer_type, + "request_date": str(s.request_date), + "date_from": str(s.date_from), + "overtime_plase": s.overtime_plase, + "date_to": str(s.date_to), + "state_name": s.state, + "state": validator.get_state_name(s, s.state), + "reason": s.reason, + } if approvel: value.update({ 'account_id': s.sudo().account_id.name if s.sudo().account_id else False, @@ -281,12 +340,18 @@ class OverTimeController(http.Controller): value['lines'] = li return http_helper.response(message=_("Overtime Update successfully"), - data={'overtimes': [value],}) + data={'overtimes': [value], }) + else: + return http_helper.response(code=400, message="Overtime Update Not Fount", success=False) + except (UserError, AccessError, ValidationError, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: _logger.error(str(e)) message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) + def get_overtime_line(self,emp,over,t): vals = {'employee_id':emp.id, 'over_time_workdays_hours':t['over_time_workdays_hours'], @@ -328,11 +393,15 @@ class OverTimeController(http.Controller): return http_helper.response(code=400, message=_("You are can not perform this operation. please check with one of your team admins"), success=False) + except (UserError, AccessError, ValidationError, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: _logger.error(str(e)) message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) + @http.route(['/rest_api/v2/overtimes/'], type='http', auth='none', csrf=False, methods=['DELETE']) def delete_overtime(self, overtimeId, **kw): http_method, body, headers, token = http_helper.parse_request() @@ -358,6 +427,9 @@ class OverTimeController(http.Controller): return http_helper.response(code=400, message=_("You can not perform this operation. please check with one of your team admins"), success=False) + except (UserError, AccessError, ValidationError, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: _logger.error(str(e)) message = validator.get_server_error(e, user) @@ -381,8 +453,17 @@ class OverTimeController(http.Controller): s = http.request.env['employee.overtime.request'].search([('id', '=', id)]) value = None if s: - value = {'id': s.id, 'transfer_type': s.transfer_type,'request_date':str(s.request_date),'date_from':str(s.date_from), - 'date_to':str(s.date_to), 'state_name':s.state,'state':validator.get_state_name(s,s.state),'reason': s.reason, 'overtime_plase': s.overtime_plase,} + value = { + "id": s.id, + "transfer_type": http_helper.get_lable_selection(s,'transfer_type',s.transfer_type),#s.transfer_type, + "request_date": str(s.request_date), + "date_from": str(s.date_from), + "date_to": str(s.date_to), + "state_name": s.state, + "state": validator.get_state_name(s, s.state), + "reason": s.reason, + "overtime_plase": s.overtime_plase, + } if approvel: value.update({ 'account_id': s.sudo().account_id.name if s.sudo().account_id else False, @@ -413,7 +494,11 @@ class OverTimeController(http.Controller): return http_helper.response(message=_("Get Overtime successfully"), data={'overtimes': value,}) + except (UserError, AccessError, ValidationError, Warning) as e: + error = str(e) + return http_helper.response(code=400, message=str(error), success=False) except Exception as e: _logger.error(str(e)) message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) + diff --git a/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py b/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py index 63ad5f6b6..ff11322bd 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/permission.py @@ -51,11 +51,20 @@ class PermissionController(http.Controller): count = http.request.env['hr.personal.permission'].search_count([('employee_id', '=', employee.id)]) if permissions: for per in permissions: - value = {'employee_id': per.employee_id.id, 'employee_name': per.employee_id.name, 'id': per.id, - 'date_from': str(per.date_from), 'date_to': str(per.date_to), 'duration': per.duration, - 'date': str(per.date), - 'state': validator.get_state_name(per, per.state), 'state_name': per.state, - 'early_exit': per.early_exit, 'attachment': self.get_attchment(per)} + value = { + "employee_id": per.employee_id.id, + "employee_name": per.employee_id.name, + "id": per.id, + "date_from": str(per.date_from), + "date_to": str(per.date_to), + "duration": per.duration, + "date": str(per.date), + "state": validator.get_state_name(per, per.state), + "state_name": per.state, + "early_exit": per.early_exit, + "reason_msg": per.reason or "", + "attachment": self.get_attchment(per), + } emp.append(value) next = validator.get_page_pagination_next(page, count) url = "/rest_api/v2/permissions?approvel=%s&page=%s" % (approvel, next) if next else False @@ -112,11 +121,18 @@ class PermissionController(http.Controller): }) # if permission: - data = {'id': permission.id, 'date': str(permission.date), 'duration': permission.duration, - 'date_from': str(permission.date_from), 'date_to': str(permission.date_to), - 'early_exit': permission.early_exit, - 'state': validator.get_state_name(permission, permission.state), 'state_name': permission.state, - 'attachment': self.get_attchment(permission)} + data = { + "id": permission.id, + "date": str(permission.date), + "duration": permission.duration, + "date_from": str(permission.date_from), + "date_to": str(permission.date_to), + "early_exit": permission.early_exit, + "state": validator.get_state_name(permission, permission.state), + "state_name": permission.state, + "reason_msg": permission.reason or "", + "attachment": self.get_attchment(permission), + } return http_helper.response(message="Permission Created Successfully", data={'permission': [data]}) except Exception as e: http.request._cr.rollback() @@ -167,11 +183,18 @@ class PermissionController(http.Controller): 'res_id': permission.id, 'personal_permission_id': permission.id, }) - data = {'id': permission.id, 'date': str(permission.date), 'duration': permission.duration, - 'date_from': str(permission.date_from), 'date_to': str(permission.date_to), - 'early_exit': permission.early_exit, - 'state': validator.get_state_name(permission, permission.state), 'state_name': permission.state, - 'attachment': self.get_attchment(permission)} + data = { + "id": permission.id, + "date": str(permission.date), + "duration": permission.duration, + "date_from": str(permission.date_from), + "date_to": str(permission.date_to), + "early_exit": permission.early_exit, + "reason_msg": permission.reason or "", + "state": validator.get_state_name(permission, permission.state), + "state_name": permission.state, + "attachment": self.get_attchment(permission), + } return http_helper.response(message="Permission Edited Successfully", data={'permission': [data]}) else: return http_helper.response(code=400, @@ -208,11 +231,18 @@ class PermissionController(http.Controller): 'res_id': permission.id, 'personal_permission_id': permission.id, }) - data = {'id': permission.id, 'date': str(permission.date), 'duration': permission.duration, - 'date_from': str(permission.date_from), 'date_to': str(permission.date_to), - 'early_exit': permission.early_exit, - 'state': validator.get_state_name(permission, permission.state), 'state_name': permission.state, - 'attachment': self.get_attchment(permission)} + data = { + "id": permission.id, + "date": str(permission.date), + "duration": permission.duration, + "date_from": str(permission.date_from), + "date_to": str(permission.date_to), + "early_exit": permission.early_exit, + "reason_msg": permission.reason or "", + "state": validator.get_state_name(permission, permission.state), + "state_name": permission.state, + "attachment": self.get_attchment(permission), + } return http_helper.response(message="Get Permission Successfully", data={'permission': data}) else: return http_helper.response(code=400, success=False, message="Get Permission Not Fount", @@ -311,7 +341,6 @@ class PermissionController(http.Controller): if date_to: current_date = datetime.strptime(date_to, DEFAULT_SERVER_DATETIME_FORMAT) - current_month = datetime.strptime(date_to, DEFAULT_SERVER_DATETIME_FORMAT).month # date_from = current_date.strftime('%Y-0{0}-01'.format(current_month)) # date_to = current_date.strftime('%Y-0{0}-01'.format(current_month + 1)) @@ -333,10 +362,10 @@ class PermissionController(http.Controller): permission_date1 = datetime.strptime(str(rec.date_to), DEFAULT_SERVER_DATETIME_FORMAT).date() date_to_value1 = datetime.strptime(str(date_to), DEFAULT_SERVER_DATETIME_FORMAT).date() -# if rec.date_to and item.date_to: -# permission_date1 = datetime.strptime(rec.date_to, -# DEFAULT_SERVER_DATETIME_FORMAT).date() -# date_to_value1 = datetime.strptime(item.date_to, DEFAULT_SERVER_DATETIME_FORMAT).date() + # if rec.date_to and item.date_to: + # permission_date1 = datetime.strptime(rec.date_to, + # DEFAULT_SERVER_DATETIME_FORMAT).date() + # date_to_value1 = datetime.strptime(item.date_to, DEFAULT_SERVER_DATETIME_FORMAT).date() if permission_date1 == date_to_value1: # return http_helper.errcode(code=403, message=_('Sorry You Have Used All Your Permission In This Day you have one permission per a Day')) diff --git a/odex25_mobile/odex_mobile/http_helper.py b/odex25_mobile/odex_mobile/http_helper.py index f744e3df7..b519fc8eb 100644 --- a/odex25_mobile/odex_mobile/http_helper.py +++ b/odex25_mobile/odex_mobile/http_helper.py @@ -123,6 +123,7 @@ class HttpHelper: request.session.logout() - + def get_lable_selection(self, rec, field_name, state): + return dict(rec._fields[field_name]._description_selection(http.request.env)).get(state) http_helper = HttpHelper() diff --git a/odex25_mobile/odoo_dynamic_workflow_mobile/controllers/controllers.py b/odex25_mobile/odoo_dynamic_workflow_mobile/controllers/controllers.py index 6add2d861..1d19503c3 100644 --- a/odex25_mobile/odoo_dynamic_workflow_mobile/controllers/controllers.py +++ b/odex25_mobile/odoo_dynamic_workflow_mobile/controllers/controllers.py @@ -73,7 +73,7 @@ class RestApi(Controller): data.append(wkd) return http_helper.response(message="Successful", data=data) except (UserError, AccessError, ValidationError, Warning) as e: - error = str(e.name) + "\n" + str(e.value) + error = str(e.name) + "\n" + str(e) return http_helper.response(code=400, message=str(error), success=False) except Exception as e: return http_helper.response_500(str(e)) @@ -193,7 +193,7 @@ class RestApi(Controller): data.append(wkd) return http_helper.response(message="Successful", data=wkd) except (UserError, AccessError, ValidationError, Warning) as e: - error = str(e.name) + "\n" + str(e.value) + error = str(e.name) + "\n" + str(e) return http_helper.response(code=400, message=str(error), success=False) except Exception as e: return http_helper.response_500(str(e))