Сравнение пути URL в промежуточном программном обеспечении Django 2 - PullRequest
0 голосов
/ 21 февраля 2019

Мне нужно разработать промежуточное программное обеспечение Django и создать класс следующим образом:

import re
from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth import logout


class LoginRequiredMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        assert hasattr(request, 'user')

        path = request.path_info
        print(path)

        p = path.lstrip('/')
        compiled_login_url = re.compile(settings.LOGIN_URL)

        print(compiled_login_url.match(p))


        if not request.user.is_authenticated:
            if path == '/':
                print("==============> INDEX PAGE")

Теперь я хочу сравнить путь URL-адресов.Я должен определить, совпадают ли пути с settings.LOGIN_URL или нет.Я пытался использовать регулярные выражения, но они не помогли.

Вот мои URL

urlpatterns = [
              path('admin/', admin.site.urls),
              path('', views.index, name='index'),
              path('<slug:slug>/', include('app.urls')),
              path('<slug:slug>/accounts/login/', views.login, name='login'),
              path('<slug:slug>/accounts/logout/', views.logout, name='logout'),
              path('<slug:slug>/dashboard/', 'views.dashboard', name='dashboard'),
          ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Я хочу освободить login и logout от проверки в промежуточном программном обеспечении также мне нужно slug .

views.py

def user_login(request, slug):
    """
    Login user using email and password

    :param request: WSGI Request
    :param slug: to verify the facility
    :return: login page or redirect
    """

    context = {}
    try:
        facility = models.Facility.objects.get(slug=slug)
        context['facility'] = facility

    except models.Facility.DoesNotExist:
        return redirect('index')

    if request.method == "POST":
        email = request.POST['email']
        password = request.POST['password']

        user = authenticate(request, username=email, password=password)
        if user is not None:
            if user.profile.facility.slug == slug:
                login(request, user)
                return redirect('dashboard', slug=slug)
            else:
                context['error'] = "You don't belong to this facility"

        else:
            context['error'] = "Invalid email or password"

    return render(request, 'app/user/login.html', context)


@login_required
def dashboard(request, slug):
    """
    Display the dashboard of facility and list down bird
    view for the daily, weekly, monthly and yearly progress

    :param request:
    :return:
    """
    context = {}

    try:
        facility = models.Facility.objects.get(slug=slug)
        context['facility'] = facility
    except models.Facility.DoesNotExist:
        return render(request, 'app/404.html', status=404)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    {% for f in facilities %}
        <li><a href="{% url 'login' f.slug %}">{{ f.name }}</a></li>
    {% empty %}
        <li>No Facility</li>
    {% endfor %}
</ul>
</body>
</html>

1 Ответ

0 голосов
/ 21 февраля 2019

Вы можете добавить виды авторизации Django по одному к вашему urlpatterns, украсив каждое из них login_exempt.

ИЛИ

Если вы хотите переместитьпроверяя промежуточное ПО, вы можете попробовать что-то вроде:

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("myapp.urls")),
    path('accounts/', include('django.contrib.auth.urls', namespace="accounts"))
] 

middleware.py

class LoginRequiredMiddleware:            # <-- Custom Middleware
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)

    def process_view(self, request, view_func, view_args, view_kwargs):

        if getattr(view_func, 'login_exempt', False):
            return

        if 'accounts' in request.resolver_match.namespaces:
            return

Другими возможностями может быть проверка URL или имени представления в промежуточном программном обеспечении, но все же, возможно, самым чистым будет использование вашего декоратора и добавление необходимых представлений один за другим.

ИЛИ

Для этого вы можете использовать django-decorator-include .

$ pip install django-decorator-include

В вас project/url.py

from decorator_include import decorator_include
from project/custom_middleware import login_exempt


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("myapp.urls")),
    # do this 
    path('accounts/', decorator_include(login_exempt, 'django.contrib.auth.urls'))


]

Не забудьте добавить пакет в requirements.txt

...