diff --git a/requirements.txt b/requirements.txt index 2eccc25..7cad501 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,7 +47,7 @@ text-unidecode==1.3 tomli==2.0.1 typing_extensions==4.8.0 tzdata==2023.3 -urllib3==2.0.7 +urllib3==1.26.6 vine==5.1.0 virtualenv==20.24.6 wcwidth==0.2.12 diff --git a/smartreport_app/admin.py b/smartreport_app/admin.py index 0f72385..53f1481 100644 --- a/smartreport_app/admin.py +++ b/smartreport_app/admin.py @@ -7,7 +7,8 @@ KpiReportElement, Alarm, DashboardLayout, - Email + Email, + ArchivedReport, ) @@ -76,4 +77,11 @@ class ReportTemplateImageAdmin(admin.ModelAdmin): class EmailAdmin(admin.ModelAdmin): list_display = ('id', 'user_type', 'emails') list_filter = ('user_type', ) - search_fields = ('user_type', ) \ No newline at end of file + search_fields = ('user_type', ) + +@admin.register(ArchivedReport) +class ArchivedReportAdmin(admin.ModelAdmin): + list_display = ('id', 'user_type', 'created', 'sent') + list_filter = ('user_type', ) + search_fields = ('user_type', ) + ordering = ('created',) \ No newline at end of file diff --git a/smartreport_app/email.py b/smartreport_app/email.py index d3b39ac..22e69bc 100644 --- a/smartreport_app/email.py +++ b/smartreport_app/email.py @@ -28,16 +28,17 @@ def send_emails_for_unsent_reports(): # Check if the report template matches the user's template if report.template.user_type == user_type: # Iterate over each Email instance - for email_instance in email_instances: + for email_address in email_instance.emails: # Create an EmailMessage object subject = 'Subject of your email' - message = 'Body of your email' + message = f'Dear {report.template.user_type},\nThis is your {report.template.frequency} report.' + email = EmailMessage( - subject, - message, - settings.DEFAULT_FROM_EMAIL, - email_instance.emails, - reply_to=[settings.DEFAULT_FROM_EMAIL], + subject = subject, + body = message, + from_email = settings.DEFAULT_FROM_EMAIL, + to = [email_address], + reply_to = [settings.DEFAULT_FROM_EMAIL], ) # Get the file content from the ArchivedReport @@ -74,19 +75,19 @@ def send_emails_for_alarms(): if current_kpi_value <= alarm.min_value or current_kpi_value >= alarm.max_value: # Create an EmailMessage object - subject = 'SmartReports Alarm' - message = f'The value of {alarm.kpi.kb_name} is {current_kpi_value}.' - email = EmailMessage( - subject = subject, - body = message, - from_email = settings.DEFAULT_FROM_EMAIL, - to = Email.objects.get(user_type=alarm.user_type).emails, - reply_to = [settings.DEFAULT_FROM_EMAIL], - ) - print(subject, message, settings.DEFAULT_FROM_EMAIL, Email.objects.get(user_type=alarm.user_type).emails, [settings.DEFAULT_FROM_EMAIL], sep='\n' ) - - print(f'sending mail for {current_kpi_uid} to {Email.objects.get(user_type=alarm.user_type).emails}') - - # Send the email - print(f'Success: {email.send()== 1}') + subject = 'SmartReports Alarm Notification' + message = f'Dear {alarm.user_type},\nThe value of {alarm.kpi.kb_name} is {current_kpi_value}.' + + for email_address in Email.objects.get(user_type=alarm.user_type).emails: + email = EmailMessage( + subject = subject, + body = message, + from_email = settings.DEFAULT_FROM_EMAIL, + to = [email_address], + reply_to = [settings.DEFAULT_FROM_EMAIL], + ) + + print(f'sending mail for {current_kpi_uid} to {email_address}') + # Send the email + print(f'Success: {email.send()== 1}') diff --git a/smartreport_app/migrations/0014_remove_archivedreport_template_and_more.py b/smartreport_app/migrations/0014_remove_archivedreport_template_and_more.py new file mode 100644 index 0000000..958c1bf --- /dev/null +++ b/smartreport_app/migrations/0014_remove_archivedreport_template_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.7 on 2023-11-27 14:54 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('smartreport_app', '0013_kpireportelement_kpis_delete_kpireportelementkpi'), + ] + + operations = [ + migrations.RemoveField( + model_name='archivedreport', + name='template', + ), + migrations.AlterField( + model_name='archivedreport', + name='created', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='archived_reports', to='smartreport_app.reporttemplate'), + ), + migrations.AlterField( + model_name='kpireportelement', + name='kpis', + field=models.ManyToManyField(to='smartreport_app.kpi'), + ), + ] diff --git a/smartreport_app/migrations/0015_archivedreport_template_alter_archivedreport_created.py b/smartreport_app/migrations/0015_archivedreport_template_alter_archivedreport_created.py new file mode 100644 index 0000000..d885c86 --- /dev/null +++ b/smartreport_app/migrations/0015_archivedreport_template_alter_archivedreport_created.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.7 on 2023-11-27 15:09 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('smartreport_app', '0014_remove_archivedreport_template_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='archivedreport', + name='template', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='archived_reports', to='smartreport_app.reporttemplate'), + preserve_default=False, + ), + migrations.AlterField( + model_name='archivedreport', + name='created', + field=models.DateTimeField(auto_now_add=True), + ), + ]