|
@@ -1,11 +1,14 @@
|
|
|
+import traceback
|
|
|
+
|
|
|
from django.contrib.auth.models import User
|
|
|
from django.db.models import Manager, Model, QuerySet
|
|
|
from django.db.models.base import ModelBase
|
|
|
|
|
|
from djangotools.common import response
|
|
|
-from djangotools.common.path import PointPath, ModelPath, AttributePath
|
|
|
-from djangotools.model.serializedmodel import SerializableModel
|
|
|
-from djangotools.view.utils import debug_login
|
|
|
+from djangotools.common.errors import CustomException
|
|
|
+from djangotools.common.path import PointPath, ModelPath, AttributePath, ConstPath
|
|
|
+from djangotools.models.serializedmodel import SerializableModel
|
|
|
+from djangotools.views.utils import debug_login
|
|
|
|
|
|
|
|
|
class Route:
|
|
@@ -14,9 +17,12 @@ class Route:
|
|
|
DELETE = "DELETE"
|
|
|
PUT = "PUT"
|
|
|
method = {GET, POST, DELETE, PUT}
|
|
|
- def __init__(self, route=None, method=None):
|
|
|
+ def __init__(self, route=None, method=None, **options):
|
|
|
+ self.options = options
|
|
|
self.method = method if method is not None else self.method
|
|
|
if isinstance(method, str): self.method = {method}
|
|
|
+ if isinstance(route, str):
|
|
|
+ route = ConstPath(route)
|
|
|
self.path = route or PointPath()
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
@@ -30,21 +36,46 @@ class Route:
|
|
|
raise Exception()
|
|
|
|
|
|
|
|
|
+def request_wrapper(fct):
|
|
|
+ def wrapper(request, *args, **kwargs):
|
|
|
+ try:
|
|
|
+ return fct(request, *args, **kwargs)
|
|
|
+ except CustomException as err:
|
|
|
+ return err.get_error()
|
|
|
+ except Exception as err:
|
|
|
+ traceback.print_exc()
|
|
|
+ return response.serv_json(500, 500, err.__class__.__name__, str(err))
|
|
|
+
|
|
|
+ return wrapper
|
|
|
|
|
|
class RegisteredRoute:
|
|
|
+
|
|
|
+
|
|
|
_default_user_id_ = 1
|
|
|
- def __init__(self, path : Route, callback, is_manager=False, is_method=True, **options):
|
|
|
+ def __init__(self, path : Route, callback, is_manager=False, is_method=True):
|
|
|
+ """
|
|
|
+
|
|
|
+ :param path:
|
|
|
+ :param callback:
|
|
|
+ :param is_manager:
|
|
|
+ :param is_method:
|
|
|
+ :param options:
|
|
|
+ need_auth: active l'authentitficatyion
|
|
|
+ no_wrap: ne wrappe pas les callbacks (pour les captures d'exception)
|
|
|
+ """
|
|
|
self.route = path if path is not None else Route()
|
|
|
- self.callback = callback
|
|
|
+
|
|
|
self.is_manager = is_manager
|
|
|
self.is_method = is_method
|
|
|
- self.options = options
|
|
|
+ self.options = self.route.options
|
|
|
+ if not "no_wrap" in self.options: callback = request_wrapper(callback)
|
|
|
+ self.callback = callback
|
|
|
|
|
|
- def __call__(self, req, *args, **kwargs):
|
|
|
+ def _call(self, req, *args, **kwargs):
|
|
|
obj = None
|
|
|
debug_login(req, self._default_user_id_)
|
|
|
user = req.user
|
|
|
- if "need_auth" and not user.is_authenticated:
|
|
|
+ if self.options.get("need_auth", False) and not user.is_authenticated:
|
|
|
return response.serv_json_not_logged()
|
|
|
|
|
|
|
|
@@ -78,7 +109,10 @@ class RegisteredRoute:
|
|
|
if self.is_method:
|
|
|
ret = self.callback(obj, req, *args, **kwargs)
|
|
|
else:
|
|
|
- ret = self.callback(req, obj, *args, **kwargs)
|
|
|
+ if obj is None:
|
|
|
+ ret = self.callback(req, *args, **kwargs)
|
|
|
+ else:
|
|
|
+ ret = self.callback(req, obj, *args, **kwargs)
|
|
|
|
|
|
if isinstance(ret, QuerySet):
|
|
|
return [x.serialize() for x in ret]
|
|
@@ -89,6 +123,12 @@ class RegisteredRoute:
|
|
|
return ret
|
|
|
|
|
|
|
|
|
+ def __call__(self, req, *args, **kwargs):
|
|
|
+ if "no_wrap" in self.options:
|
|
|
+ return self._call(req, *args, **kwargs)
|
|
|
+ return request_wrapper(self._call)(req, *args, **kwargs)
|
|
|
+
|
|
|
+
|
|
|
|
|
|
def copy(self):
|
|
|
return RegisteredRoute(self.route, self.callback, is_manager=self.is_manager,
|