""" Django views file. Responsible of route URL requested to the destination endpoint. """ from django.views.decorators.csrf import csrf_exempt from tools.exceptions import SystemException from tools.service.service import Service from .calls import CALLS MethodNotAvailable = type('MethodNotAvailable', (SystemException, ), {}) @csrf_exempt def wrapper(request, service_str, **kwargs): """ Method that wrap the url path to the function to launch. It use the new system through app.calls.*.{METHOD_NAME}_REQUEST dictionary. Parameters ---------- request: django.http.HttpRequest Http request object. service_str: str Last path of the url. eg, "/AI/skyNet/detection" -> service_str = 'detection' kwargs: dict Optionla key arguments. Returns ------- any: Result of method """ http_method = request.method service = CALLS[service_str] service_dict = service.__dict__.copy() if "{}_REQUEST".format(http_method.upper()) not in service_dict.keys(): return MethodNotAvailable('METHOD {} not available'.format(http_method)) service_conf = service_dict["{}_REQUEST".format(http_method.upper())] del service_dict service = Service(service_conf) service_check = service.check() if service_check.__class__ == Service.SERVICE_EXCEPTION: return service_check return service.run(request)