From c432e5770950bbbeeebedb5efb4dbc4f480206fd Mon Sep 17 00:00:00 2001 From: papi Date: Sat, 11 May 2024 22:14:59 +0300 Subject: [PATCH 1/4] HTML email for new student and lecturer --- .vscode/settings.json | 6 +- accounts/apps.py | 9 + accounts/forms.py | 34 -- accounts/signals.py | 24 ++ accounts/tasks.py | 32 +- accounts/utils.py | 29 ++ core/utils.py | 32 ++ .../new_lecturer_account_confirmation.html | 303 ++++++++++++++++++ .../new_student_account_confirmation.html | 303 ++++++++++++++++++ 9 files changed, 716 insertions(+), 56 deletions(-) create mode 100644 accounts/utils.py create mode 100644 core/utils.py create mode 100755 templates/accounts/email/new_lecturer_account_confirmation.html create mode 100755 templates/accounts/email/new_student_account_confirmation.html diff --git a/.vscode/settings.json b/.vscode/settings.json index 33c968b..121f72a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,8 @@ { "editor.formatOnSave": true, - "[html]": { - "editor.formatOnSave": false - }, + // "[html]": { + // "editor.formatOnSave": false + // }, "liveSassCompile.settings.includeItems": ["/**/static/scss/style.scss"], "liveSassCompile.settings.formats": [ { diff --git a/accounts/apps.py b/accounts/apps.py index fb0257e..5f93240 100644 --- a/accounts/apps.py +++ b/accounts/apps.py @@ -3,3 +3,12 @@ from django.apps import AppConfig class AccountsConfig(AppConfig): name = "accounts" + + def ready(self) -> None: + from django.db.models.signals import post_save + from .models import User + from .signals import post_save_account_receiver + + post_save.connect(post_save_account_receiver, sender=User) + + return super().ready() diff --git a/accounts/forms.py b/accounts/forms.py index 8ec182c..d006ee4 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -1,6 +1,3 @@ -from datetime import datetime - -from django.conf import settings from django import forms from django.db import transaction from django.contrib.auth.forms import ( @@ -10,7 +7,6 @@ from django.contrib.auth.forms import ( from django.contrib.auth.forms import PasswordResetForm from course.models import Program from .models import User, Student, Parent, RELATION_SHIP, LEVEL, GENDERS -from .tasks import send_new_student_email, send_new_lecturer_email class StaffAddForm(UserCreationForm): @@ -118,24 +114,9 @@ class StaffAddForm(UserCreationForm): user.address = self.cleaned_data.get("address") user.email = self.cleaned_data.get("email") - # Generate a username - registration_date = datetime.now().strftime("%Y") - total_lecturers_count = User.objects.filter(is_lecturer=True).count() - generated_username = ( - f"{settings.LECTURER_ID_PREFIX}-{registration_date}-{total_lecturers_count}" - ) - # Generate a password - generated_password = User.objects.make_random_password() - - user.username = generated_username - user.set_password(generated_password) - if commit: user.save() - # Send email with the generated credentials - send_new_lecturer_email.delay(user.pk, generated_password) - return user @@ -272,18 +253,6 @@ class StudentAddForm(UserCreationForm): 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") - total_students_count = Student.objects.count() - generated_username = ( - f"{settings.STUDENT_ID_PREFIX}-{registration_date}-{total_students_count}" - ) - # Generate a password - generated_password = User.objects.make_random_password() - - user.username = generated_username - user.set_password(generated_password) - if commit: user.save() Student.objects.create( @@ -292,9 +261,6 @@ class StudentAddForm(UserCreationForm): program=self.cleaned_data.get("program"), ) - # Send email with the generated credentials - send_new_student_email.delay(user.pk, generated_password) - return user diff --git a/accounts/signals.py b/accounts/signals.py index e69de29..db2b145 100644 --- a/accounts/signals.py +++ b/accounts/signals.py @@ -0,0 +1,24 @@ +from .tasks import send_new_student_email, send_new_lecturer_email +from .utils import generate_student_credentials, generate_lecturer_credentials + + +def post_save_account_receiver(sender, instance=None, created=False, *args, **kwargs): + """ + Send email notification + """ + if created: + if instance.is_student: + username, password = generate_student_credentials() + instance.username = username + instance.set_password(password) + instance.save() + # Send email with the generated credentials + send_new_student_email.delay(instance.pk, password) + + if instance.is_lecturer: + username, password = generate_lecturer_credentials() + instance.username = username + instance.set_password(password) + instance.save() + # Send email with the generated credentials + send_new_lecturer_email.delay(instance.pk, password) diff --git a/accounts/tasks.py b/accounts/tasks.py index 206599f..c8cbeff 100644 --- a/accounts/tasks.py +++ b/accounts/tasks.py @@ -1,31 +1,25 @@ -import time from celery import shared_task -from django.conf import settings from django.contrib.auth import get_user_model -from django.core.mail import send_mail - - -def send_email(user, subject, msg): - send_mail( - subject, - msg, - settings.EMAIL_FROM_ADDRESS, - [user.email], - fail_silently=False, - ) +from core.utils import send_html_email @shared_task def send_new_student_email(user_pk, password): user = get_user_model().objects.get(pk=user_pk) - subject = "Your Dj LMS account credentials" - msg = f"Dear Student {user.first_name},\n\nHere are the login credentials for your DJ LMS account.\n\nYour ID: {user.username}\nYour password: {password}\n\nBe sure to change your password for security." - send_email(user, subject, msg) + send_html_email( + subject="Your Dj LMS account confirmation and credentials", + recipient_list=[user.email], + template="accounts/email/new_student_account_confirmation.html", + context={"user": user, "password": password}, + ) @shared_task def send_new_lecturer_email(user_pk, password): user = get_user_model().objects.get(pk=user_pk) - subject = "Your Dj LMS account credentials" - msg = f"Dear Lecturer {user.first_name},\n\nHere are the login credentials for your DJ LMS account.\n\nYour ID: {user.username}\nYour password: {password}\n\nBe sure to change your password for security." - send_email(user, subject, msg) + send_html_email( + subject="Your Dj LMS account confirmation and credentials", + recipient_list=[user.email], + template="accounts/email/new_student_account_confirmation.html", + context={"user": user, "password": password}, + ) diff --git a/accounts/utils.py b/accounts/utils.py new file mode 100644 index 0000000..712d9c6 --- /dev/null +++ b/accounts/utils.py @@ -0,0 +1,29 @@ +from datetime import datetime +from django.contrib.auth import get_user_model +from django.conf import settings + + +def generate_password(): + return get_user_model().objects.make_random_password() + + +def generate_student_id(): + # Generate a username based on first and last name and registration date + registered_year = datetime.now().strftime("%Y") + students_count = get_user_model().objects.filter(is_student=True).count() + return f"{settings.STUDENT_ID_PREFIX}-{registered_year}-{students_count}" + + +def generate_lecturer_id(): + # Generate a username based on first and last name and registration date + registered_year = datetime.now().strftime("%Y") + lecturers_count = get_user_model().objects.filter(is_lecturer=True).count() + return f"{settings.LECTURER_ID_PREFIX}-{registered_year}-{lecturers_count}" + + +def generate_student_credentials(): + return generate_student_id(), generate_password() + + +def generate_lecturer_credentials(): + return generate_lecturer_id(), generate_password() diff --git a/core/utils.py b/core/utils.py new file mode 100644 index 0000000..19bf670 --- /dev/null +++ b/core/utils.py @@ -0,0 +1,32 @@ +from django.core.mail import send_mail +from django.template.loader import render_to_string +from django.utils.html import strip_tags +from django.conf import settings + + +def send_email(user, subject, msg): + send_mail( + subject, + msg, + settings.EMAIL_FROM_ADDRESS, + [user.email], + fail_silently=False, + ) + + +def send_html_email(subject, recipient_list, template, context): + """A function responsible for sending HTML email""" + # Render the HTML template + html_message = render_to_string(template, context) + + # Generate plain text version of the email (optional) + plain_message = strip_tags(html_message) + + # Send the email + send_mail( + subject, + plain_message, + settings.EMAIL_FROM_ADDRESS, + recipient_list, + html_message=html_message, + ) diff --git a/templates/accounts/email/new_lecturer_account_confirmation.html b/templates/accounts/email/new_lecturer_account_confirmation.html new file mode 100755 index 0000000..95916b8 --- /dev/null +++ b/templates/accounts/email/new_lecturer_account_confirmation.html @@ -0,0 +1,303 @@ + + + + Account Confirmation + + + + +
+
+
+

Dj LMS

+

+ Your learning management system +

+
+
+

🚀 Confirm your account

+

Dear {{ user.get_full_name }},

+

+ A new lecturer account with ID of {user.username} has been + created for you.
+ You're receiving this e-mail because the Dj-LMS admin has given your + e-mail address to register an account on djlms.com.
+

+
Login credentials for your DJ LMS account:
+
    +
  • ID: {user.username}
  • +
  • Your password: {password}
  • +
+

To secure your account be sure to change your password.

+

+ Confirm Email and Login +

+

+ âš  If you think this email should not come to you, you can simply + ignore it. +

+
+ +
+
+ + diff --git a/templates/accounts/email/new_student_account_confirmation.html b/templates/accounts/email/new_student_account_confirmation.html new file mode 100755 index 0000000..a6d2a67 --- /dev/null +++ b/templates/accounts/email/new_student_account_confirmation.html @@ -0,0 +1,303 @@ + + + + Account Confirmation + + + + +
+
+
+

Dj LMS

+

+ Your learning management system +

+
+
+

🚀 Confirm your account

+

Dear {{ user.get_full_name }},

+

+ A new student account with ID of {user.username} has been + created for you.
+ You're receiving this e-mail because the Dj-LMS admin has given your + e-mail address to register an account on djlms.com.
+

+
Login credentials for your DJ LMS account:
+
    +
  • ID: {user.username}
  • +
  • Your password: {password}
  • +
+

To secure your account be sure to change your password.

+

+ Confirm Email and Login +

+

+ âš  If you think this email should not come to you, you can simply + ignore it. +

+
+ +
+
+ + From b8014c2a88bccc2ca5ad6b8e32bfbea33e4f3091 Mon Sep 17 00:00:00 2001 From: papi Date: Sat, 11 May 2024 22:21:44 +0300 Subject: [PATCH 2/4] Minor typo fixes --- .../accounts/email/new_lecturer_account_confirmation.html | 6 +++--- .../accounts/email/new_student_account_confirmation.html | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/templates/accounts/email/new_lecturer_account_confirmation.html b/templates/accounts/email/new_lecturer_account_confirmation.html index 95916b8..1d30167 100755 --- a/templates/accounts/email/new_lecturer_account_confirmation.html +++ b/templates/accounts/email/new_lecturer_account_confirmation.html @@ -281,13 +281,13 @@

âš  If you think this email should not come to you, you can simply - ignore it.âš  If you think this email shouldn't be coming to you, you can + simply ignore it.