Merge branch 'simpler_router' into 'master'

Cleanup routing and remove middleware

See merge request bde/photo21!15
This commit is contained in:
erdnaxe 2021-10-23 15:37:21 +02:00
commit 04951b2ee4
7 changed files with 25 additions and 45 deletions

View file

@ -1,27 +0,0 @@
from django.http import HttpResponseRedirect
from django.conf import settings
import re
class LoginRequiredMiddleware:
"""
If user is not accessing the site from an authorized IP, force
authentification.
"""
def __init__(self, get_response):
"""Init middleware"""
self.get_response = get_response
self.whitelist_re = re.compile("^/accounts/.*$")
def __call__(self, request):
"""
If user is not authenticated and external, redirect to login view
before calling the view.
"""
if not request.user.is_authenticated:
if not self.whitelist_re.match(request.path_info):
return HttpResponseRedirect(settings.LOGIN_URL)
response = self.get_response(request)
return response

View file

@ -77,7 +77,6 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware', 'django.middleware.locale.LocaleMiddleware',
'django.contrib.sites.middleware.CurrentSiteMiddleware', 'django.contrib.sites.middleware.CurrentSiteMiddleware',
'photo21.middleware.LoginRequiredMiddleware',
] ]
ROOT_URLCONF = 'photo21.urls' ROOT_URLCONF = 'photo21.urls'

View file

@ -37,7 +37,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
</li> </li>
{% if perms.photologue.add_gallery %} {% if perms.photologue.add_gallery %}
<li class="nav-item"> <li class="nav-item">
{% url 'gallery-upload' as url %} {% url 'photologue:pl-gallery-upload' as url %}
<a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}">{% trans 'Upload' %}</a> <a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}">{% trans 'Upload' %}</a>
</li> </li>
{% endif %} {% endif %}

View file

@ -20,17 +20,16 @@ from django.conf.urls.static import static
from .views import IndexView, MediaAccess from .views import IndexView, MediaAccess
# photologue_custom overrides some photologue patterns
urlpatterns = [ urlpatterns = [
path('', IndexView.as_view(), name='index'), path('', IndexView.as_view(), name='index'),
path('photologue/', include('photologue_custom.urls')), path('', include('photologue_custom.urls', namespace='photologue')),
path('photologue/', include('photologue.urls', namespace='photologue')),
path('accounts/', include('allauth.urls')), path('accounts/', include('allauth.urls')),
path('i18n/', include('django.conf.urls.i18n')), path('i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('admin/doc/', include('django.contrib.admindocs.urls')), path('admin/doc/', include('django.contrib.admindocs.urls')),
] ]
# In production media are served through NGINX with X-Accel-Redirect
if settings.DEBUG: if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else: else:

View file

@ -39,7 +39,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% if gallery.extended.tags.all %} {% if gallery.extended.tags.all %}
<p class="text-muted"> <p class="text-muted">
Tags : {% for tag in gallery.extended.tags.all %} Tags : {% for tag in gallery.extended.tags.all %}
<a class="badge rounded-pill bg-dark text-decoration-none" href="{% url 'tag-detail' tag.slug %}">{{ tag }}</a> <a class="badge rounded-pill bg-dark text-decoration-none" href="{% url 'photologue:tag-detail' tag.slug %}">{{ tag }}</a>
{% endfor %} {% endfor %}
</p> </p>
{% endif %} {% endif %}
@ -49,14 +49,14 @@ SPDX-License-Identifier: GPL-3.0-or-later
<div class="card-header pb-0 border-bottom-0"> <div class="card-header pb-0 border-bottom-0">
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li class="nav-item"> <li class="nav-item">
{% url 'pl-gallery' gallery.slug as url %} {% url 'photologue:pl-gallery' gallery.slug as url %}
<a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}"> <a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}">
{% trans "All pictures" %} {% trans "All pictures" %}
</a> </a>
</li> </li>
{% for owner in owners %} {% for owner in owners %}
<li class="nav-item"> <li class="nav-item">
{% url 'pl-gallery-owner' slug=gallery.slug owner=owner.id as url %} {% url 'photologue:pl-gallery-owner' slug=gallery.slug owner=owner.id as url %}
<a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}"> <a class="nav-link {% if request.path_info == url %}active{% endif %}" href="{{ url }}">
{% if owner.get_full_name %}{{ owner.get_full_name }}{% else %}{{ owner.username }}{% endif %} {% if owner.get_full_name %}{{ owner.get_full_name }}{% else %}{{ owner.username }}{% endif %}
</a> </a>
@ -72,7 +72,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% endfor %} {% endfor %}
</div> </div>
<div class="card-footer"> <div class="card-footer">
<a href="{% url 'gallery-download' gallery.slug %}" class="btn btn-secondary btn-sm">{% trans 'Download all gallery' %}</a> <a href="{% url 'photologue:pl-gallery-download' gallery.slug %}" class="btn btn-secondary btn-sm">{% trans 'Download all gallery' %}</a>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -1,15 +1,19 @@
from django.urls import path, re_path from django.urls import path, re_path
from .views import (CustomGalleryArchiveIndexView, CustomGalleryDetailView, from .views import (CustomGalleryArchiveIndexView, CustomGalleryDetailView,
CustomGalleryYearArchiveView, GalleryDownload, CustomGalleryYearArchiveView, CustomPhotoDetailView,
GalleryUpload, TagDetail) GalleryDownload, GalleryUpload, TagDetail)
# Rather than using photologue default router, we redefine our own router
# with login and permission checks.
app_name = 'photologue'
urlpatterns = [ urlpatterns = [
path('tag/<slug:slug>/', TagDetail.as_view(), name='tag-detail'), path('tag/<slug:slug>/', TagDetail.as_view(), name='tag-detail'),
path('gallery/', CustomGalleryArchiveIndexView.as_view(), name='pl-gallery-archive'), path('gallery/', CustomGalleryArchiveIndexView.as_view(), name='pl-gallery-archive'),
re_path(r'^gallery/(?P<year>\d{4})/$', CustomGalleryYearArchiveView.as_view(), name='pl-gallery-archive-year'), re_path(r'^gallery/(?P<year>\d{4})/$', CustomGalleryYearArchiveView.as_view(), name='pl-gallery-archive-year'),
path('gallery/<slug:slug>/', CustomGalleryDetailView.as_view(), name='pl-gallery'), path('gallery/<slug:slug>/', CustomGalleryDetailView.as_view(), name='pl-gallery'),
path('gallery/<slug:slug>/<int:owner>/', CustomGalleryDetailView.as_view(), name='pl-gallery-owner'), path('gallery/<slug:slug>/<int:owner>/', CustomGalleryDetailView.as_view(), name='pl-gallery-owner'),
path('gallery/<slug:slug>/download/', GalleryDownload.as_view(), name='gallery-download'), path('gallery/<slug:slug>/download/', GalleryDownload.as_view(), name='pl-gallery-download'),
path('upload/', GalleryUpload.as_view(), name='gallery-upload'), path('photo/<slug:slug>/', CustomPhotoDetailView.as_view(), name='pl-photo'),
path('upload/', GalleryUpload.as_view(), name='pl-gallery-upload'),
] ]

View file

@ -16,7 +16,8 @@ from django.utils.text import slugify
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from photologue.models import Gallery, Photo from photologue.models import Gallery, Photo
from photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView from photologue.views import (GalleryArchiveIndexView, GalleryYearArchiveView,
PhotoDetailView)
from PIL import Image from PIL import Image
from taggit.models import Tag from taggit.models import Tag
@ -39,7 +40,7 @@ class TagDetail(LoginRequiredMixin, DetailView):
return context return context
class CustomGalleryArchiveIndexView(GalleryArchiveIndexView): class CustomGalleryArchiveIndexView(LoginRequiredMixin, GalleryArchiveIndexView):
""" """
Override to use event date Override to use event date
""" """
@ -47,7 +48,7 @@ class CustomGalleryArchiveIndexView(GalleryArchiveIndexView):
uses_datetime_field = False # Fix related object access uses_datetime_field = False # Fix related object access
class CustomGalleryYearArchiveView(GalleryYearArchiveView): class CustomGalleryYearArchiveView(LoginRequiredMixin, GalleryYearArchiveView):
""" """
Override to use event date Override to use event date
""" """
@ -55,7 +56,7 @@ class CustomGalleryYearArchiveView(GalleryYearArchiveView):
uses_datetime_field = False # Fix related object access uses_datetime_field = False # Fix related object access
class CustomGalleryDetailView(DetailView): class CustomGalleryDetailView(LoginRequiredMixin, DetailView):
""" """
Custom gallery detail view to filter on photo owner Custom gallery detail view to filter on photo owner
""" """
@ -102,13 +103,17 @@ class GalleryDownload(LoginRequiredMixin, DetailView):
return response return response
class CustomPhotoDetailView(LoginRequiredMixin, PhotoDetailView):
pass
class GalleryUpload(PermissionRequiredMixin, FormView): class GalleryUpload(PermissionRequiredMixin, FormView):
""" """
Form to upload new photos in a gallery Form to upload new photos in a gallery
""" """
form_class = UploadForm form_class = UploadForm
template_name = "photologue/upload.html" template_name = "photologue/upload.html"
success_url = reverse_lazy("gallery-upload") success_url = reverse_lazy("photologue:pl-gallery-upload")
permission_required = 'photologue.add_gallery' permission_required = 'photologue.add_gallery'
def form_valid(self, form): def form_valid(self, form):