|
| 1 | +"""Wrapper file for eox-audit-model""" |
| 2 | +from importlib import import_module, util |
| 3 | + |
| 4 | +try: |
| 5 | + eox_audit_decorator = import_module('eox_audit_model.decorators') |
| 6 | + audit_method = eox_audit_decorator.audit_method |
| 7 | +except ImportError: |
| 8 | + pass |
| 9 | + |
| 10 | + |
| 11 | +def audit_api_wrapper(action='', data_filter=None): |
| 12 | + """This decorator wraps the functionality of audit_method in order to |
| 13 | + work with django API view methods,also this allows to filter the data that will be |
| 14 | + stored in the data base. |
| 15 | +
|
| 16 | + Example |
| 17 | +
|
| 18 | + class YourAPIView(APIView): |
| 19 | +
|
| 20 | + @audit_api_wrapper(action='Get my items', data_filter=['username', 'location']) |
| 21 | + def get(self, request, *args, **kwargs): |
| 22 | + ... |
| 23 | + """ |
| 24 | + def decorator(func): |
| 25 | + def wrapper(*args, **kwargs): |
| 26 | + if not util.find_spec('eox_audit_model'): |
| 27 | + return func(*args, **kwargs) |
| 28 | + |
| 29 | + request = args[1] |
| 30 | + audit_data = request.data if request.data else request.query_params |
| 31 | + |
| 32 | + if data_filter: |
| 33 | + audit_data = {key: value for key, value in audit_data.items() if key in data_filter} |
| 34 | + |
| 35 | + @audit_method(action=action) |
| 36 | + def eox_core_api_method(audit_data): |
| 37 | + """This method is just a wrapper in order to capture the input data""" |
| 38 | + return func(*args, **kwargs) |
| 39 | + |
| 40 | + return eox_core_api_method(audit_data) |
| 41 | + |
| 42 | + return wrapper |
| 43 | + |
| 44 | + return decorator |
0 commit comments