Skip to content

Commit

Permalink
things
Browse files Browse the repository at this point in the history
  • Loading branch information
matteotolloso committed Nov 27, 2023
1 parent 671a10b commit 35a2c2f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
2 changes: 2 additions & 0 deletions build_files.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
echo "BUILD START"
python3.9 -m pip install -r requirements.txt
python3.9 manage.py collectstatic --noinput --clear
echo "MIGRATIONS"
python3.9 manage.py migrate
echo "BUILD END"
13 changes: 7 additions & 6 deletions smartreport/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@

DEBUG = True

DEFAULT_FROM_EMAIL = 'smartreports@example.com'
DEFAULT_FROM_EMAIL = 'updates@smartreports.it'

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.mailtrap.io'
EMAIL_PORT = 2525
EMAIL_HOST_USER = 'fa29b0a6d18965'
EMAIL_HOST_PASSWORD = '84f5260599344e'
EMAIL_USE_TLS = True
EMAIL_HOST = 'authsmtp.securemail.pro'
EMAIL_PORT = 465
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
51 changes: 27 additions & 24 deletions smartreport_app/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,43 @@ def send_emails_for_unsent_reports():
user_type=user_type, sent=False
)

# Get the corresponding Email instance for the user type
email_instance = Email.objects.get(user_type=user_type)
# Get all corresponding Email instances for the user type
email_instances = Email.objects.filter(user_type=user_type)

# Validate that emails field is a list
if not isinstance(email_instance.emails, list):
raise ValidationError("Emails must be a list")
for email_instance in email_instances:
if not isinstance(email_instance.emails, list):
raise ValidationError("Emails must be a list")

# Iterate over each unsent report and send an email for each file
for report in unsent_reports:
# Check if the report template matches the user's template
if report.template.user_type == user_type:
# Create an EmailMessage object
subject = 'Subject of your email'
message = 'Body of your email'
email = EmailMessage(
subject,
message,
settings.DEFAULT_FROM_EMAIL,
email_instance.emails,
reply_to=[settings.DEFAULT_FROM_EMAIL],
)

# Get the file content from the ArchivedReport
file_content = report.file.read()

# Attach the file to the email
email.attach(report.file.name, file_content, 'application/octet-stream')

# Send the email
email.send()
# Iterate over each Email instance
for email_instance in email_instances:
# Create an EmailMessage object
subject = 'Subject of your email'
message = 'Body of your email'
email = EmailMessage(
subject,
message,
settings.DEFAULT_FROM_EMAIL,
email_instance.emails,
reply_to=[settings.DEFAULT_FROM_EMAIL],
)

# Get the file content from the ArchivedReport
file_content = report.file.read()

# Attach the file to the email
email.attach(report.file.name, file_content, 'application/octet-stream')

# Send the email
email.send()

# Update the sent status of the ArchivedReport
report.sent = True
report.save()

except Email.DoesNotExist:
print(f"Email with user_type {user_type} does not exist.")
except Exception as e:
Expand Down Expand Up @@ -81,6 +83,7 @@ def send_emails_for_alarms():
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}')

Expand Down
1 change: 1 addition & 0 deletions smartreport_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
router.register(r"alarms-list", views.AlarmViewSet)
router.register(r"report-img", views.ReportTemplateImageViewSet)
router.register(r"dashboard-layout", views.DashboardLayoutViewSet)
router.register(r"report-archive", views.ArchivedReportViewSet)
router.register(r"kpi-data", views.KpiDataViewSet, basename="kpi-data")
router.register(r'sync-kb', views.SyncKBViewSet, basename='sync-kb')
router.register(r'send-reports', views.SendReportEmailsViewSet, basename='send-emails')
Expand Down
2 changes: 1 addition & 1 deletion smartreport_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def list(self, request):
return Response({"data": data})


class ArchiveViewSet(viewsets.ModelViewSet):
class ArchivedReportViewSet(viewsets.ModelViewSet):
queryset = ArchivedReport.objects.all()
serializer_class = ArchivedReportSerializer
filterset_fields = ["user_type"]
Expand Down

0 comments on commit 35a2c2f

Please sign in to comment.