|
@@ -6,10 +6,61 @@ from django.urls import path
|
|
|
|
|
|
from djangotools.common import response
|
|
|
from djangotools.common.path import PointPath, ConstPath, AttributePath, UrlPath, ModelPath
|
|
|
-from djangotools.views.route import RegisteredRoute, Post, Delete, Route, Get, Put
|
|
|
+from djangotools.common.route import RegisteredRoute, Post, Delete, Route, Get, Put
|
|
|
from djangotools.common import models
|
|
|
|
|
|
|
|
|
+
|
|
|
+class AutoPathManager:
|
|
|
+ _ref = None
|
|
|
+
|
|
|
+ def __init__(self, app=None):
|
|
|
+ self._models = {}
|
|
|
+ for name, model in models.iter_models_items():
|
|
|
+ if isinstance(model, type) and issubclass(model, AutoPath):
|
|
|
+ self._models[model.__name__] = model
|
|
|
+ self.routes = defaultdict(dict)
|
|
|
+
|
|
|
+
|
|
|
+ def _init(self):
|
|
|
+ for name, model in self._models.items():
|
|
|
+ model.init(self)
|
|
|
+ return self
|
|
|
+
|
|
|
+ def add(self, registerd_route, override=True):
|
|
|
+ for method in registerd_route.route.method:
|
|
|
+ path = registerd_route.route.path.get_path()
|
|
|
+ if method in self.routes[path]:
|
|
|
+ if not override:
|
|
|
+ raise ValueError(f"La route {path} est déja défini avec {method}")
|
|
|
+ self.routes[path][method] = registerd_route
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_instance(cls):
|
|
|
+ if cls._ref is None:
|
|
|
+ cls._ref = AutoPathManager()
|
|
|
+ cls._ref._init()
|
|
|
+ return cls._ref
|
|
|
+
|
|
|
+ def get_pathes(self):
|
|
|
+ pathes = []
|
|
|
+ for route, methods in self.routes.items():
|
|
|
+ def wrapper(req : HttpRequest, *args, methods=methods, **kwargs):
|
|
|
+ method = req.method
|
|
|
+ if method not in methods:
|
|
|
+ return response.serv_json_not_found(f"Method {method} on {req.path}")
|
|
|
+ ret = methods[method](req, *args, **kwargs)
|
|
|
+ if isinstance(ret, HttpResponse):
|
|
|
+ return ret
|
|
|
+ return response.serv_json_ok(ret)
|
|
|
+
|
|
|
+ pathes.append(path(route, wrapper))
|
|
|
+ return pathes
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
class AutoPath:
|
|
|
|
|
|
_path_ = None
|
|
@@ -75,52 +126,3 @@ class AutoPath:
|
|
|
cls._inited = True
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-class AutoPathManager:
|
|
|
- _ref = None
|
|
|
-
|
|
|
- def __init__(self, app=None):
|
|
|
- self._models = {}
|
|
|
- for name, model in models.iter_models_items():
|
|
|
- if isinstance(model, type) and issubclass(model, AutoPath):
|
|
|
- self._models[model.__name__] = model
|
|
|
- self.routes = defaultdict(dict)
|
|
|
-
|
|
|
-
|
|
|
- def _init(self):
|
|
|
- for name, model in self._models.items():
|
|
|
- model.init(self)
|
|
|
- return self
|
|
|
-
|
|
|
- def add(self, registerd_route, override=True):
|
|
|
- for method in registerd_route.route.method:
|
|
|
- path = registerd_route.route.path.get_path()
|
|
|
- if method in self.routes[path]:
|
|
|
- if not override:
|
|
|
- raise ValueError(f"La route {path} est déja défini avec {method}")
|
|
|
- self.routes[path][method] = registerd_route
|
|
|
-
|
|
|
- @classmethod
|
|
|
- def get_instance(cls):
|
|
|
- if cls._ref is None:
|
|
|
- cls._ref = AutoPathManager()
|
|
|
- cls._ref._init()
|
|
|
- return cls._ref
|
|
|
-
|
|
|
- def get_pathes(self):
|
|
|
- pathes = []
|
|
|
- for route, methods in self.routes.items():
|
|
|
- def wrapper(req : HttpRequest, *args, methods=methods, **kwargs):
|
|
|
- method = req.method
|
|
|
- if method not in methods:
|
|
|
- return response.serv_json_not_found(f"Method {method} on {req.path}")
|
|
|
- ret = methods[method](req, *args, **kwargs)
|
|
|
- if isinstance(ret, HttpResponse):
|
|
|
- return ret
|
|
|
- return response.serv_json_ok(ret)
|
|
|
-
|
|
|
- pathes.append(path(route, wrapper))
|
|
|
- return pathes
|