Skip to content

Commit

Permalink
Merge pull request #42 from SmartReports/dev
Browse files Browse the repository at this point in the history
bugmix
  • Loading branch information
matteotolloso authored Nov 27, 2023
2 parents 708f3e8 + fb64d65 commit 97a119a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions smartreport_app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
KpiReportElement,
Alarm,
DashboardLayout,
Email
Email,
ArchivedReport,
)


Expand Down Expand Up @@ -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', )
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',)
45 changes: 23 additions & 22 deletions smartreport_app/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}')

Original file line number Diff line number Diff line change
@@ -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'),
),
]
Original file line number Diff line number Diff line change
@@ -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),
),
]

0 comments on commit 97a119a

Please sign in to comment.