Skip to content

Commit

Permalink
Merge pull request #11 from izzyjosh/signup-upgrade
Browse files Browse the repository at this point in the history
Signup upgrade
  • Loading branch information
codewitgabi committed Nov 21, 2023
2 parents 7bd55ed + d448db0 commit 291d89e
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 42 deletions.
29 changes: 20 additions & 9 deletions Chat/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
"whitenoise.middleware.WhiteNoiseMiddleware",
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand Down Expand Up @@ -72,7 +72,6 @@
WSGI_APPLICATION = 'Chat.wsgi.application'


# Database
"""
DATABASES = {
"default": {
Expand All @@ -85,7 +84,7 @@
}
}
"""

# Database
DATABASES = {
'default': dj_database_url.parse(
os.environ.get("DATABASE_URL"),
Expand All @@ -94,28 +93,40 @@
),
}

#DATABASES = {
# "default": {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3'
# }
#}
"""DATABASES = {
"default": {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3'
}
}"""

# Password validation

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
'OPTIONS': {
'user_attributes':{
'username','first_name','last_name',
},
'max_similarity':0.5,
}
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 10,
},
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
{
'NAME': 'main.custom_validators.PasswordCombinationValidator',
},
]


Expand Down
32 changes: 32 additions & 0 deletions main/custom_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

class PasswordCombinationValidator:
def validate(self,password,user=None):
special_char = "@#$_&-+()/?!;:"
status = ""

if password.isupper() == True:
raise ValidationError(_("Password don't have lowercase letters"),
code="no lowercase",
)
if password.islower() == True:
raise ValidationError(_("Password don't have uppercase letters"),
code="no uppercase",
)
if password.isalpha() ==True:
raise ValidationError(_("Password should not be letters alone"),
code="no numbers",
)
for i in special_char:
if password.find(i) != -1:
status += "1"

if status == "":
raise ValidationError(_("Password do not contain special characters"),
code="no special characters",
)


def get_help_text(self):
return _("the password should be a combination of uppercase,lowercase,number and special symbols")
58 changes: 58 additions & 0 deletions main/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model

User = get_user_model()

class CustomUserCreationForm(UserCreationForm):

def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)

username = forms.CharField(
label = "Username*",
help_text = "",
required = True,
widget = forms.TextInput(attrs={
"name":"username",
"placeholder":"Username",
})
)
email = forms.EmailField(
label="Email",
required=True,
widget=forms.TextInput(attrs={
"name": "email",
"type": "email",
"placeholder": "Email"
})
)

password1 = forms.CharField(
label="Password*",
required=True,
help_text = "",
widget=forms.PasswordInput(attrs={
"type": "password",
"name": "password1",
"id": "password",
"placeholder": "Password"
})
)

password2 = forms.CharField(
label="Password Confirmation*",
required=True,
help_text = "",
widget=forms.PasswordInput(attrs={
"type": "password",
"name": "password2",
"id": "password",
"placeholder": "Password Confirmation"
})
)

class Meta:
model = User
fields = ("username","email","password1","password2",)

3 changes: 0 additions & 3 deletions main/tests.py

This file was deleted.

58 changes: 35 additions & 23 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib import messages
from .models import Category, Group, Message
from django.db.models import Q, Count
from .forms import CustomUserCreationForm


def redirect_auth_user(func):
Expand Down Expand Up @@ -89,31 +90,42 @@ def create_group(request):
return render(request, "create-group.html", context)


@redirect_auth_user
#@redirect_auth_user
#def signup(request):
# if request.method == "POST":
# username = request.POST.get("username")
# email = request.POST.get("email")
# password1 = request.POST.get("password1")
# password2 = request.POST.get("password2")
#
# if password1 == password2:
# if User.objects.filter(username=username).exists():
# messages.error(request, "User with given username already exists")
# elif User.objects.filter(email=email).exists():
# messages.error(request, "User with given email already exists")
# else:
# User.objects.create_user(
# username=username,
# email=email,
# password=password1)
# messages.info(request, f"Account for {username} created successfully!!")
# return redirect("signin")
# else:
# messages.error(request, "Passwords do not match")
#
# return render(request, "signup.html")

#signup form
def signup(request):
if request.method == "POST":
username = request.POST.get("username")
email = request.POST.get("email")
password1 = request.POST.get("password1")
password2 = request.POST.get("password2")

if password1 == password2:
if User.objects.filter(username=username).exists():
messages.error(request, "User with given username already exists")
elif User.objects.filter(email=email).exists():
messages.error(request, "User with given email already exists")
else:
User.objects.create_user(
username=username,
email=email,
password=password1)
messages.info(request, f"Account for {username} created successfully!!")
return redirect("signin")
else:
messages.error(request, "Passwords do not match")

return render(request, "signup.html")
if request.method == "POST":
form = CustomUserCreationForm(request.POST)

if form.is_valid():
form.save()
return redirect("signin")
else:
form = CustomUserCreationForm()
return render(request,"signup.html",{"form":form})

@redirect_auth_user
def signin(request):
Expand Down
23 changes: 22 additions & 1 deletion templates/root.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@
margin-bottom: 0.5em;
font-size: 0.8rem;
}
.error-texts {
font-size: 0.9rem;
color: red;
margin-bottom: 1.5em;
}
.error-texts ul {
margin-left: 1.5em;
text-align: left;
}
#eye {
position: absolute;
top: 0;
right: 10px;
top: calc(50% - 9px);
color: #000;
}
#auth-form input {
width: 100%;
margin: 0.5em 0;
padding: 12px;
}
</style>
</head>
<body>
Expand All @@ -77,4 +98,4 @@
{% endblock %}
</div>
</body>
</html>
</html>
38 changes: 32 additions & 6 deletions templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,47 @@
{% for message in messages %}
<div class="error-msg">{{ message }}</div>
{% endfor %}
<form action="" method="post">

<div class="error-texts">
{% if form.errors %}
{% for error in form.errors.values %}
{{ error }}
{% endfor %}
{% endif %}
</div>

<!--<form action="" method="post">
{% csrf_token %}
<label>Username<span>*</span></label>
<input type="text" name="username" placeholder="Username" required>
<label>Username<span>*</span></label>
<input type="text" name="username" placeholder="Username" required>
<label>Email<span>*</span></label>
<input type="email" name="email" placeholder="Email" required>
<label>Password<span>*</span></label>
<input type="password" name="password1" placeholder="Password" required>
<label>Confirm Password<span>*</span></label>
<input type="password" name="password2" placeholder="Confirm Password" required>
<input type="password" name="password2" placeholder="Confirm Password" required>-->
<form id="auth-form" method="post">
{% csrf_token %}
{{ form.email }}
<br>
<br>
{{ form.username }}
<br>
<br>
<div class="password-field">
{{ form.password1 }}
<i class="bi bi-eye-slash" id="eye"></i>
</div>
<br>
<div class="password-field">
{{ form.password2 }} <i class="bi bi-eye-slash" id="eye"></i>
</div>

<button class="signup-btn">Sign Up</button>
<div class="form-footer">
<span class="b">Already have an account?</span>
<a class="b" href="{% url 'signin' %}">Sign In</a>
</div>
</div>
</form>

{% endblock %}
{% endblock %}

0 comments on commit 291d89e

Please sign in to comment.