71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.contrib.auth.tokens import default_token_generator
|
|
from django.contrib.sites.shortcuts import get_current_site
|
|
from django.template.loader import render_to_string
|
|
from django.core.mail import EmailMultiAlternatives
|
|
from django.utils.encoding import force_bytes
|
|
from django.http import HttpResponse, Http404
|
|
from django.conf import settings
|
|
from django.urls import reverse
|
|
from .forms import RegistrationForm
|
|
from .models import NelUser
|
|
|
|
|
|
def profile(request):
|
|
return HttpResponse('<h1>lol</h1>')
|
|
|
|
|
|
def send_activation_email(request, user):
|
|
current_site = get_current_site(request)
|
|
context = {
|
|
'email': user.email,
|
|
'domain': current_site.domain,
|
|
'site_name': current_site.name,
|
|
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
|
|
'user': user,
|
|
'token': default_token_generator.make_token(user),
|
|
'protocol': 'https' if request.is_secure() else 'http',
|
|
}
|
|
subject = render_to_string('neluser/activate_email_subject.txt', context)
|
|
subject = ''.join(subject.splitlines())
|
|
email_message = EmailMultiAlternatives(
|
|
subject,
|
|
render_to_string('neluser/activate_email.txt', context),
|
|
settings.DEFAULT_FROM_EMAIL,
|
|
[user.email]
|
|
)
|
|
email_message.attach_alternative(
|
|
render_to_string('neluser/activate_email.html', context),
|
|
'text/html'
|
|
)
|
|
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()
|
|
return render(
|
|
request,
|
|
'neluser/activate_done.html'
|
|
)
|
|
raise Http404("Unable to activate user %d with token %s." % (uid, token))
|
|
|
|
|
|
def register(request):
|
|
if request.method == 'GET':
|
|
form = RegistrationForm()
|
|
elif request.method == 'POST':
|
|
form = RegistrationForm(data=request.POST)
|
|
if form.is_valid():
|
|
user = form.save(commit=False)
|
|
user.set_password(form.cleaned_data.get('password1'))
|
|
user.is_active = not settings.REGISTER_REQUIRE_VALIDATION
|
|
user.save()
|
|
if settings.REGISTER_REQUIRE_VALIDATION:
|
|
send_activation_email(request, user)
|
|
return render(request, 'neluser/register_done.html')
|
|
return render(request, 'neluser/register.html', {'form': form})
|