From bfe37bedea9b338fa4fc062aff98b58ed6909cb8 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 2 Jun 2018 16:03:13 +0200 Subject: [PATCH] Allow pages to be marked as NSFW --- pages/migrations/0002_page_is_nsfw.py | 18 ++++++++++++++++++ pages/models.py | 1 + pages/views.py | 7 +++++++ 3 files changed, 26 insertions(+) create mode 100644 pages/migrations/0002_page_is_nsfw.py diff --git a/pages/migrations/0002_page_is_nsfw.py b/pages/migrations/0002_page_is_nsfw.py new file mode 100644 index 0000000..63e3a1e --- /dev/null +++ b/pages/migrations/0002_page_is_nsfw.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.6 on 2018-06-02 13:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pages', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='page', + name='is_nsfw', + field=models.BooleanField(default=False), + ), + ] diff --git a/pages/models.py b/pages/models.py index cbf3cd1..d924449 100644 --- a/pages/models.py +++ b/pages/models.py @@ -6,6 +6,7 @@ import markdown class Page(models.Model): slug = models.CharField(max_length=200, unique=True) default = models.BooleanField(default=False) + is_nsfw = models.BooleanField(default=False) def __str__(self): return self.slug diff --git a/pages/views.py b/pages/views.py index a932658..5fbf820 100644 --- a/pages/views.py +++ b/pages/views.py @@ -2,6 +2,7 @@ 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 @@ -15,6 +16,12 @@ class PageView(generic.DetailView): 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(),