2018-08-10 19:14:33 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
/signup app views.
|
|
|
|
"""
|
2018-08-12 14:52:05 -04:00
|
|
|
# TODO: make unique username validation be case insensitive
|
2019-01-21 15:59:31 -05:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
2018-08-12 01:59:22 -04:00
|
|
|
from django.shortcuts import redirect, render
|
2019-01-21 15:59:31 -05:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.conf import settings
|
2018-08-12 01:59:22 -04:00
|
|
|
|
2018-08-12 14:52:05 -04:00
|
|
|
from .forms import SignupForm
|
2019-01-21 15:59:31 -05:00
|
|
|
from .models import Confirmation
|
2018-08-10 19:14:33 -04:00
|
|
|
|
|
|
|
def index(request):
|
|
|
|
"""
|
|
|
|
The signup page.
|
|
|
|
"""
|
2018-08-12 01:59:22 -04:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
return redirect('homepage:index')
|
2018-08-12 14:52:05 -04:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = SignupForm(request.POST)
|
|
|
|
if form.is_valid():
|
2019-01-21 15:59:31 -05:00
|
|
|
user = form.save()
|
|
|
|
confirm = Confirmation(
|
|
|
|
user=user,
|
|
|
|
code=get_code(32)
|
|
|
|
)
|
|
|
|
confirm.save()
|
|
|
|
verify_url = request.build_absolute_uri(reverse('signup:verify'))
|
|
|
|
verify_url = verify_url.replace("http", "https")
|
|
|
|
email_confirmation(confirm, verify_url)
|
|
|
|
return render(request, 'signup/confirmation_sent.html', locals())
|
2018-08-12 14:52:05 -04:00
|
|
|
else:
|
|
|
|
form = SignupForm()
|
|
|
|
return render(request, 'signup/index.html', {'form': form})
|
2019-01-21 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_code(n):
|
|
|
|
"""
|
|
|
|
Helper function to generate n number of psudeo-random ASCII characters.
|
|
|
|
"""
|
|
|
|
chars = []
|
|
|
|
for _ in range(n):
|
|
|
|
chars.append(random.choice(string.ascii_letters + string.digits))
|
|
|
|
return "".join(chars)
|
|
|
|
|
|
|
|
|
|
|
|
def email_confirmation(confirm, verify_url):
|
|
|
|
"""
|
|
|
|
Sends out a confirmation to the user.
|
|
|
|
"""
|
|
|
|
message = "Please use the following link to verify your account: "
|
|
|
|
message += verify_url + "?code=" + confirm.code
|
|
|
|
send_mail(
|
|
|
|
"Email verification",
|
|
|
|
message,
|
|
|
|
settings.DEFAULT_FROM_EMAIL,
|
|
|
|
[confirm.user.email],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def verify(request):
|
|
|
|
"""
|
|
|
|
Verifies and activates the user's account.
|
|
|
|
"""
|
|
|
|
code = request.GET.get("code")
|
|
|
|
if not code:
|
|
|
|
return redirect('homepage:index')
|
|
|
|
try:
|
|
|
|
confirm = Confirmation.objects.get(code=code)
|
|
|
|
except Confirmation.DoesNotExist:
|
|
|
|
return redirect('homepage:index')
|
|
|
|
user = confirm.user
|
|
|
|
user.is_active = True
|
|
|
|
user.save()
|
|
|
|
confirm.delete()
|
|
|
|
return render(request, 'signup/confirmed.html', locals())
|