From da8c8db0d2a650a0bb43623f9255b86942ad477b Mon Sep 17 00:00:00 2001 From: Emanuele Date: Wed, 10 Dec 2025 11:43:04 +0100 Subject: [PATCH] added email_sent field to student model, do not send mail twice if not needed --- cntmanage/flightslot/actions/send_email.py | 12 ++++++++-- cntmanage/flightslot/admins/student_adm.py | 2 +- ...1_student_mail_sent_alter_student_phone.py | 24 +++++++++++++++++++ cntmanage/flightslot/models/students.py | 6 +++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 cntmanage/flightslot/migrations/0031_student_mail_sent_alter_student_phone.py diff --git a/cntmanage/flightslot/actions/send_email.py b/cntmanage/flightslot/actions/send_email.py index 5e71105..d8176aa 100644 --- a/cntmanage/flightslot/actions/send_email.py +++ b/cntmanage/flightslot/actions/send_email.py @@ -31,6 +31,7 @@ def send_mail_password(request: HttpRequest, queryset: QuerySet[Student]) -> Non img.add_header("Content-Disposition", "inline", filename="cantorair.png") # build mail list filling template + queryset = queryset.filter(mail_sent=False) mails: List[EmailMultiAlternatives] = [] for student in queryset: if not student.user or not student.email: # skip student if has not an associated user @@ -56,18 +57,25 @@ def send_mail_password(request: HttpRequest, queryset: QuerySet[Student]) -> Non mail.attach(filename=img) mail.attach_alternative(content=html_message, mimetype="text/html") mails.append(mail) + student.mail_sent = True + student.save() except Exception as e: messages.error(request=request, message=f"General Error: {e}") + if len(mails) == 0: + messages.warning(request=request, message="No email will be sent") + return + # Open only one conenction and send mass email try: + sent: int = 0 with get_connection() as conn: - conn.send_messages(mails) + sent = conn.send_messages(mails) except SMTPException as e: messages.error(request=request, message=f"Send Mail SMTP error: {e.strerror}") except Exception as e: messages.error(request=request, message=f"Send Mail General error: {e}") else: - messages.success(request=request, message=f"Successfully sent {len(mails)} messages") + messages.success(request=request, message=f"Successfully sent {sent} messages") return diff --git a/cntmanage/flightslot/admins/student_adm.py b/cntmanage/flightslot/admins/student_adm.py index 0e99952..a3b8679 100644 --- a/cntmanage/flightslot/admins/student_adm.py +++ b/cntmanage/flightslot/admins/student_adm.py @@ -69,7 +69,7 @@ class ChangeAircraftForm(AdminActionForm): class StudentAdmin(ImportMixin, AdminConfirmMixin, AdminActionFormsMixin, admin.ModelAdmin): model = Student - list_display = ("surname", "name", "course", "course_color", "email", "phone", "username", "password", "active", ) + list_display = ("surname", "name", "course", "course_color", "email", "phone", "username", "password", "active", "mail_sent") list_filter = ("course", "active", ) search_fields = ("surname", "name", "phone", "email", ) actions = ("change_course", "deactivate_students", "change_aircraft", "send_mail", ) diff --git a/cntmanage/flightslot/migrations/0031_student_mail_sent_alter_student_phone.py b/cntmanage/flightslot/migrations/0031_student_mail_sent_alter_student_phone.py new file mode 100644 index 0000000..77e21a0 --- /dev/null +++ b/cntmanage/flightslot/migrations/0031_student_mail_sent_alter_student_phone.py @@ -0,0 +1,24 @@ +# Generated by Django 5.2.8 on 2025-12-10 10:28 + +import phonenumber_field.modelfields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('flightslot', '0030_weekpreference_inserted_alter_availability_week_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='student', + name='mail_sent', + field=models.BooleanField(default=False), + ), + migrations.AlterField( + model_name='student', + name='phone', + field=phonenumber_field.modelfields.PhoneNumberField(db_index=True, max_length=128, null=True, region=None, unique=True), + ), + ] diff --git a/cntmanage/flightslot/models/students.py b/cntmanage/flightslot/models/students.py index fb3374f..15c9ded 100644 --- a/cntmanage/flightslot/models/students.py +++ b/cntmanage/flightslot/models/students.py @@ -19,6 +19,7 @@ class Student(models.Model): phone = modelfields.PhoneNumberField( null=True, + db_index=True, unique=True ) @@ -54,6 +55,11 @@ class Student(models.Model): Aircraft ) + mail_sent = models.BooleanField( + null=False, + default=False + ) + def default_password(self) -> str: # Maximum 4 digits for passowrd if self.pk: return f"{self.name.lower()[0]}{self.surname.lower()}{self.id % 10000}"