2018-02-04 00:36:39 +00:00
|
|
|
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
|
2018-11-08 13:18:24 +00:00
|
|
|
from django.shortcuts import redirect, render, get_object_or_404
|
2018-02-04 00:36:39 +00:00
|
|
|
from django.contrib.auth.tokens import default_token_generator
|
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2018-11-08 13:18:24 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-11-08 14:29:59 +00:00
|
|
|
from django.contrib.auth import logout
|
|
|
|
from django.contrib import messages
|
2018-02-04 00:36:39 +00:00
|
|
|
from django.template.loader import render_to_string
|
2018-11-08 14:29:59 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-02-04 00:36:39 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2018-11-08 13:18:24 +00:00
|
|
|
from django.views.generic.edit import FormView
|
2018-02-04 00:36:39 +00:00
|
|
|
from django.utils.encoding import force_bytes
|
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
from django.conf import settings
|
2018-11-08 13:18:24 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-11-09 17:09:04 +00:00
|
|
|
from nsfw.views import is_nsfw_allowed
|
2018-11-08 14:29:59 +00:00
|
|
|
from .forms import RegistrationForm, ChangePasswordForm, DeleteAccountForm
|
2018-02-04 00:36:39 +00:00
|
|
|
from .models import NelUser
|
2018-01-27 20:06:40 +00:00
|
|
|
|
2018-02-04 00:36:39 +00:00
|
|
|
|
2018-11-08 13:18:24 +00:00
|
|
|
@login_required
|
|
|
|
def settings_default(request):
|
2019-07-24 17:07:41 +00:00
|
|
|
next_page = reverse_lazy("set_nsfw")
|
2018-11-08 13:18:24 +00:00
|
|
|
return redirect(next_page)
|
|
|
|
|
|
|
|
|
2018-11-08 20:09:17 +00:00
|
|
|
@login_required
|
|
|
|
def set_nsfw(request):
|
|
|
|
ctx = {
|
2019-07-24 17:07:41 +00:00
|
|
|
"nsfw_allowed": is_nsfw_allowed(request),
|
|
|
|
"current_url": reverse_lazy("set_nsfw"),
|
|
|
|
"tab": "preferences",
|
|
|
|
"active_block": "nsfw_filter",
|
2018-11-08 20:09:17 +00:00
|
|
|
}
|
2019-07-24 17:07:41 +00:00
|
|
|
return render(request, "neluser/settings/preferences/nsfw.html", ctx)
|
2018-11-08 20:09:17 +00:00
|
|
|
|
|
|
|
|
2018-11-08 13:18:24 +00:00
|
|
|
class ChangePasswordView(LoginRequiredMixin, FormView):
|
2019-07-24 17:07:41 +00:00
|
|
|
template_name = "neluser/settings/security/password.html"
|
2018-11-08 13:18:24 +00:00
|
|
|
form_class = ChangePasswordForm
|
2019-07-24 17:07:41 +00:00
|
|
|
success_url = reverse_lazy("password_change")
|
2018-11-08 13:18:24 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2019-07-24 17:07:41 +00:00
|
|
|
context["tab"] = "security"
|
|
|
|
context["active_block"] = "password"
|
2018-11-08 13:18:24 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kw = super().get_form_kwargs()
|
2019-07-24 17:07:41 +00:00
|
|
|
kw["request"] = self.request
|
2018-11-08 13:18:24 +00:00
|
|
|
return kw
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2019-07-24 17:07:41 +00:00
|
|
|
new_password = form.cleaned_data["new_password"]
|
2018-11-08 13:18:24 +00:00
|
|
|
user = self.request.user
|
|
|
|
user.set_password(new_password)
|
|
|
|
user.save()
|
|
|
|
return super().form_valid(form)
|
2018-02-04 00:36:39 +00:00
|
|
|
|
|
|
|
|
2018-11-08 14:29:59 +00:00
|
|
|
class DeleteAccountView(LoginRequiredMixin, FormView):
|
2019-07-24 17:07:41 +00:00
|
|
|
template_name = "neluser/settings/security/delete_account.html"
|
2018-11-08 14:29:59 +00:00
|
|
|
form_class = DeleteAccountForm
|
2019-07-24 17:07:41 +00:00
|
|
|
success_url = reverse_lazy("delete_account")
|
2018-11-08 14:29:59 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2019-07-24 17:07:41 +00:00
|
|
|
context["tab"] = "security"
|
|
|
|
context["active_block"] = "delete_account"
|
2018-11-08 14:29:59 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kw = super().get_form_kwargs()
|
2019-07-24 17:07:41 +00:00
|
|
|
kw["request"] = self.request
|
2018-11-08 14:29:59 +00:00
|
|
|
return kw
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.request.user.delete()
|
|
|
|
logout(self.request)
|
2019-07-24 17:07:41 +00:00
|
|
|
messages.info(self.request, _("Your account has been deleted."))
|
2018-11-08 14:29:59 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
2018-02-04 00:36:39 +00:00
|
|
|
def send_activation_email(request, user):
|
|
|
|
current_site = get_current_site(request)
|
|
|
|
context = {
|
2019-07-24 17:07:41 +00:00
|
|
|
"email": user.email,
|
|
|
|
"domain": current_site.domain,
|
|
|
|
"site_name": current_site.name,
|
|
|
|
"uid": urlsafe_base64_encode(force_bytes(user.pk)),
|
|
|
|
"user": user,
|
|
|
|
"token": default_token_generator.make_token(user),
|
|
|
|
"protocol": "https" if request.is_secure() else "http",
|
2018-02-04 00:36:39 +00:00
|
|
|
}
|
2019-07-24 17:07:41 +00:00
|
|
|
subject = render_to_string("neluser/activate_email_subject.txt", context)
|
|
|
|
subject = "".join(subject.splitlines())
|
2018-02-04 00:36:39 +00:00
|
|
|
email_message = EmailMultiAlternatives(
|
|
|
|
subject,
|
2019-07-24 17:07:41 +00:00
|
|
|
render_to_string("neluser/activate_email.txt", context),
|
2018-02-04 00:36:39 +00:00
|
|
|
settings.DEFAULT_FROM_EMAIL,
|
2019-07-24 17:07:41 +00:00
|
|
|
[user.email],
|
2018-02-04 00:36:39 +00:00
|
|
|
)
|
|
|
|
email_message.attach_alternative(
|
2019-07-24 17:07:41 +00:00
|
|
|
render_to_string("neluser/activate_email.html", context), "text/html"
|
2018-02-04 00:36:39 +00:00
|
|
|
)
|
|
|
|
email_message.send()
|
|
|
|
|
|
|
|
|
|
|
|
def activate(request, uidb64, token):
|
|
|
|
uid = urlsafe_base64_decode(uidb64).decode()
|
|
|
|
user = get_object_or_404(NelUser, pk=uid)
|
|
|
|
if default_token_generator.check_token(user, token):
|
|
|
|
user.is_active = True
|
|
|
|
user.save()
|
2019-07-24 17:07:41 +00:00
|
|
|
return render(request, "neluser/activate_done.html")
|
2018-02-04 00:36:39 +00:00
|
|
|
raise Http404("Unable to activate user %d with token %s." % (uid, token))
|
|
|
|
|
|
|
|
|
|
|
|
def register(request):
|
2019-07-24 17:07:41 +00:00
|
|
|
if request.method == "GET":
|
2018-02-04 00:36:39 +00:00
|
|
|
form = RegistrationForm()
|
2019-07-24 17:07:41 +00:00
|
|
|
elif request.method == "POST":
|
2018-02-04 00:36:39 +00:00
|
|
|
form = RegistrationForm(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save(commit=False)
|
2019-07-24 17:07:41 +00:00
|
|
|
user.set_password(form.cleaned_data.get("password1"))
|
2018-02-04 00:36:39 +00:00
|
|
|
user.is_active = not settings.REGISTER_REQUIRE_VALIDATION
|
|
|
|
user.save()
|
|
|
|
if settings.REGISTER_REQUIRE_VALIDATION:
|
|
|
|
send_activation_email(request, user)
|
2019-07-24 17:07:41 +00:00
|
|
|
return render(request, "neluser/register_done.html")
|
|
|
|
return render(request, "neluser/register.html", {"form": form})
|