-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from SADiLaR/feature/user-management
Adding user management
- Loading branch information
Showing
21 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AccountsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "accounts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django import forms | ||
from django.contrib.auth.forms import UserCreationForm | ||
|
||
from users.models import CustomUser | ||
|
||
|
||
class CustomUserCreationForm(UserCreationForm): | ||
email = forms.EmailField(required=True, help_text="Required. Add a valid email address.") | ||
username = forms.CharField(required=True, help_text="Required. Add a valid username.") | ||
first_name = forms.CharField(required=True, help_text="Required. Add a valid first name.") | ||
last_name = forms.CharField(required=True, help_text="Required. Add a valid last name.") | ||
|
||
class Meta: | ||
model = CustomUser | ||
fields = ("username", "email", "first_name", "last_name") | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(CustomUserCreationForm, self).__init__(*args, **kwargs) | ||
self.fields["email"].required = True | ||
self.fields["username"].required = True | ||
self.fields["first_name"].required = True | ||
self.fields["last_name"].required = True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import unittest | ||
|
||
from django.test import TestCase | ||
|
||
from accounts.forms import CustomUserCreationForm | ||
|
||
|
||
class CustomUserCreationFormTest(TestCase): | ||
def setUp(self): | ||
self.username = "testuser" | ||
self.email = "[email protected]" | ||
self.first_name = "Test" | ||
self.last_name = "User" | ||
self.password1 = "sadilar2024" | ||
self.password2 = "sadilar2024" | ||
|
||
def test_valid_data(self): | ||
form = CustomUserCreationForm( | ||
{ | ||
"username": self.username, | ||
"email": self.email, | ||
"first_name": self.first_name, | ||
"last_name": self.last_name, | ||
"password1": self.password1, | ||
"password2": self.password2, | ||
} | ||
) | ||
|
||
self.assertTrue(form.is_valid()) | ||
|
||
def test_blank_data(self): | ||
form = CustomUserCreationForm({}) | ||
self.assertFalse(form.is_valid()) | ||
|
||
self.assertEqual( | ||
form.errors, | ||
{ | ||
"username": ["This field is required."], | ||
"email": ["This field is required."], | ||
"first_name": ["This field is required."], | ||
"last_name": ["This field is required."], | ||
"password1": ["This field is required."], | ||
"password2": ["This field is required."], | ||
}, | ||
) | ||
|
||
def test_invalid_email(self): | ||
form = CustomUserCreationForm( | ||
{ | ||
"username": self.username, | ||
"email": "not a valid email", | ||
"first_name": self.first_name, | ||
"last_name": self.last_name, | ||
"password1": self.password1, | ||
"password2": self.password2, | ||
} | ||
) | ||
self.assertFalse(form.is_valid()) | ||
self.assertEqual( | ||
form.errors, | ||
{ | ||
"email": ["Enter a valid email address."], | ||
}, | ||
) | ||
|
||
def test_passwords_do_not_match(self): | ||
form = CustomUserCreationForm( | ||
{ | ||
"username": self.username, | ||
"email": self.email, | ||
"first_name": self.first_name, | ||
"last_name": self.last_name, | ||
"password1": self.password1, | ||
"password2": "wrong password", | ||
} | ||
) | ||
|
||
self.assertFalse(form.is_valid()) | ||
self.assertEqual( | ||
form.errors, | ||
{ | ||
"password2": ["The two password fields didn’t match."], | ||
}, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.contrib.auth import views as auth_views | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
path("register/", views.register, name="accounts_register"), | ||
path("login/", auth_views.LoginView.as_view(template_name="accounts/login.html"), name="login"), | ||
path( | ||
"password_reset/", | ||
auth_views.PasswordResetView.as_view(template_name="accounts/password_reset_form.html"), | ||
name="password_reset", | ||
), | ||
path( | ||
"password_reset/done/", | ||
auth_views.PasswordResetDoneView.as_view(template_name="accounts/password_reset_done.html"), | ||
name="password_reset_done", | ||
), | ||
path( | ||
"reset/<uidb64>/<token>/", | ||
auth_views.PasswordResetConfirmView.as_view( | ||
template_name="accounts/password_reset_confirm.html" | ||
), | ||
name="password_reset_confirm", | ||
), | ||
path( | ||
"reset/done/", | ||
auth_views.PasswordResetCompleteView.as_view( | ||
template_name="accounts/password_reset_complete.html" | ||
), | ||
name="password_reset_complete", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.contrib.auth import login | ||
from django.shortcuts import redirect, render | ||
|
||
from .forms import CustomUserCreationForm | ||
|
||
|
||
def register(request): | ||
if request.method == "POST": | ||
form = CustomUserCreationForm(request.POST) | ||
if form.is_valid(): | ||
user = form.save(commit=False) | ||
user.is_staff = True | ||
user.save() | ||
login(request, user) | ||
return redirect("home") | ||
else: | ||
form = CustomUserCreationForm() | ||
return render(request, "accounts/register.html", {"form": form}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}Log In{% endblock %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<h2>Log In</h2> | ||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form }} | ||
<button type="submit">Log In</button> | ||
</form> | ||
|
||
<br> | ||
<hr> | ||
<form method="post" action="{% url 'logout' %}"> | ||
{% csrf_token %} | ||
<button type="submit">logout</button> | ||
</form> | ||
</div> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %}Password reset complete{% endblock %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<p>Your password has been set. You may go ahead and log in now.</p> | ||
<br> | ||
<p><a href="{{ login_url }}">Log in</a></p> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}Enter new password{% endblock %} | ||
|
||
{% block content %} | ||
|
||
{% if validlink %} | ||
|
||
<div class="container"> | ||
<p>Please enter your new password twice so we can verify you typed it in correctly.</p> | ||
|
||
<form method="post">{% csrf_token %} | ||
<fieldset class="module aligned"> | ||
<div class="form-row field-password1"> | ||
{{ form.new_password1.errors }} | ||
<div class="flex-container"> | ||
<label for="id_new_password1">New password:</label> | ||
{{ form.new_password1 }} | ||
</div> | ||
</div> | ||
<div class="form-row field-password2"> | ||
{{ form.new_password2.errors }} | ||
<div class="flex-container"> | ||
<label for="id_new_password2">Confirm password:</label> | ||
{{ form.new_password2 }} | ||
</div> | ||
</div> | ||
</fieldset> | ||
<br> | ||
<br> | ||
<div class="submit-row"> | ||
<input type="submit" value="Change my password"> | ||
</div> | ||
</form> | ||
|
||
{% else %} | ||
|
||
<p>"The password reset link was invalid, possibly because it has already been used. Please request a new | ||
password reset.</p> | ||
|
||
{% endif %} | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}Email Sent{% endblock %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<p>We’ve emailed you instructions for setting your password, if an account exists with the email you entered. | ||
You should receive them shortly.</p> | ||
|
||
<p>If you don’t receive an email, please make sure you’ve entered the address you registered with, and check | ||
your spam folder.</p> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %}Forgot Your Password?{% endblock %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<p>Forgotten your password? Enter your email address below, and we’ll email instructions for setting a new | ||
one.</p> | ||
|
||
<form method="post"> | ||
{% csrf_token %} | ||
<fieldset class="module aligned"> | ||
<div class="form-row field-email"> | ||
{{ form.email.errors }} | ||
<div class="flex-container"> | ||
<label for="id_email">Email address:</label> | ||
{{ form.email }} | ||
</div> | ||
</div> | ||
</fieldset> | ||
<div class="submit-row"> | ||
<br> | ||
<br> | ||
<input type="submit" value="Reset my password"> | ||
</div> | ||
</form> | ||
</div> | ||
{% endblock %} |
Oops, something went wrong.