Flag allowed NSFW content

This commit is contained in:
Rodolphe Breard 2018-06-02 21:43:23 +02:00
parent 6ba0834175
commit 4bdfe2d05c
10 changed files with 77 additions and 13 deletions

View file

@ -40,3 +40,7 @@ html,body {
font-style: italic; font-style: italic;
color: #9073FF; color: #9073FF;
} }
.messages {
margin-top: 10px;
}

View file

@ -21,7 +21,20 @@
<body> <body>
{% navbar %} {% navbar %}
<div class="container" id="main-content"> <div class="container" id="main-content">
{% block content %}{% endblock %} {% if messages %}
<div class="messages">
{% for message in messages %}
<div class="alert alert-{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}danger{% elif message.level == DEFAULT_MESSAGE_LEVELS.WARNING %}warning{% elif message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %}success{% elif message.level == DEFAULT_MESSAGE_LEVELS.INFO %}info{% else %}secondary{% endif %}" role="alert">
{% if 'safe' in message.tags %}
{{ message|safe }}
{% else %}
{{ message }}
{% endif %}
</div>
{% endfor %}
</div>
{% endif %}
{% block content %}{% endblock %}
</div> </div>
<script src="{% static "khaganat/jquery-3.2.1.min.js" %}"></script> <script src="{% static "khaganat/jquery-3.2.1.min.js" %}"></script>
<script src="{% static "khaganat/bootstrap/4.0.0-beta.3/js/bootstrap.bundle.min.js" %}"></script> <script src="{% static "khaganat/bootstrap/4.0.0-beta.3/js/bootstrap.bundle.min.js" %}"></script>

View file

@ -111,9 +111,11 @@ class EntriesView(generic.ListView):
return False return False
def dispatch(self, request, *args, **kwargs): 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) return nsfw.redirect(request)
else:
nsfw.alert(request)
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):

View file

@ -2,7 +2,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 1.0\n" "Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-02-04 01:03+0100\n"
"Last-Translator: Khaganat <assoc@khaganat.net>\n" "Last-Translator: Khaganat <assoc@khaganat.net>\n"
"Language-Team: Khaganat <assoc@khaganat.net>\n" "Language-Team: Khaganat <assoc@khaganat.net>\n"
@ -140,6 +140,16 @@ msgstr ""
msgid "1 day" msgid "1 day"
msgstr "" 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.html:4
#: templates/neluser/password_reset_confirm.html:4 #: templates/neluser/password_reset_confirm.html:4
#: templates/neluser/password_reset_done.html:4 #: templates/neluser/password_reset_done.html:4

View file

@ -2,7 +2,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 1.0\n" "Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-02-04 01:03+0100\n"
"Last-Translator: Khaganat <assoc@khaganat.net>\n" "Last-Translator: Khaganat <assoc@khaganat.net>\n"
"Language-Team: Khaganat <assoc@khaganat.net>\n" "Language-Team: Khaganat <assoc@khaganat.net>\n"
@ -145,6 +145,18 @@ msgstr "1 heure"
msgid "1 day" msgid "1 day"
msgstr "1 jour" 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.html:4
#: templates/neluser/password_reset_confirm.html:4 #: templates/neluser/password_reset_confirm.html:4
#: templates/neluser/password_reset_done.html:4 #: templates/neluser/password_reset_done.html:4

View file

@ -1,6 +1,9 @@
from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponseRedirect, QueryDict from django.http import HttpResponseRedirect, QueryDict
from django.template.loader import render_to_string
from django.urls.exceptions import Resolver404 from django.urls.exceptions import Resolver404
from django.shortcuts import render from django.shortcuts import render
from django.contrib import messages
from neluser.models import NelUser from neluser.models import NelUser
from django.urls import reverse, resolve from django.urls import reverse, resolve
from django.conf import settings from django.conf import settings
@ -22,7 +25,17 @@ def is_nsfw_allowed(request):
return s.lower() in settings.KHAGANAT_NSFW_OK 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: try:
max_age = int(max_age) or None max_age = int(max_age) or None
except ValueError: except ValueError:
@ -60,3 +73,7 @@ def redirect(request):
next_url=request.get_full_path() next_url=request.get_full_path()
) )
return HttpResponseRedirect(dest) return HttpResponseRedirect(dest)
def alert(request):
msg = render_to_string('neluser/nsfw_message.html')
messages.info(request, msg, extra_tags='safe')

View file

@ -9,12 +9,12 @@
<p>{% trans "The content you were about to see is flagged as sensitive and therefore cannot be seen while the safe mode is activated." %}</p> <p>{% trans "The content you were about to see is flagged as sensitive and therefore cannot be seen while the safe mode is activated." %}</p>
<div> <div>
<a class="btn btn-primary" href="{% url "index" %}" role="button">{% trans "Go back home" %}</a> <a class="btn btn-primary" href="{% url "index" %}" role="button">{% trans "Go back home" %}</a>
<a class="btn btn-danger" href="{% url "disable_nsfw" "0" %}?next={{ next_url }}" role="button">{% trans "Permanently disable safe mode" %}</a> <a class="btn btn-danger" href="{% url "enable_nsfw" "0" %}?next={{ next_url }}" role="button">{% trans "Permanently disable safe mode" %}</a>
<br> <br>
{% trans "Or disable safe mode for:" %} {% trans "Or disable safe mode for:" %}
<a class="badge badge-warning" href="{% url "disable_nsfw" "300" %}?next={{ next_url }}">{% trans "5 minutes" %}</a> <a class="badge badge-warning" href="{% url "enable_nsfw" "300" %}?next={{ next_url }}">{% trans "5 minutes" %}</a>
<a class="badge badge-warning" href="{% url "disable_nsfw" "3600" %}?next={{ next_url }}">{% trans "1 hour" %}</a> <a class="badge badge-warning" href="{% url "enable_nsfw" "3600" %}?next={{ next_url }}">{% trans "1 hour" %}</a>
<a class="badge badge-warning" href="{% url "disable_nsfw" "86400" %}?next={{ next_url }}">{% trans "1 day" %}</a> <a class="badge badge-warning" href="{% url "enable_nsfw" "86400" %}?next={{ next_url }}">{% trans "1 day" %}</a>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -0,0 +1,3 @@
{% load i18n %}
{% trans "This page contains sensitive content which is displayed because you disabled the safe mode." %}<br>
<a href="{% url 'disable_nsfw' %}">{% trans "Enable safe mode" %}</a>

View file

@ -65,5 +65,6 @@ urlpatterns = [
# NSFW # NSFW
path('nsfw/', nsfw.warn_view, name='nsfw'), path('nsfw/', nsfw.warn_view, name='nsfw'),
path('nsfw/disable/<max_age>/', nsfw.disable_view, name='disable_nsfw'), path('nsfw/enable/<max_age>/', nsfw.enable_view, name='enable_nsfw'),
path('nsfw/disable/', nsfw.disable_view, name='disable_nsfw'),
] ]

View file

@ -17,9 +17,11 @@ class PageView(generic.DetailView):
template_name = 'pages/page.html' template_name = 'pages/page.html'
def dispatch(self, request, *args, **kwargs): 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) return nsfw.redirect(request)
else:
nsfw.alert(request)
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_object(self): def get_object(self):