khaganat-web/nsfw/views.py

89 lines
2.8 KiB
Python
Raw Permalink Normal View History

2018-06-02 19:43:23 +00:00
from django.utils.translation import ugettext_lazy as _
2018-05-27 16:45:52 +00:00
from django.http import HttpResponseRedirect, QueryDict
2018-06-02 19:43:23 +00:00
from django.template.loader import render_to_string
2018-05-27 16:45:52 +00:00
from django.shortcuts import render
2018-06-02 19:43:23 +00:00
from django.contrib import messages
2018-05-27 16:45:52 +00:00
from neluser.models import NelUser
from django.conf import settings
from django.urls import reverse
from utils import is_link_legit
2018-05-27 16:45:52 +00:00
def is_nsfw_allowed(request):
if isinstance(request.user, NelUser):
if request.user.nsfw_allowed:
return True
2019-07-24 17:07:41 +00:00
s = request.COOKIES.get(settings.KHAGANAT_NSFW_NAME) or ""
2018-05-27 16:45:52 +00:00
return s.lower() in settings.KHAGANAT_NSFW_OK
2018-06-02 19:43:23 +00:00
def disable_view(request):
if isinstance(request.user, NelUser):
request.user.nsfw_allowed = False
request.user.save()
2019-07-24 17:07:41 +00:00
next_url = QueryDict(request.META.get("QUERY_STRING")).get("next")
if not is_link_legit(next_url):
2019-07-24 17:07:41 +00:00
next_url = reverse("index")
2018-06-02 19:43:23 +00:00
response = HttpResponseRedirect(next_url)
response.delete_cookie(settings.KHAGANAT_NSFW_NAME)
return response
def enable_view(request, max_age):
2018-05-27 16:45:52 +00:00
try:
max_age = int(max_age) or None
except ValueError:
max_age = None
2019-07-24 17:07:41 +00:00
next_url = QueryDict(request.META.get("QUERY_STRING")).get("next")
2018-05-27 16:45:52 +00:00
if not is_link_legit(next_url):
2019-07-24 17:07:41 +00:00
next_url = reverse("index")
2018-05-27 16:45:52 +00:00
response = HttpResponseRedirect(next_url)
if isinstance(request.user, NelUser) and not max_age:
request.user.nsfw_allowed = True
request.user.save()
else:
response.set_cookie(
2019-07-24 17:07:41 +00:00
settings.KHAGANAT_NSFW_NAME, settings.KHAGANAT_NSFW_OK[0], max_age=max_age
2018-05-27 16:45:52 +00:00
)
return response
def warn_view(request):
2019-07-24 17:07:41 +00:00
next_url = QueryDict(request.META.get("QUERY_STRING")).get("next") or "/"
2018-05-27 16:45:52 +00:00
if not is_link_legit(next_url):
2019-07-24 17:07:41 +00:00
next_url = "/"
prev_url = QueryDict(request.META.get("QUERY_STRING")).get("prev")
if not is_link_legit(prev_url):
prev_url = None
context = {
2019-07-24 17:07:41 +00:00
"prev_url": prev_url,
"go_home": True,
"next_url": next_url,
"is_authenticated": request.user.is_authenticated,
}
2019-07-24 17:07:41 +00:00
return render(request, "nsfw/redirect_page.html", context=context)
def warn_msg(request, next_url=None):
2018-05-27 16:45:52 +00:00
context = {
2019-07-24 17:07:41 +00:00
"prev_url": None,
"go_home": False,
"next_url": next_url,
"is_authenticated": request.user.is_authenticated,
2018-05-27 16:45:52 +00:00
}
2019-07-24 17:07:41 +00:00
ret = render_to_string(request, "nsfw/redirect_page.html", context=context)
2018-11-08 19:11:14 +00:00
return ret
2018-05-27 16:45:52 +00:00
def redirect(request):
2019-07-24 17:07:41 +00:00
dest = "{to_url}?next={next_url}".format(
to_url=reverse(warn_view), next_url=request.get_full_path()
2018-05-27 16:45:52 +00:00
)
return HttpResponseRedirect(dest)
2018-06-02 19:43:23 +00:00
2018-11-08 19:11:14 +00:00
def alert(request, next_url=None):
2019-07-24 17:07:41 +00:00
context = {"next_url": next_url}
msg = render_to_string("nsfw/disabled_alert.html", context=context)
messages.info(request, msg, extra_tags="safe")