khaganat-web/pages/views.py

34 lines
1.1 KiB
Python
Raw Normal View History

from django.http import HttpResponseRedirect, Http404
2018-01-13 14:45:54 +00:00
from django.utils.translation import get_language
from django.views import generic
from django.urls import reverse
from nsfw import views as nsfw
2018-01-27 22:12:09 +00:00
from .models import Page, PageContent
2018-01-13 14:45:54 +00:00
def index(request):
default_page = Page.objects.filter(default=True).first()
2019-07-24 17:07:41 +00:00
return HttpResponseRedirect(reverse("page", args=(default_page.slug,)))
2018-01-13 14:45:54 +00:00
2018-01-27 22:12:09 +00:00
2018-01-13 14:45:54 +00:00
class PageView(generic.DetailView):
model = PageContent
2019-07-24 17:07:41 +00:00
context_object_name = "page"
template_name = "pages/page.html"
2018-01-13 14:45:54 +00:00
2018-06-02 14:03:13 +00:00
def dispatch(self, request, *args, **kwargs):
2018-06-02 19:43:23 +00:00
if self.get_object().page.is_nsfw:
if not nsfw.is_nsfw_allowed(request):
2018-06-02 14:03:13 +00:00
return nsfw.redirect(request)
2018-06-02 19:43:23 +00:00
else:
nsfw.alert(request)
2018-06-02 14:03:13 +00:00
return super().dispatch(request, *args, **kwargs)
2018-01-13 14:45:54 +00:00
def get_object(self):
obj = PageContent.objects.filter(
2019-07-24 17:07:41 +00:00
language=get_language(), page__slug=self.kwargs["slug"]
2018-01-27 22:12:09 +00:00
).first()
if obj is None:
2019-07-24 17:07:41 +00:00
raise Http404("Page not found: %s" % self.kwargs["slug"])
return obj