Sfoglia il codice sorgente

Added yelp send mail functionality whenever a negative review posted

Mohidul Islam 4 anni fa
parent
commit
645288b6f0

+ 5 - 5
analytics/background_job.py

@@ -2,15 +2,14 @@ import requests
 from django.conf import settings
 from gauth.models import Location
 from analytics.send_email import send_email
-from yelp.store_reviews import populate_reviews
 
-to = getattr(settings, 'ADMIN_MAINTEINER_EMAILS')
+to = getattr(settings, 'ADMIN_MAINTAINER_EMAILS')
 host_url = getattr(settings, 'HOST_URI')
 
 
 def send_email_weekly_summary():
-    # locations = Location.objects.all()
-    locations = Location.objects.filter(location_id='14748677429039074158')
+    locations = Location.objects.all()
+    # locations = Location.objects.filter(location_id='14748677429039074158')
     for location in locations:
         to.append(location.recipient_email) if location.recipient_email else None
         subject = f"Weekly report for {location.care_name}."
@@ -18,8 +17,9 @@ def send_email_weekly_summary():
         message_body = requests.get(url).text
         message_body += '<br><p>Regards,</p><p>Byte Trek 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():
-    populate_reviews()
     send_email_weekly_summary()

+ 3 - 3
facebook_app/analytics.py

@@ -18,9 +18,9 @@ def get_facebook_weekly_summary(location_id):
 def send_email_bad_reviews():
     locations = Location.objects.all()
     for location in locations:
-        to = settings.ADMIN_MAINTEINER_EMAILS
-        # date = timezone.now() - timezone.timedelta(hours=12)
-        date = timezone.now() - timezone.timedelta(days=20)
+        to = settings.ADMIN_MAINTAINER_EMAILS
+        date = timezone.now() - timezone.timedelta(hours=12)
+        # date = timezone.now() - timezone.timedelta(days=20)
         reviews = FacebookReview.objects.filter(
             page__location_id=location.location_id,
             create_time__gte=date,

+ 4 - 4
review/background_job.py

@@ -62,7 +62,7 @@ def reply_uncommented_reviews():
 def send_email_bad_reviews():
     locations = Location.objects.all()
     for location in locations:
-        to = settings.ADMIN_MAINTEINER_EMAILS
+        to = settings.ADMIN_MAINTAINER_EMAILS
         reviews = get_bad_reviews(location.location_id, hours=1)
         total_reviews = reviews.count()
         if total_reviews > 0:
@@ -91,8 +91,8 @@ def send_email_bad_reviews():
                 to.remove(location.recipient_email)
 
 
-def background_task_6_hours_interval():
+def background_task_every_hour():
     populate_reviews()
     send_email_bad_reviews()
-    # sleep(60 * random.randint(2, 5))
-    # reply_uncommented_reviews()
+    sleep(60 * random.randint(2, 5))
+    reply_uncommented_reviews()

+ 4 - 7
review_automation/settings/config.py

@@ -1,7 +1,9 @@
 # Cron-Jobs of the project
 CRONJOBS = [
-    ('0 */6 * * *', 'review.background_job.background_task_6_hours_interval'),
+    ('0 * * * *', 'review.background_job.background_task_6_hours_interval'),
     ('0 0 * * SUN', 'analytics.background_job.background_task_7_days_interval'),
+    ('0 */12 * * *', 'facebook_app.background_job.schedule_task'),
+    ('0 */12 * * *', 'yelp.background_job.scheduled_task')
 ]
 
 
@@ -19,13 +21,8 @@ EMAIL_USE_TLS = True
 
 EMAIL_HOST = 'smtp.gmail.com'
 
-EMAIL_HOST_USER = 'webmaster@ercare24.com'
-EMAIL_HOST_PASSWORD = 'Webdev#7182'
-
-
 EMAIL_PORT = 587
 
-ADMIN_MAINTEINER_EMAILS = [
+ADMIN_MAINTAINER_EMAILS = [
     'hannan@ercare24.com'
-    # 'ifatmohit@gmail.com'
     ]

+ 41 - 0
yelp/analytics.py

@@ -1,6 +1,9 @@
 from django.db.models import Count
 from django.utils import timezone
 from .models import YelpReview
+from analytics.send_email import send_email
+from gauth.models import Location
+from django.conf import settings
 
 
 def get_yelp_weekly_summary(location_id):
@@ -8,3 +11,41 @@ def get_yelp_weekly_summary(location_id):
     reviews = YelpReview.objects.filter(location__location_id=location_id, date_posted__gte=date)
     rating = reviews.values('rating').annotate(total=Count('rating')).order_by('-rating')
     return reviews, rating
+
+
+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)
+        reviews = YelpReview.objects.filter(
+            location__location_id=location.location_id,
+            date_posted__gte=date,
+            rating__lte=3
+        )
+        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
+
+            subject = f"A negative Yelp review has been post in {location.care_name}."
+            message = f'''
+            <p>Hi there, </p>
+            <p>'''
+            if total_reviews > 1:
+                tt = f'{total_reviews} negative Yelp reviews have'
+            else:
+                tt = f'A negative Yelp review has '
+            message += tt + f'been posted in {location.care_name}.</p>'
+            for r in reviews:
+                link = f'<p>Link: {r.location.url}</p>'
+                name = f'<p><b>Reviewer Name:</b> {r.reviewer_name}</p>'
+                rating = f'<p style="color: red"><b>Rating:</b> {str(r.rating)} star rating.</p>'
+                comment = f'<p><b>Comment: </b>{r.comment if r.comment else "No comment"}</p>'
+                message += name + rating + comment + link
+            message += '<p>Regards,</p><p>Byte Trek Ltd.</p>'
+
+            send_email(to_list=to, subject=subject, message=message)
+            if location.recipient_email in to:
+                to.remove(location.recipient_email)
+

+ 7 - 0
yelp/background_job.py

@@ -0,0 +1,7 @@
+from .store_reviews import populate_reviews
+from .analytics import send_email_bad_reviews
+
+
+def scheduled_task():
+    populate_reviews()
+    send_email_bad_reviews()