diff --git a/payroll/api.py b/payroll/api.py index 98f2b2c7..af4e9292 100644 --- a/payroll/api.py +++ b/payroll/api.py @@ -1,8 +1,8 @@ import json from django.conf import settings -from django.http import JsonResponse from django.core.exceptions import ValidationError +from django.http import JsonResponse from core.utils.generic_helpers import get_previous_months_data from payroll.views import EditPayrollBaseView @@ -88,30 +88,31 @@ def post(self, request, *args, **kwargs): ) return JsonResponse({}) - + + class EmployeeNotesApi(EditPayrollBaseView): def post(self, request, *args, **kwargs): try: data = json.loads(request.body) if not data: - return JsonResponse( - {"error": "Missing request body"}, - status=400 - ) - notes = data.get('notes') - employee_no = data.get('employee_no') + return JsonResponse({"error": "Missing request body"}, status=400) + notes = data.get("notes") + employee_no = data.get("employee_no") if not notes or not employee_no: return JsonResponse( - {"error": "Both 'notes' and 'employee_no' are required"}, - status=400 + {"error": "Both 'notes' and 'employee_no' are required"}, status=400 ) employee_data = payroll_service.get_employee_data( self.cost_centre, self.financial_year, ) employee = next( - (item for item in employee_data if str(item["employee_no"]) == employee_no), + ( + item + for item in employee_data + if str(item["employee_no"]) == employee_no + ), None, ) if employee: @@ -123,17 +124,10 @@ def post(self, request, *args, **kwargs): ) return JsonResponse({}, status=204) except json.JSONDecodeError: - return JsonResponse( - {"error": "Invalid JSON format"}, - status=400 - ) - except ValidationError as e: - return JsonResponse( - {"error": str(e)}, - status=400 - ) + return JsonResponse({"error": "Invalid JSON format"}, status=400) + except ValidationError: + return JsonResponse({"error": "Invalid data provided"}, status=400) except Exception: return JsonResponse( - {"error": "An error occurred while processing the request"}, - status=500 - ) \ No newline at end of file + {"error": "An error occurred while processing the request"}, status=500 + )