background_job.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import requests
  2. from django.conf import settings
  3. from gauth.models import Location, LocationManager
  4. from django.core.mail import EmailMessage
  5. SENDER = '"Review Monitor" from SignatureCare Emergency Center. <noreply@ercare24.com>'
  6. CC = getattr(settings, 'ADMIN_MAINTAINER_EMAILS')
  7. REVIEW_MAINTAINER_EMAILS = getattr(settings, 'REVIEW_MAINTAINER_EMAILS')
  8. host_url = getattr(settings, 'HOST_URI')
  9. def send_email(subject, message_body, to_list, sender=SENDER, cc=CC):
  10. if not to_list:
  11. to_list = REVIEW_MAINTAINER_EMAILS
  12. mail = EmailMessage(
  13. subject=subject,
  14. body=message_body,
  15. from_email=sender,
  16. to=to_list,
  17. cc=cc,
  18. )
  19. mail.content_subtype = 'html'
  20. return mail.send()
  21. def send_email_weekly_summary():
  22. locations = Location.objects.all()
  23. # locations = Location.objects.filter(location_id='12541597562633926366')
  24. for location in locations:
  25. to_list = list(LocationManager.objects.filter(location_id=location.location_id).values_list('email', flat=True))
  26. subject = f"Weekly report for {location.care_name}."
  27. url = f'{host_url}/analytics/weekly-report/{location.location_id}'
  28. message_body = requests.get(url).text
  29. message_body += '<br><p>Regards,</p><p>SignatureCare Review Team</p>'
  30. send_email(subject=subject, message_body=message_body, to_list=to_list)