diff options
| author | Bobby <[email protected]> | 2022-09-20 21:48:57 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-20 21:48:57 -0400 |
| commit | c5c3e0c20d1d7920e00619957188f5c701953e42 (patch) | |
| tree | eab94284521009228e66abee7c1c3748d77fd368 | |
| parent | ac0ca39b0891e4f9f53dbea85c1b30b5042252f0 (diff) | |
| download | thatcomputerscientist-c5c3e0c20d1d7920e00619957188f5c701953e42.tar.xz thatcomputerscientist-c5c3e0c20d1d7920e00619957188f5c701953e42.zip | |
Added registration
| -rw-r--r-- | blog/views.py | 2 | ||||
| -rw-r--r-- | templates/blog/register.html | 23 | ||||
| -rw-r--r-- | users/views.py | 48 |
3 files changed, 68 insertions, 5 deletions
diff --git a/blog/views.py b/blog/views.py index d79a686c..3f583058 100644 --- a/blog/views.py +++ b/blog/views.py @@ -70,7 +70,7 @@ def register(request): pass # Create new captcha CaptchaStore.objects.create(captcha_string=random_string, csrf_token=csrf_token) - return render(request, 'blog/register.html', {'title': 'Register', 'captcha': base64_data}) + return render(request, 'blog/register.html', {'title': 'Register', 'captcha': base64_data}) def refresh_captcha(request): diff --git a/templates/blog/register.html b/templates/blog/register.html index 4d561cb3..7bf5da85 100644 --- a/templates/blog/register.html +++ b/templates/blog/register.html @@ -28,7 +28,8 @@ <input type="password" name="password" id="password" class="form-control" placeholder="Password" required> {% for message in messages %} {% if 'passwordError' in message.tags %} - <small class="error" style="display:block;">{{ message.message }}</small> + {% load replace %} + <small class="error" style="display:block;">{{ message.message|replace:"[']" }}</small> {% endif %} {% endfor %} </div> @@ -58,8 +59,28 @@ <div> <button type="submit" class="btn btn-primary" style="margin-top:10px">Register</button> </div> + <br> + {% for message in messages %} + {% if 'accountCreated' in message.tags %} + <small class="success" style="display:block;">{{ message.message }}</small> + {% endif %} + {% endfor %} </section> </div> <script src={% static 'js/captcha.js' %}></script> +<script> + var username = new URLSearchParams(window.location.search).get('u'); + var email = new URLSearchParams(window.location.search).get('e'); + if (username) { + document.getElementsByName("username").forEach(function(input) { + input.value = username; + }); + } + if (email) { + document.getElementsByName("email").forEach(function(input) { + input.value = email.split('?u=')[0]; + }); + } +</script> {% endblock %} diff --git a/users/views.py b/users/views.py index ef522404..f2815876 100644 --- a/users/views.py +++ b/users/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponseRedirect from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login, logout, update_session_auth_hash from django.contrib import messages -from .models import UserProfile +from .models import UserProfile, CaptchaStore from django.contrib.auth.models import User from django.core.mail import send_mail from django.conf import settings @@ -13,6 +13,7 @@ from django.utils.http import urlsafe_base64_encode from django.contrib.sites.shortcuts import get_current_site from .tokens import account_activation_token, EmailChangeTokenGenerator from django.utils.http import urlsafe_base64_decode +import django.contrib.auth.password_validation as validators # Create your views here. def login_user(request): @@ -190,5 +191,46 @@ def change_email(request, uidb64, token): def register(request): - messages.error(request, 'Registration is currently disabled!', extra_tags='password2Error') - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + if request.method == 'POST': + username = request.POST['username'] + email = request.POST['email'] + password = request.POST['password'] + confirm_password = request.POST['password2'] + captcha = request.POST['captcha'] + csrf_token = request.META.get('CSRF_COOKIE') + current_captcha = CaptchaStore.objects.get(csrf_token=csrf_token).captcha_string + if str(captcha).lower() != str(current_captcha).lower(): + messages.error(request, 'Captcha is incorrect!', extra_tags='captchaError') + return HttpResponseRedirect(request.META.get('HTTP_REFERER') + '?u={}&e={}'.format(username, email)) + if password != confirm_password: + messages.error(request, 'Passwords do not match!', extra_tags='password2Error') + return HttpResponseRedirect(request.META.get('HTTP_REFERER') + '?u={}&e={}'.format(username, email)) + if User.objects.filter(username=username).exists(): + messages.error(request, 'Username is already in use!', extra_tags='usernameError') + return HttpResponseRedirect(request.META.get('HTTP_REFERER') + '?e={}'.format(email)) + if User.objects.filter(email=email).exists(): + messages.error(request, 'Email is already in use!', extra_tags='emailError') + return HttpResponseRedirect(request.META.get('HTTP_REFERER') + '?u={}'.format(username)) + try: + validators.validate_password(password=password) + except Exception as e: + messages.error(request, e, extra_tags='passwordError') + return HttpResponseRedirect(request.META.get('HTTP_REFERER') + '?u={}&e={}'.format(username, email)) + user = User.objects.create_user(username=username, email=email, password=password) + user.save() + user_profile = UserProfile(user=user) + user_profile.save() + # Send verification email + subject = 'Verify your email address' + message = render_to_string('verification_email.html', { + 'user': user.username if user.first_name is None else user.first_name, + 'site_name': 'That Computer Scientist', + 'uid': urlsafe_base64_encode(force_bytes(user.pk)), + 'token': account_activation_token.make_token(user), + 'protocol': 'https://' if request.is_secure() else 'http://', + 'domain': get_current_site(request).domain, + }) + message = strip_tags(message) + send_mail(subject, message, 'That Computer Scientist <' + settings.EMAIL_HOST_USER + '>', [email]) + messages.success(request, 'Account was created! Please check your email to verify your account.', extra_tags='accountCreated') + return redirect('blog:register') |
