Browse Source

- Add command and subcommand argument

fanch 1 year ago
parent
commit
78e462cb3f

+ 3 - 1
src/djangotools/common/__init__.py

@@ -1,2 +1,4 @@
 from djangotools.common.module_loader import iter_module, iter_module_content
 from djangotools.common.module_loader import iter_module, iter_module_content
-from djangotools.common.models import BaseSerializationModel
+from djangotools.common.models import BaseSerializationModel
+from djangotools.common.url import redirect, get_url
+

+ 2 - 2
src/djangotools/common/route.py

@@ -4,14 +4,14 @@ from django.conf import settings
 from django.contrib.auth.models import User
 from django.contrib.auth.models import User
 from django.db.models import Manager, Model, QuerySet
 from django.db.models import Manager, Model, QuerySet
 from django.db.models.base import ModelBase
 from django.db.models.base import ModelBase
-from django.shortcuts import redirect
 
 
-from djangotools.common import response, BaseSerializationModel
+from djangotools.common import response, BaseSerializationModel, redirect
 from djangotools.common.errors import CustomException
 from djangotools.common.errors import CustomException
 from djangotools.common.path import PointPath, ModelPath, AttributePath, ConstPath
 from djangotools.common.path import PointPath, ModelPath, AttributePath, ConstPath
 from djangotools.common.loggin import debug_login
 from djangotools.common.loggin import debug_login
 
 
 
 
+
 class Route:
 class Route:
     GET = "GET"
     GET = "GET"
     POST = "POST"
     POST = "POST"

+ 12 - 0
src/djangotools/common/url.py

@@ -0,0 +1,12 @@
+from django.conf import settings
+from django.shortcuts import redirect as _redirect
+from pathlib import Path
+base = settings.BASE_URL
+if base[0] != "/": base = "/"+base
+def get_url(url):
+    if url[0] == "/": url = url[1:]
+    return str(Path(base, url))[1:]
+
+def redirect(to, *args, permanent=False, **kwargs):
+    to = "/"+get_url(to)
+    return _redirect(to, *args, permanent=permanent, **kwargs)

+ 1 - 0
src/djangotools/config/base.py

@@ -74,6 +74,7 @@ class ConfigLoader:
         ("LOGIN_TEMPLATE", "login.html"),
         ("LOGIN_TEMPLATE", "login.html"),
         ("ROOT_TEMPLATE", "index.html"),
         ("ROOT_TEMPLATE", "index.html"),
         ("LOGIN_CONTEXT", "login"),
         ("LOGIN_CONTEXT", "login"),
+        ("BASE_URL", "/"),
         ("LOGIN_URL", "login"),
         ("LOGIN_URL", "login"),
         ("LOGOUT_URL", "disconnect"),
         ("LOGOUT_URL", "disconnect"),
         ("AUTH_URL", "auth"),
         ("AUTH_URL", "auth"),

+ 7 - 1
src/djangotools/views/context/base.py

@@ -1,5 +1,5 @@
 from django.conf import settings
 from django.conf import settings
-from django.shortcuts import redirect
+
 
 
 from djangotools.common.errors import UnauthorizedException
 from djangotools.common.errors import UnauthorizedException
 import datetime
 import datetime
@@ -16,6 +16,7 @@ from djangotools.common.errors import UnauthorizedException
 
 
 from django.conf import settings
 from django.conf import settings
 from djangotools.common.module_loader import iter_module_content
 from djangotools.common.module_loader import iter_module_content
+from djangotools.common import get_url, redirect
 
 
 allow_all=False
 allow_all=False
 
 
@@ -30,6 +31,10 @@ def get_settings(request):
                 "url" : f"http://localhost:{request.META['SERVER_PORT']}",
                 "url" : f"http://localhost:{request.META['SERVER_PORT']}",
             }
             }
 
 
+
+base_url = settings.BASE_URL
+if not base_url.startswith("/"): base_url = "/"+base_url
+#if base_url.endswith("/"): base_url = base_url[:-1]
 class ContextData:
 class ContextData:
     page = None
     page = None
     need_auth = True
     need_auth = True
@@ -79,6 +84,7 @@ class ContextData:
             "context" : {
             "context" : {
                 "page" : self.page,
                 "page" : self.page,
                 "data" : data,
                 "data" : data,
+                "base_url" : base_url,
                 "user" : {
                 "user" : {
                     "authenticated" : request.user.is_authenticated,
                     "authenticated" : request.user.is_authenticated,
                     "name" : request.user.username,
                     "name" : request.user.username,

+ 3 - 3
src/djangotools/views/misc.py

@@ -7,7 +7,7 @@ from django.contrib.auth.models import User
 from django.http import HttpResponseRedirect
 from django.http import HttpResponseRedirect
 from django.shortcuts import render
 from django.shortcuts import render
 
 
-from djangotools.common import response
+from djangotools.common import response, get_url
 from djangotools.common.errors import CustomException, UnauthorizedException
 from djangotools.common.errors import CustomException, UnauthorizedException
 from djangotools.views.context.base import get_context_class
 from djangotools.views.context.base import get_context_class
 from djangotools.views.router import Router
 from djangotools.views.router import Router
@@ -81,7 +81,7 @@ Router.get(settings.LOGIN_URL, need_auth=False, redirect_login=True)(render_page
 @Router.post(settings.AUTH_URL, need_auth=False)
 @Router.post(settings.AUTH_URL, need_auth=False)
 def auth(request):
 def auth(request):
     params = request.POST
     params = request.POST
-    redirect = request.GET.get("redirect", "/")
+    redirect = request.GET.get("redirect", "")
     user = params.get("login", None)
     user = params.get("login", None)
     password = params.get("password", None)
     password = params.get("password", None)
     if request.user and request.user.is_authenticated:
     if request.user and request.user.is_authenticated:
@@ -92,7 +92,7 @@ def auth(request):
         user = authenticate(request, username=user, password=password)
         user = authenticate(request, username=user, password=password)
         if user is not None:
         if user is not None:
             login(request, user)
             login(request, user)
-            return HttpResponseRedirect(redirect)
+            return HttpResponseRedirect("/"+settings.BASE_URL)
     users = [k.username for k in User.objects.all()]
     users = [k.username for k in User.objects.all()]
     return _render(request, settings.LOGIN_CONTEXT, settings.LOGIN_TEMPLATE, data={"error" : f"Mauvais login ou mot de passe"})
     return _render(request, settings.LOGIN_CONTEXT, settings.LOGIN_TEMPLATE, data={"error" : f"Mauvais login ou mot de passe"})
 
 

+ 1 - 0
src/djangotools/views/router.py

@@ -81,6 +81,7 @@ class _Router:
                 return response.serv_json_ok(ret)
                 return response.serv_json_ok(ret)
 
 
             pathes.append(path(route, wrapper))
             pathes.append(path(route, wrapper))
+
         return pathes + self._raw_pathes
         return pathes + self._raw_pathes