from django.http import HttpResponseRedirect, Http404 from django.utils.translation import get_language from django.views import generic from django.urls import reverse from neluser import nsfw from .models import Page, PageContent def index(request): default_page = Page.objects.filter(default=True).first() return HttpResponseRedirect(reverse('page', args=(default_page.slug, ))) class PageView(generic.DetailView): model = PageContent context_object_name = 'page' template_name = 'pages/page.html' def dispatch(self, request, *args, **kwargs): if not nsfw.is_nsfw_allowed(request): if self.get_object().page.is_nsfw: return nsfw.redirect(request) return super().dispatch(request, *args, **kwargs) def get_object(self): obj = PageContent.objects.filter( language=get_language(), page__slug=self.kwargs['slug'] ).first() if obj is None: raise Http404('Page not found: %s' % self.kwargs['slug']) return obj