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
|
2018-06-03 20:16:40 +00:00
|
|
|
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
|
|
|
|
s = request.COOKIES.get(settings.KHAGANAT_NSFW_NAME) or ''
|
|
|
|
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()
|
2018-06-03 21:48:21 +00:00
|
|
|
next_url = QueryDict(request.META.get('QUERY_STRING')).get('next')
|
|
|
|
if not is_link_legit(next_url):
|
|
|
|
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
|
2018-06-03 21:48:21 +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):
|
2018-06-03 21:48:21 +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(
|
|
|
|
settings.KHAGANAT_NSFW_NAME,
|
|
|
|
'true',
|
|
|
|
max_age=max_age
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def warn_view(request):
|
|
|
|
next_url = QueryDict(request.META.get('QUERY_STRING')).get('next') or '/'
|
|
|
|
if not is_link_legit(next_url):
|
|
|
|
next_url = '/'
|
2018-06-03 21:48:21 +00:00
|
|
|
prev_url = QueryDict(request.META.get('QUERY_STRING')).get('prev')
|
|
|
|
if not is_link_legit(prev_url):
|
|
|
|
prev_url = None
|
|
|
|
context = {
|
|
|
|
'prev_url': prev_url,
|
|
|
|
'go_home': True,
|
|
|
|
'next_url': next_url,
|
|
|
|
'is_authenticated': request.user.is_authenticated,
|
|
|
|
}
|
|
|
|
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 = {
|
2018-06-03 21:48:21 +00:00
|
|
|
'prev_url': None,
|
|
|
|
'go_home': False,
|
2018-05-27 16:45:52 +00:00
|
|
|
'next_url': next_url,
|
|
|
|
'is_authenticated': request.user.is_authenticated,
|
|
|
|
}
|
2018-06-03 21:48:21 +00:00
|
|
|
return render_to_string(request, 'nsfw/redirect_page.html', context=context)
|
2018-05-27 16:45:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def redirect(request):
|
|
|
|
dest = '{to_url}?next={next_url}'.format(
|
|
|
|
to_url=reverse(warn_view),
|
|
|
|
next_url=request.get_full_path()
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(dest)
|
2018-06-02 19:43:23 +00:00
|
|
|
|
2018-06-03 21:48:21 +00:00
|
|
|
def alert(request, next_url=None):
|
|
|
|
context = {
|
|
|
|
'next_url': next_url,
|
|
|
|
}
|
|
|
|
msg = render_to_string('nsfw/disabled_alert.html', context=context)
|
2018-06-02 19:43:23 +00:00
|
|
|
messages.info(request, msg, extra_tags='safe')
|