Browse Source

Chnage send mail to multiple location manager and add cc mails

Mohidul Islam 4 years ago
parent
commit
8f60b8c5ae

+ 23 - 12
analytics/background_job.py

@@ -1,25 +1,36 @@
 import requests
 from django.conf import settings
-from gauth.models import Location
-from analytics.send_email import send_email
+from gauth.models import Location, LocationManager
+from django.core.mail import EmailMessage
 
-to = getattr(settings, 'ADMIN_MAINTAINER_EMAILS')
+SENDER = '"Review Monitor" from SignatureCare Emergency Center. <noreply@ercare24.com>'
+CC = getattr(settings, 'ADMIN_MAINTAINER_EMAILS')
+REVIEW_MAINTAINER_EMAILS = getattr(settings, 'REVIEW_MAINTAINER_EMAILS')
 host_url = getattr(settings, 'HOST_URI')
 
 
+def send_email(subject, message_body, to_list, sender=SENDER, cc=CC):
+    if not to_list:
+        to_list = REVIEW_MAINTAINER_EMAILS
+
+    mail = EmailMessage(
+        subject=subject,
+        body=message_body,
+        from_email=sender,
+        to=to_list,
+        cc=cc,
+    )
+    mail.content_subtype = 'html'
+    return mail.send()
+
+
 def send_email_weekly_summary():
     locations = Location.objects.all()
-    # locations = Location.objects.filter(location_id='14748677429039074158')
+    # locations = Location.objects.filter(location_id='12541597562633926366')
     for location in locations:
-        to.append(location.recipient_email) if location.recipient_email else None
+        to_list = list(LocationManager.objects.filter(location_id=location.location_id).values_list('email', flat=True))
         subject = f"Weekly report for {location.care_name}."
         url = f'{host_url}/analytics/weekly-report/{location.location_id}'
         message_body = requests.get(url).text
         message_body += '<br><p>Regards,</p><p>SignatureCare Review Team</p>'
-        send_email(to_list=to, subject=subject, message=message_body)
-        if location.recipient_email in to:
-            to.remove(location.recipient_email)
-
-
-def background_task_7_days_interval():
-    send_email_weekly_summary()
+        send_email(subject=subject, message_body=message_body, to_list=to_list)

+ 6 - 12
facebook_app/analytics.py

@@ -1,9 +1,8 @@
-from django.conf import settings
 from django.db.models import Count
 from django.utils import timezone
 from .models import FacebookReview
-from gauth.models import Location
-from analytics.send_email import send_email
+from gauth.models import Location, LocationManager
+from analytics.background_job import send_email
 
 
 def get_facebook_weekly_summary(location_id):
@@ -18,7 +17,6 @@ def get_facebook_weekly_summary(location_id):
 def send_email_bad_reviews():
     locations = Location.objects.all()
     for location in locations:
-        to = settings.ADMIN_MAINTAINER_EMAILS
         date = timezone.now() - timezone.timedelta(hours=12)
         # date = timezone.now() - timezone.timedelta(days=20)
         reviews = FacebookReview.objects.filter(
@@ -29,24 +27,20 @@ def send_email_bad_reviews():
         total_reviews = reviews.count()
         if total_reviews > 0:
             # Add location maintainer email if it has an email
-            to.append(location.recipient_email) if location.recipient_email else None
+            to = list(LocationManager.objects.filter(location_id=location.location_id).values_list('email', flat=True))
 
             subject = f"A negative Facebook review has been post in {location.care_name}."
-            message = f'''
-            <p>Hi there, </p>
-            <p>'''
+            message = '<p>Hi there, </p><p>'
             if total_reviews > 1:
                 tt = f'{total_reviews} negative Facebook reviews have'
             else:
-                tt = f'A negative Facebook review has '
+                tt = 'A negative Facebook review has '
             message += tt + f'been posted in {location.care_name}.</p>'
             for r in reviews:
                 link = f'<p>Link: https://facebook.com/{r.id}</p>'
                 comment = f'<p><b>Comment: </b>{r.review_text if r.review_text else "No comment"}</p>'
                 message += comment + link
             message += '<p>Regards,</p><p>SignatureCare Review Team.</p>'
+            send_email(subject=subject, message_body=message, to_list=to)
 
-            send_email(to_list=to, subject=subject, message=message)
-            if location.recipient_email in to:
-                to.remove(location.recipient_email)
 

+ 7 - 14
review/background_job.py

@@ -1,13 +1,12 @@
 import random
 from time import sleep
 from django.utils import timezone
-from gauth.models import Location
-from django.conf import settings
-from analytics.send_email import send_email
+from gauth.models import Location, LocationManager
+from analytics.background_job import send_email
 from .models import Review, CustomReply, Reply
 from nlu_job.nlu_utils import is_a_name
 from review.review_utils import reply_review
-from review.review_utils import populate_reviews, get_bad_reviews, fetch_batch_of_reviews
+from review.review_utils import get_bad_reviews, fetch_batch_of_reviews
 
 from nameparser import HumanName
 
@@ -62,21 +61,17 @@ def reply_uncommented_reviews():
 def send_email_bad_reviews():
     locations = Location.objects.all()
     for location in locations:
-        to = settings.ADMIN_MAINTAINER_EMAILS
         reviews = get_bad_reviews(location.location_id, hours=1)
         total_reviews = reviews.count()
         if total_reviews > 0:
-            # Add location mainteiner email if it has an email
-            to.append(location.recipient_email) if location.recipient_email else None
+            to = list(LocationManager.objects.filter(location_id=location.location_id).values_list('email', flat=True))
 
             subject = f"A negative Google review has been post in {location.care_name}."
-            message = f'''
-            <p>Hi there, </p>
-            <p>'''
+            message = '<p>Hi there, </p><p>'
             if total_reviews > 1:
                 tt = f'{total_reviews} negative Google reviews have'
             else:
-                tt = f'A negative Google review has '
+                tt = 'A negative Google review has '
             message += tt + f'been posted in {location.care_name}.</p>'
             for r in reviews:
                 link = f'<p>Link: {r.location.review_site_url}</p>'
@@ -86,9 +81,7 @@ def send_email_bad_reviews():
                 message += name + rating + comment + link
             message += '<p>Regards,</p><p>SignatureCare Review Team.</p>'
 
-            send_email(to_list=to, subject=subject, message=message)
-            if location.recipient_email in to:
-                to.remove(location.recipient_email)
+            send_email(subject=subject, message_body=message, to_list=to)
 
 
 def background_task_every_hour():

+ 5 - 1
review_automation/settings/config.py

@@ -1,7 +1,7 @@
 # Cron-Jobs of the project
 CRONJOBS = [
     ('0 * * * *', 'review.background_job.background_task_every_hour'),
-    ('0 0 * * SUN', 'analytics.background_job.background_task_7_days_interval'),
+    ('0 0 * * SUN', 'analytics.background_job.send_email_weekly_summary'),
     ('0 */12 * * *', 'facebook_app.background_job.schedule_task'),
 ]
 
@@ -25,3 +25,7 @@ EMAIL_PORT = 587
 ADMIN_MAINTAINER_EMAILS = [
     'hannan@ercare24.com'
     ]
+
+REVIEW_MAINTAINER_EMAILS = [
+    'review@ercare24.com'
+    ]

+ 1 - 1
yelp/analytics.py

@@ -1,7 +1,7 @@
 from django.db.models import Count
 from django.utils import timezone
 from .models import YelpReview
-from analytics.send_email import send_email
+from analytics.background_job import send_email
 from gauth.models import Location
 from django.conf import settings