def list(self, request, *args, **kwargs): """Retrieve a list of objects.""" response = super(CustomListModelMixin, self).list(request, *args, **kwargs) return APIResponse(data=response.data, status=response.status_code, headers=response._headers)
def retrieve(self, request, *args, **kwargs): """Retrieve an object.""" response = super(CustomRetrieveModelMixin, self).retrieve(request, *args, **kwargs) return APIResponse(data=response.data, status=response.status_code, headers=response._headers)
def drf_exception_handler(exception, context): """Custom exception handler for DRF.""" request = context["request"] error_data = ExceptionHandler().handle_exception(request, exception) return APIResponse(error_data, is_success=False, status=error_data["status"])
def create(self, request, *args, **kwargs): """Create an object.""" response = super(CustomCreateModelMixin, self).create(request, *args, **kwargs) return APIResponse( data=response.data, status=get_status_code(response), headers=response._headers, )
def json_permission_denied(request, exception, *args, **kwargs): """Override 403 error to return a JSON Error""" if not is_api_request(request): return permission_denied(request, exception, *args, **kwargs) context = { "request_path": quote(request.path), "exception": get_exception_message(exception), } return APIResponse(context, is_success=False, status=status.HTTP_403_FORBIDDEN)
def json_page_not_found(request, exception, *args, **kwargs): """Override 404 error to return a JSON Error""" if not is_api_request(request): return page_not_found(request, exception, *args, **kwargs) context = { "request_path": quote(request.path), "exception": get_exception_message(exception), } return APIResponse(context, is_success=False, status=status.HTTP_404_NOT_FOUND)
def __call__(self, request): """Middleware call method.""" if not is_api_request(request): return self.get_response(request) request_body = request.body requested_at = datetime.utcnow() response = self.get_response(request) path = request.get_full_path() method = request.method status_code = response.status_code response_data = self.get_response_data(request, response) request_body = self.get_request_data(request, request_body) response_time = datetime.utcnow() - requested_at response_time = round(response_time.total_seconds() * 1000) response_data = json.loads( json.dumps(response_data, cls=DjangoJSONEncoder)) user = request.user if request.user.is_authenticated else None headers = self.get_headers(request) self.mask_data(request_body, response_data, headers) raw_agent, pretty_agent = get_user_agent(headers) try: logger.info( "access_log", extra={ "user": user.username if user else None, "path": path, "method": method, "request_data": request_body, "requested_at": requested_at, "response_time": int(response_time), "status_code": status_code, "response_data": response_data, "ip": get_ip(headers), "raw_user_agent": raw_agent, "user_agent": pretty_agent, "headers": headers, }, ) except Exception as e: # noqa logger.error(e, exc_info=True) if getattr(request, "error_data", None): return APIResponse(request.error_data, is_success=False, status=response.status_code) return response
def destroy(self, request, *args, **kwargs): """Delete an object.""" response = super(CustomDestroyModelMixin, self).destroy(request, *args, **kwargs) return APIResponse(data=response.data, status=get_status_code(response))
def update(self, request, *args, **kwargs): """Update an object.""" response = super(CustomUpdateModelMixin, self).update(request, *args, **kwargs) return APIResponse(data=response.data, status=get_status_code(response))
def patch(self, request, access_req_id, *args, **kwargs): superadmin = request.user data = request.data req = AccessRequestHandler().take_decision(access_req_id, superadmin, data) return APIResponse(req)
def get(self, request, *args, **kwargs): data = AccessRequestHandler().get_request_list({"superadmin_id": request.user}) return APIResponse(data)
def post(self, request, *args, **kwargs): admin = request.user data = request.data req = AccessRequestHandler().create(admin, data) return APIResponse(req)
def health(request): return APIResponse({"status": "healthy"})