Auto-generate credentials on registration
This commit is contained in:
parent
a5d0edc4bd
commit
6e0873f3a9
@ -1,3 +1,6 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from django.core.mail import send_mail
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.contrib.auth.forms import (
|
from django.contrib.auth.forms import (
|
||||||
@ -19,6 +22,7 @@ class StaffAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Username",
|
label="Username",
|
||||||
|
required=False
|
||||||
)
|
)
|
||||||
|
|
||||||
first_name = forms.CharField(
|
first_name = forms.CharField(
|
||||||
@ -85,6 +89,7 @@ class StaffAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password",
|
label="Password",
|
||||||
|
required=False
|
||||||
)
|
)
|
||||||
|
|
||||||
password2 = forms.CharField(
|
password2 = forms.CharField(
|
||||||
@ -96,6 +101,7 @@ class StaffAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password Confirmation",
|
label="Password Confirmation",
|
||||||
|
required=False
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta(UserCreationForm.Meta):
|
class Meta(UserCreationForm.Meta):
|
||||||
@ -110,8 +116,33 @@ class StaffAddForm(UserCreationForm):
|
|||||||
user.phone = self.cleaned_data.get("phone")
|
user.phone = self.cleaned_data.get("phone")
|
||||||
user.address = self.cleaned_data.get("address")
|
user.address = self.cleaned_data.get("address")
|
||||||
user.email = self.cleaned_data.get("email")
|
user.email = self.cleaned_data.get("email")
|
||||||
|
|
||||||
|
# Generate a username based on first and last name and registration date
|
||||||
|
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
||||||
|
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}"
|
||||||
|
|
||||||
|
# Check if the generated username already exists, and regenerate if needed
|
||||||
|
while User.objects.filter(username=generated_username).exists():
|
||||||
|
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
||||||
|
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(" ", "")
|
||||||
|
|
||||||
|
user.username = generated_username
|
||||||
|
|
||||||
|
generated_password = User.objects.make_random_password()
|
||||||
|
user.set_password(generated_password)
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
|
# Send email with the generated credentials
|
||||||
|
send_mail(
|
||||||
|
'Your account credentials',
|
||||||
|
f'Your username: {generated_username}\nYour password: {generated_password}',
|
||||||
|
'from@example.com',
|
||||||
|
[user.email],
|
||||||
|
fail_silently=False,
|
||||||
|
)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@ -122,6 +153,8 @@ class StudentAddForm(UserCreationForm):
|
|||||||
attrs={"type": "text", "class": "form-control", "id": "username_id"}
|
attrs={"type": "text", "class": "form-control", "id": "username_id"}
|
||||||
),
|
),
|
||||||
label="Username",
|
label="Username",
|
||||||
|
required=False
|
||||||
|
|
||||||
)
|
)
|
||||||
address = forms.CharField(
|
address = forms.CharField(
|
||||||
max_length=30,
|
max_length=30,
|
||||||
@ -203,6 +236,8 @@ class StudentAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password",
|
label="Password",
|
||||||
|
required=False
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
password2 = forms.CharField(
|
password2 = forms.CharField(
|
||||||
@ -214,6 +249,7 @@ class StudentAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password Confirmation",
|
label="Password Confirmation",
|
||||||
|
required=False
|
||||||
)
|
)
|
||||||
|
|
||||||
# def validate_email(self):
|
# def validate_email(self):
|
||||||
@ -225,21 +261,41 @@ class StudentAddForm(UserCreationForm):
|
|||||||
model = User
|
model = User
|
||||||
|
|
||||||
@transaction.atomic()
|
@transaction.atomic()
|
||||||
def save(self):
|
def save(self, commit=True):
|
||||||
user = super().save(commit=False)
|
user = super().save(commit=False)
|
||||||
user.is_student = True
|
user.is_lecturer = True
|
||||||
user.first_name = self.cleaned_data.get("first_name")
|
user.first_name = self.cleaned_data.get("first_name")
|
||||||
user.last_name = self.cleaned_data.get("last_name")
|
user.last_name = self.cleaned_data.get("last_name")
|
||||||
user.address = self.cleaned_data.get("address")
|
|
||||||
user.phone = self.cleaned_data.get("phone")
|
user.phone = self.cleaned_data.get("phone")
|
||||||
|
user.address = self.cleaned_data.get("address")
|
||||||
user.email = self.cleaned_data.get("email")
|
user.email = self.cleaned_data.get("email")
|
||||||
|
|
||||||
|
# Generate a username based on first and last name and registration date
|
||||||
|
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
||||||
|
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}"
|
||||||
|
|
||||||
|
# Check if the generated username already exists, and regenerate if needed
|
||||||
|
while User.objects.filter(username=generated_username).exists():
|
||||||
|
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
||||||
|
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(" ", "")
|
||||||
|
|
||||||
|
user.username = generated_username
|
||||||
|
|
||||||
|
generated_password = User.objects.make_random_password()
|
||||||
|
user.set_password(generated_password)
|
||||||
|
|
||||||
|
if commit:
|
||||||
user.save()
|
user.save()
|
||||||
student = Student.objects.create(
|
|
||||||
student=user,
|
# Send email with the generated credentials
|
||||||
level=self.cleaned_data.get("level"),
|
send_mail(
|
||||||
program=self.cleaned_data.get("program"),
|
'Your account credentials',
|
||||||
|
f'Your username: {generated_username}\nYour password: {generated_password}',
|
||||||
|
'from@example.com',
|
||||||
|
[user.email],
|
||||||
|
fail_silently=False,
|
||||||
)
|
)
|
||||||
student.save()
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -212,7 +212,9 @@ def staff_add_view(request):
|
|||||||
form = StaffAddForm(request.POST)
|
form = StaffAddForm(request.POST)
|
||||||
first_name = request.POST.get("first_name")
|
first_name = request.POST.get("first_name")
|
||||||
last_name = request.POST.get("last_name")
|
last_name = request.POST.get("last_name")
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
|
|
||||||
form.save()
|
form.save()
|
||||||
messages.success(
|
messages.success(
|
||||||
request,
|
request,
|
||||||
|
|||||||
@ -189,8 +189,8 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media")
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# E-mail configuration
|
# E-mail configuration
|
||||||
|
|
||||||
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
|
EMAIL_BACKEND = config("EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend")
|
||||||
EMAIL_HOST = "smtp.gmail.com" # Gmail as the email host, but you can change it
|
EMAIL_HOST = config("EMAIL_HOST", default="smtp.gmail.com") # Gmail as the email host, but you can change it
|
||||||
EMAIL_PORT = config("EMAIL_PORT", default=587)
|
EMAIL_PORT = config("EMAIL_PORT", default=587)
|
||||||
EMAIL_USE_TLS = True
|
EMAIL_USE_TLS = True
|
||||||
EMAIL_HOST_USER = config("USER_EMAIL")
|
EMAIL_HOST_USER = config("USER_EMAIL")
|
||||||
|
|||||||
@ -19,16 +19,7 @@
|
|||||||
|
|
||||||
<form action="" method="POST">{% csrf_token %}
|
<form action="" method="POST">{% csrf_token %}
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="card">
|
|
||||||
<p class="form-title">Login Info</p>
|
|
||||||
<div class="card-body">
|
|
||||||
{{ form.username|as_crispy_field }}
|
|
||||||
{{ form.password1|as_crispy_field }}
|
|
||||||
{{ form.password2|as_crispy_field }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<p class="form-title">Personal Info</p>
|
<p class="form-title">Personal Info</p>
|
||||||
|
|||||||
@ -19,17 +19,7 @@
|
|||||||
|
|
||||||
<form action="" method="POST">{% csrf_token %}
|
<form action="" method="POST">{% csrf_token %}
|
||||||
<div class="row mb-3">
|
<div class="row mb-3">
|
||||||
<div class="col-md-6">
|
|
||||||
<div class="card">
|
|
||||||
<p class="form-title">Login Info</p>
|
|
||||||
|
|
||||||
<div class="card-body">
|
|
||||||
{{ form.username|as_crispy_field }}
|
|
||||||
{{ form.password1|as_crispy_field }}
|
|
||||||
{{ form.password2|as_crispy_field }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<p class="form-title">Personal Info</p>
|
<p class="form-title">Personal Info</p>
|
||||||
@ -42,8 +32,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<div class="row mb-3">
|
|
||||||
<div class="col-md-6 mr-auto">
|
<div class="col-md-6 mr-auto">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<p class="form-title">Others</p>
|
<p class="form-title">Others</p>
|
||||||
@ -54,6 +42,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<input class="btn btn-primary" type="submit" value="Save">
|
<input class="btn btn-primary" type="submit" value="Save">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user