Move media handling to new app

This commit is contained in:
Edgar P. Burkhart 2023-04-22 09:58:38 +02:00
parent ad18226974
commit a0d0b5d594
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
9 changed files with 38 additions and 23 deletions

2
.gitignore vendored
View file

@ -4,4 +4,4 @@ __pycache__
/nummi-git/
/pkg/
/src/
/nummi/media
/media

View file

@ -4,7 +4,6 @@ from . import views
urlpatterns = [
path("", views.IndexView.as_view(), name="index"),
path("media/user/<username>/<path:path>", views.MediaView.as_view(), name="media"),
path("login", views.LoginView.as_view(), name="login"),
path("logout", views.LogoutView.as_view(), name="logout"),
path("transactions", views.TransactionListView.as_view(), name="transactions"),

View file

@ -1,4 +1,3 @@
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.postgres.search import (
@ -7,12 +6,9 @@ from django.contrib.postgres.search import (
SearchVector,
TrigramSimilarity,
)
from django.core.exceptions import PermissionDenied
from django.db import models
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy
from django.views import View
from django.views.generic import (
CreateView,
DeleteView,
@ -22,7 +18,6 @@ from django.views.generic import (
UpdateView,
)
from django.views.generic.dates import MonthArchiveView
from django.views.static import serve
from .forms import (
AccountForm,
@ -417,22 +412,6 @@ class SearchView(TransactionListView):
return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]}
class MediaView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
_username = kwargs.get("username")
_path = kwargs.get("path")
if request.user.get_username() != _username:
raise PermissionDenied
if settings.DEBUG:
return serve(request, f"user/{_username}/{_path}", settings.MEDIA_ROOT)
_res = HttpResponse()
_res["Content-Type"] = ""
_res["X-Accel-Redirect"] = f"/internal/media/user/{_username}/{_path}"
return _res
class TransactionMonthView(UserMixin, MonthArchiveView):
template_name = "main/month/transaction.html"
model = Transaction

0
nummi/media/__init__.py Normal file
View file

6
nummi/media/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class MediaConfig(AppConfig):
name = "media"
verbose_name = "Media"

7
nummi/media/urls.py Normal file
View file

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("user/<username>/<path:path>", views.MediaView.as_view(), name="media"),
]

22
nummi/media/views.py Normal file
View file

@ -0,0 +1,22 @@
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.views import View
from django.views.static import serve
class MediaView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
_username = kwargs.get("username")
_path = kwargs.get("path")
if request.user.get_username() != _username:
raise PermissionDenied
if settings.DEBUG:
return serve(request, f"user/{_username}/{_path}", settings.MEDIA_ROOT)
_res = HttpResponse()
_res["Content-Type"] = ""
_res["X-Accel-Redirect"] = f"/internal/media/user/{_username}/{_path}"
return _res

View file

@ -45,6 +45,7 @@ CSRF_TRUSTED_ORIGINS = CONFIG.get("trusted_origins", ["http://localhost"])
INSTALLED_APPS = [
"main.apps.MainConfig",
"media.apps.MediaConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",

View file

@ -19,6 +19,7 @@ from django.urls import include, path
urlpatterns = i18n_patterns(
path("", include("main.urls")),
path("media/", include("media.urls")),
path("admin/", admin.site.urls),
prefix_default_language=False,
)