diff --git a/accounts/forms.py b/accounts/forms.py index a10fae1..47f6177 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -1,3 +1,6 @@ +from datetime import datetime + +from django.core.mail import send_mail from django import forms from django.db import transaction from django.contrib.auth.forms import ( @@ -19,6 +22,7 @@ class StaffAddForm(UserCreationForm): } ), label="Username", + required=False ) first_name = forms.CharField( @@ -85,6 +89,7 @@ class StaffAddForm(UserCreationForm): } ), label="Password", + required=False ) password2 = forms.CharField( @@ -96,6 +101,7 @@ class StaffAddForm(UserCreationForm): } ), label="Password Confirmation", + required=False ) class Meta(UserCreationForm.Meta): @@ -110,8 +116,33 @@ class StaffAddForm(UserCreationForm): user.phone = self.cleaned_data.get("phone") user.address = self.cleaned_data.get("address") 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() + + # 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 @@ -122,6 +153,8 @@ class StudentAddForm(UserCreationForm): attrs={"type": "text", "class": "form-control", "id": "username_id"} ), label="Username", + required=False + ) address = forms.CharField( max_length=30, @@ -212,6 +245,8 @@ class StudentAddForm(UserCreationForm): } ), label="Password", + required=False + ) password2 = forms.CharField( @@ -223,6 +258,7 @@ class StudentAddForm(UserCreationForm): } ), label="Password Confirmation", + required=False ) # def validate_email(self): @@ -234,22 +270,43 @@ class StudentAddForm(UserCreationForm): model = User @transaction.atomic() - def save(self): + def save(self, commit=True): user = super().save(commit=False) - user.is_student = True + user.is_lecturer = True user.first_name = self.cleaned_data.get("first_name") user.last_name = self.cleaned_data.get("last_name") user.gender = self.cleaned_data.get("gender") user.address = self.cleaned_data.get("address") user.phone = self.cleaned_data.get("phone") + user.address = self.cleaned_data.get("address") user.email = self.cleaned_data.get("email") - user.save() - student = Student.objects.create( - student=user, - level=self.cleaned_data.get("level"), - program=self.cleaned_data.get("program"), - ) - student.save() + + # 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() + + # 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 diff --git a/accounts/views.py b/accounts/views.py index 246c1ca..010991c 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -212,7 +212,9 @@ def staff_add_view(request): form = StaffAddForm(request.POST) first_name = request.POST.get("first_name") last_name = request.POST.get("last_name") + if form.is_valid(): + form.save() messages.success( request, diff --git a/config/settings.py b/config/settings.py index 9c9e2c2..8073185 100644 --- a/config/settings.py +++ b/config/settings.py @@ -189,8 +189,8 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media") # ----------------------------------- # E-mail configuration -EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" -EMAIL_HOST = "smtp.gmail.com" # Gmail as the email host, but you can change it +EMAIL_BACKEND = config("EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend") +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_USE_TLS = True EMAIL_HOST_USER = config("USER_EMAIL") diff --git a/templates/accounts/add_staff.html b/templates/accounts/add_staff.html index ca9cd5c..57d26cf 100644 --- a/templates/accounts/add_staff.html +++ b/templates/accounts/add_staff.html @@ -19,16 +19,7 @@