diff --git a/khaganat/static/khaganat/css/khaganat.css b/khaganat/static/khaganat/css/khaganat.css index 371dade..469c22e 100644 --- a/khaganat/static/khaganat/css/khaganat.css +++ b/khaganat/static/khaganat/css/khaganat.css @@ -40,3 +40,7 @@ html,body { font-style: italic; color: #9073FF; } + +.messages { + margin-top: 10px; +} diff --git a/khaganat/templates/khaganat/base.html b/khaganat/templates/khaganat/base.html index 8fb83aa..4e6afdd 100644 --- a/khaganat/templates/khaganat/base.html +++ b/khaganat/templates/khaganat/base.html @@ -21,7 +21,20 @@ {% navbar %}
- {% block content %}{% endblock %} + {% if messages %} +
+ {% for message in messages %} + + {% endfor %} +
+ {% endif %} + {% block content %}{% endblock %}
diff --git a/logs/views.py b/logs/views.py index fbf9318..ae30534 100644 --- a/logs/views.py +++ b/logs/views.py @@ -111,9 +111,11 @@ class EntriesView(generic.ListView): return False def dispatch(self, request, *args, **kwargs): - if not nsfw.is_nsfw_allowed(request): - if self.is_nsfw(): + if self.is_nsfw(): + if not nsfw.is_nsfw_allowed(request): return nsfw.redirect(request) + else: + nsfw.alert(request) return super().dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): diff --git a/neluser/locale/en/LC_MESSAGES/django.po b/neluser/locale/en/LC_MESSAGES/django.po index 2df83b5..ccf90f0 100644 --- a/neluser/locale/en/LC_MESSAGES/django.po +++ b/neluser/locale/en/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: 1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-05-27 18:28+0200\n" +"POT-Creation-Date: 2018-06-02 21:36+0200\n" "PO-Revision-Date: 2018-02-04 01:03+0100\n" "Last-Translator: Khaganat \n" "Language-Team: Khaganat \n" @@ -140,6 +140,16 @@ msgstr "" msgid "1 day" msgstr "" +#: templates/neluser/nsfw_message.html:2 +msgid "" +"This page contains sensitive content which is displayed because you disabled " +"the safe mode." +msgstr "" + +#: templates/neluser/nsfw_message.html:3 +msgid "Enable safe mode" +msgstr "" + #: templates/neluser/password_reset.html:4 #: templates/neluser/password_reset_confirm.html:4 #: templates/neluser/password_reset_done.html:4 diff --git a/neluser/locale/fr/LC_MESSAGES/django.po b/neluser/locale/fr/LC_MESSAGES/django.po index be6ecf0..859b8bc 100644 --- a/neluser/locale/fr/LC_MESSAGES/django.po +++ b/neluser/locale/fr/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: 1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-05-27 18:28+0200\n" +"POT-Creation-Date: 2018-06-02 21:36+0200\n" "PO-Revision-Date: 2018-02-04 01:03+0100\n" "Last-Translator: Khaganat \n" "Language-Team: Khaganat \n" @@ -145,6 +145,18 @@ msgstr "1 heure" msgid "1 day" msgstr "1 jour" +#: templates/neluser/nsfw_message.html:2 +msgid "" +"This page contains sensitive content which is displayed because you disabled " +"the safe mode." +msgstr "" +"Cette page contient du contenu indiqué comme pouvant heurter la sensibilité. " +"Ce contenu est affiché car vous avez désactivé la navigation filtrée." + +#: templates/neluser/nsfw_message.html:3 +msgid "Enable safe mode" +msgstr "Activer la navigation filtrée" + #: templates/neluser/password_reset.html:4 #: templates/neluser/password_reset_confirm.html:4 #: templates/neluser/password_reset_done.html:4 diff --git a/neluser/nsfw.py b/neluser/nsfw.py index abb8c73..38f29eb 100644 --- a/neluser/nsfw.py +++ b/neluser/nsfw.py @@ -1,6 +1,9 @@ +from django.utils.translation import ugettext_lazy as _ from django.http import HttpResponseRedirect, QueryDict +from django.template.loader import render_to_string from django.urls.exceptions import Resolver404 from django.shortcuts import render +from django.contrib import messages from neluser.models import NelUser from django.urls import reverse, resolve from django.conf import settings @@ -22,7 +25,17 @@ def is_nsfw_allowed(request): return s.lower() in settings.KHAGANAT_NSFW_OK -def disable_view(request, max_age): +def disable_view(request): + if isinstance(request.user, NelUser): + request.user.nsfw_allowed = False + request.user.save() + next_url = reverse('index') + response = HttpResponseRedirect(next_url) + response.delete_cookie(settings.KHAGANAT_NSFW_NAME) + return response + + +def enable_view(request, max_age): try: max_age = int(max_age) or None except ValueError: @@ -60,3 +73,7 @@ def redirect(request): next_url=request.get_full_path() ) return HttpResponseRedirect(dest) + +def alert(request): + msg = render_to_string('neluser/nsfw_message.html') + messages.info(request, msg, extra_tags='safe') diff --git a/neluser/templates/neluser/nsfw.html b/neluser/templates/neluser/nsfw.html index 6752bdb..9aacc09 100644 --- a/neluser/templates/neluser/nsfw.html +++ b/neluser/templates/neluser/nsfw.html @@ -9,12 +9,12 @@

{% trans "The content you were about to see is flagged as sensitive and therefore cannot be seen while the safe mode is activated." %}

{% trans "Go back home" %} - {% trans "Permanently disable safe mode" %} + {% trans "Permanently disable safe mode" %}
{% trans "Or disable safe mode for:" %} - {% trans "5 minutes" %} - {% trans "1 hour" %} - {% trans "1 day" %} + {% trans "5 minutes" %} + {% trans "1 hour" %} + {% trans "1 day" %}
{% endblock %} diff --git a/neluser/templates/neluser/nsfw_message.html b/neluser/templates/neluser/nsfw_message.html new file mode 100644 index 0000000..ca90f97 --- /dev/null +++ b/neluser/templates/neluser/nsfw_message.html @@ -0,0 +1,3 @@ +{% load i18n %} +{% trans "This page contains sensitive content which is displayed because you disabled the safe mode." %}
+{% trans "Enable safe mode" %} diff --git a/neluser/urls.py b/neluser/urls.py index 8b49f26..1022245 100644 --- a/neluser/urls.py +++ b/neluser/urls.py @@ -65,5 +65,6 @@ urlpatterns = [ # NSFW path('nsfw/', nsfw.warn_view, name='nsfw'), - path('nsfw/disable//', nsfw.disable_view, name='disable_nsfw'), + path('nsfw/enable//', nsfw.enable_view, name='enable_nsfw'), + path('nsfw/disable/', nsfw.disable_view, name='disable_nsfw'), ] diff --git a/pages/views.py b/pages/views.py index 5fbf820..c04af03 100644 --- a/pages/views.py +++ b/pages/views.py @@ -17,9 +17,11 @@ class PageView(generic.DetailView): 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: + if self.get_object().page.is_nsfw: + if not nsfw.is_nsfw_allowed(request): return nsfw.redirect(request) + else: + nsfw.alert(request) return super().dispatch(request, *args, **kwargs) def get_object(self):