|
@@ -4,7 +4,7 @@ from collections import defaultdict
|
|
from django.http import HttpRequest, HttpResponse
|
|
from django.http import HttpRequest, HttpResponse
|
|
from django.urls import path
|
|
from django.urls import path
|
|
|
|
|
|
-from djangotools.common import response
|
|
|
|
|
|
+from djangotools.common import response, errors
|
|
from djangotools.common.path import PointPath, ConstPath, AttributePath, UrlPath, ModelPath
|
|
from djangotools.common.path import PointPath, ConstPath, AttributePath, UrlPath, ModelPath
|
|
from djangotools.common.route import RegisteredRoute, Post, Delete, Route, Get, Put
|
|
from djangotools.common.route import RegisteredRoute, Post, Delete, Route, Get, Put
|
|
from djangotools.common import models
|
|
from djangotools.common import models
|
|
@@ -28,6 +28,7 @@ class AutoPathManager:
|
|
return self
|
|
return self
|
|
|
|
|
|
def add(self, registerd_route, override=True):
|
|
def add(self, registerd_route, override=True):
|
|
|
|
+ print(registerd_route)
|
|
for method in registerd_route.route.method:
|
|
for method in registerd_route.route.method:
|
|
path = registerd_route.route.path.get_path()
|
|
path = registerd_route.route.path.get_path()
|
|
if method in self.routes[path]:
|
|
if method in self.routes[path]:
|
|
@@ -49,9 +50,18 @@ class AutoPathManager:
|
|
method = req.method
|
|
method = req.method
|
|
if method not in methods:
|
|
if method not in methods:
|
|
return response.serv_json_not_found(f"Method {method} on {req.path}")
|
|
return response.serv_json_not_found(f"Method {method} on {req.path}")
|
|
|
|
+
|
|
|
|
+ if "bind_user" in methods[method].options:
|
|
|
|
+ kwargs["user"] = req.user
|
|
|
|
+
|
|
ret = methods[method](req, *args, **kwargs)
|
|
ret = methods[method](req, *args, **kwargs)
|
|
if isinstance(ret, HttpResponse):
|
|
if isinstance(ret, HttpResponse):
|
|
return ret
|
|
return ret
|
|
|
|
+ if isinstance(ret, errors.CustomException):
|
|
|
|
+ return ret.get_error()
|
|
|
|
+ if isinstance(ret, Exception):
|
|
|
|
+ return response.serv_json(500, 500, err.__class__.__name__, str(err))
|
|
|
|
+
|
|
return response.serv_json_ok(ret)
|
|
return response.serv_json_ok(ret)
|
|
|
|
|
|
pathes.append(path(route, wrapper))
|
|
pathes.append(path(route, wrapper))
|
|
@@ -67,31 +77,40 @@ class AutoPath:
|
|
_key_ = "id"
|
|
_key_ = "id"
|
|
_registered_ = []
|
|
_registered_ = []
|
|
_inited = False
|
|
_inited = False
|
|
|
|
+ _bind_user_ = False
|
|
|
|
|
|
|
|
|
|
- def _auto_path_update(self, req):
|
|
|
|
-
|
|
|
|
- self.update(json.loads(req.body))
|
|
|
|
|
|
+ def _auto_path_update(self, req, **kwargs):
|
|
|
|
+ ret = self.update(json.loads(req.body))
|
|
self.save()
|
|
self.save()
|
|
- return self
|
|
|
|
|
|
+ return ret
|
|
|
|
|
|
- def _auto_path_get(self, req):
|
|
|
|
|
|
+ def _auto_path_get(self, req, **kwargs):
|
|
return self
|
|
return self
|
|
- def _auto_path_list(self, req):
|
|
|
|
|
|
+
|
|
|
|
+ def _auto_path_list(self, req, **kwargs):
|
|
|
|
+ if "user" in kwargs and hasattr(self, "user"):
|
|
|
|
+ return self.filter(user=kwargs["user"])
|
|
|
|
+
|
|
return self.all()
|
|
return self.all()
|
|
|
|
|
|
- def _auto_path_delete(self, req):
|
|
|
|
|
|
+ def _auto_path_delete(self, req, **kwargs):
|
|
self.delete()
|
|
self.delete()
|
|
return True
|
|
return True
|
|
- def _auto_path_create(self, req):
|
|
|
|
|
|
+
|
|
|
|
+ def _auto_path_create(self, req, **kwargs):
|
|
try:
|
|
try:
|
|
- return self.create(**json.loads(req.body))
|
|
|
|
|
|
+ return self.create(**json.loads(req.body), **kwargs)
|
|
except Exception as err:
|
|
except Exception as err:
|
|
return err
|
|
return err
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
def init(cls, manager):
|
|
def init(cls, manager):
|
|
if cls._inited: return
|
|
if cls._inited: return
|
|
|
|
+ route_options = {}
|
|
|
|
+ if cls._bind_user_:
|
|
|
|
+ route_options["bind_user"] = True
|
|
|
|
+
|
|
|
|
|
|
root = cls._path_
|
|
root = cls._path_
|
|
if root is None:
|
|
if root is None:
|
|
@@ -99,9 +118,9 @@ class AutoPath:
|
|
|
|
|
|
root = root / ConstPath(cls.__name__.lower()) / AttributePath(cls, cls._key_)
|
|
root = root / ConstPath(cls.__name__.lower()) / AttributePath(cls, cls._key_)
|
|
|
|
|
|
- manager.add(RegisteredRoute(Post(root), cls._auto_path_update))
|
|
|
|
- manager.add(RegisteredRoute(Delete(root), cls._auto_path_delete))
|
|
|
|
- manager.add(RegisteredRoute(Get(root), cls._auto_path_get))
|
|
|
|
|
|
+ manager.add(RegisteredRoute(Post(root, **route_options), cls._auto_path_update))
|
|
|
|
+ manager.add(RegisteredRoute(Delete(root, **route_options), cls._auto_path_delete))
|
|
|
|
+ manager.add(RegisteredRoute(Get(root, **route_options), cls._auto_path_get))
|
|
|
|
|
|
for k in dir(cls):
|
|
for k in dir(cls):
|
|
v = getattr(cls, k)
|
|
v = getattr(cls, k)
|
|
@@ -113,8 +132,8 @@ class AutoPath:
|
|
_root.liste = root.liste[:-1]
|
|
_root.liste = root.liste[:-1]
|
|
_root = _root / ModelPath(cls)
|
|
_root = _root / ModelPath(cls)
|
|
|
|
|
|
- manager.add(RegisteredRoute(Get(_root), cls._auto_path_list, True))
|
|
|
|
- manager.add(RegisteredRoute(Put(_root), cls._auto_path_create, True))
|
|
|
|
|
|
+ manager.add(RegisteredRoute(Get(_root, **route_options), cls._auto_path_list, True))
|
|
|
|
+ manager.add(RegisteredRoute(Put(_root, **route_options), cls._auto_path_create, True))
|
|
|
|
|
|
for k in dir(cls.objects):
|
|
for k in dir(cls.objects):
|
|
v = getattr(cls.objects, k)
|
|
v = getattr(cls.objects, k)
|