From 797c944041b849dcb0c4cc97da214b5dff7af963 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Thu, 10 Aug 2023 13:49:55 -0500 Subject: [PATCH] feat: allows to audit the create_external_certificates method --- eox_nelp/api_clients/certificates.py | 57 +++++++++++++++++----------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/eox_nelp/api_clients/certificates.py b/eox_nelp/api_clients/certificates.py index c61be715..79bff579 100644 --- a/eox_nelp/api_clients/certificates.py +++ b/eox_nelp/api_clients/certificates.py @@ -7,6 +7,13 @@ from eox_nelp.api_clients import AbstractBasicAuthApiClient +try: + from eox_audit_model.decorators import audit_method +except ImportError: + def audit_method(action): # pylint: disable=unused-argument + """Identity audit_method""" + return lambda x: x + class ExternalCertificatesApiClient(AbstractBasicAuthApiClient): """Allow to perform multiple external certificates operations.""" @@ -46,27 +53,31 @@ def create_external_certificate(self, certificate_data): Raise: KeyError: This will be raised when the mandatory are excluded in the certificate data. """ - path = "Certificates" - user = certificate_data["user"] - payload = { - "reference_id": f"nelc-openedx-lms-cert-{certificate_data['id']}", - "date": { - "issuance": certificate_data["created_at"], - "expiration": None, - }, - "individual": { - "name_en": user["english_name"], - "name_ar": user["arabic_name"], - "id": user["national_id"], - "id_type": "saudi", - }, - "group_code": certificate_data["group_code"], - "certificate_type": "completion", # What types do we have ? - "score": { - "value": certificate_data["grade"], - "type": "percentage" - }, - "metadata": getattr(settings, "EXTERNAL_CERTIFICATES_METADATA", {}), - } + @audit_method(action="Create External Certificate") + def create_external_certificate_request(certificate_data): + """This is a wrapper that allows to make audit-able the create_external_certificate method.""" + path = "Certificates" + user = certificate_data["user"] + payload = { + "reference_id": f"nelc-openedx-lms-cert-{certificate_data['id']}", + "date": { + "issuance": certificate_data["created_at"], + "expiration": None, + }, + "individual": { + "name_en": user["english_name"], + "name_ar": user["arabic_name"], + "id": user["national_id"], + "id_type": "saudi", + }, + "group_code": certificate_data["group_code"], + "certificate_type": "completion", # What types do we have ? + "score": { + "value": certificate_data["grade"], + "type": "percentage" + }, + "metadata": getattr(settings, "EXTERNAL_CERTIFICATES_METADATA", {}), + } + return self.make_post(path, payload) - return self.make_post(path, payload) + return create_external_certificate_request(certificate_data)