Bläddra i källkod

add functionality to send email per weekly

Mohidul Islam 4 år sedan
förälder
incheckning
ffe67678b5

+ 15 - 10
analytics/background_job.py

@@ -1,12 +1,17 @@
-from django.utils import timezone
-from review.models import Review
-from django.db.models import Count
+import requests
+from django.conf import settings
+from gauth.models import Location
+from analytics.send_email import send_email
 
 
-def weekly_report(location_id):
-    date = timezone.now() - timezone.timedelta(days=7)
-    reviews = Review.objects.filter(location_id=location_id, create_time__gte=date)
-    res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating')\
-        .annotate(total=Count('star_rating')).order_by('star_rating')
-    # TODO:
-    # make a table with reviews count and bad reviews and send mail to a particular email add.
+def send_email_weekly_summary():
+    # locations = Location.objects.all()
+    locations = Location.objects.filter(location_id='3511292162159714121')
+    for location in locations:
+        to = getattr(settings, 'ADMIN_MAINTEINER_EMAILS')
+        to.append(location.recipient_email) if location.recipient_email else None
+        subject = f"Weekly report for {location.care_name}."
+        url = f'http://127.0.0.1:8000/analytics/weekly-report/{location.location_id}'
+        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)

+ 22 - 34
analytics/templates/weekly_report.html

@@ -30,20 +30,22 @@
       table{
         table-layout: fixed;
       }
+      p {
+        margin: 5px
+      }
 
     </style>
   </head>
   <body>
     <div align="center" style="margin-top: 25px;">
       <img src="https://ercare24.com/wp-content/uploads/2016/07/signature-care-resized-e1462918690585.png" alt="Avatar" class="image" width=50% >
-      <h2 style="text-align:center;">Last Week Review Report</h2>
+      <h2 style="text-align:center; margin-top: 50px">Last Week Review Report</h2>
     </div>
     <div align="center">
       <table class="customers">
       <tr>
         <th style="width: 25%;">Platform</th>
-        <th style="width: 25%;">Ratings</th>
-        <th>Name Mentioned</th>
+        <th>Ratings</th>
       </tr>
 
       <!-- Google Reviews Summary -->
@@ -56,22 +58,13 @@
           {% else %}
           {% for r in google_ratings %}
             {% if r.star_rating < 3 %}
-              <span style="color: red; font-weight: bold;">{{ r.star_rating }}* star: {{ r.total }}</span> </br>
+              <p style="color: red; font-weight: bold;">{{ r.star_rating }}* star: {{ r.total }}</p>
             {% else %}
-              <span>{{ r.star_rating }}* star: {{ r.total }}</span> </br>
+              <p>{{ r.star_rating }}* star: {{ r.total }}</p>
             {% endif %}
           {% endfor %}
           {% endif %}
         </td>
-        <td>
-          {% if google_staffs %}
-          {% for s_n in google_staffs %}
-            {{ s_n }}, 
-          {% endfor %}
-          {% else %}
-            <p>No staff or doctor got mentioned!</p>
-          {% endif %}
-        </td>
       </tr>      
       <!-- Yelp Reviews Summary -->
 
@@ -83,22 +76,13 @@
             {% else %}
             {% for r in yelp_ratings %}
               {% if r.rating < 3 %}
-                <span style="color: red; font-weight: bold;">{{ r.rating }}* star: {{ r.total }}</span> </br>
+                <p style="color: red; font-weight: bold;">{{ r.rating }}* star: {{ r.total }}</p>
               {% else %}
-                <span>{{ r.rating }}* star: {{ r.total }}</span> </br>
+                <p>{{ r.rating }}* star: {{ r.total }}</p>
               {% endif %}
             {% endfor %}
             {% endif %}
           </td>
-          <td>
-            {% if yelp_staffs %}
-            {% for s_n in yelp_staffs %}
-              {{ s_n }},
-            {% endfor %}
-            {% else %}
-              <p>No staff or doctor got mentioned!</p>
-            {% endif %}
-          </td>
       </tr>      
       <!-- Facebook Reviews Summary -->
 
@@ -121,7 +105,7 @@
 <!--      </tr>-->
     </table>
 
-    <h5>Bad Reviews</h5>
+    <h2 style="text-align:center; margin-top: 50px">All bad reviews that been posted this week.</h2>
     <table class="customers">
       <tr>
         <th style="width: 25%;">Platform</th>
@@ -132,18 +116,22 @@
       <tr>
         <td>Google</td>
         {% if google_bad_reviews.count > 0 %}
-        <td style="width: 100%;">
-          <table border="1px"  style="border-collapse: collapse;margin: -9px">
+        <td style="width: 100%; padding: 0px">
+          <table border="1px"  style="border-collapse: collapse;">
             <tr style="background-color: rgb(179, 177, 177);">
 <!--              <td style="width: 20%;">Reviewer Name</td>-->
-              <td style="width: 90%;">Comment</td>
-              <td style="width: 10%;">Rating</td>
+<!--              <td style="width: 90%;">Comment</td>-->
+<!--              <td style="width: 10%;">Rating</td>-->
             </tr>
           {% for review in google_bad_reviews %}
             <tr>
 <!--              <td>{{ review.reviewer_name }}</td>-->
-              <td>{{ review.comment }}</td>
-              <td>{{ review.star_rating }}*</td>
+              {% if review.comment %}
+                <td style="width: 90%;">{{ review.comment }}</td>
+              {% else %}
+                <td>No comment only rating.</td>
+              {% endif %}
+              <td style="width: 10%;">{{ review.star_rating }}*</td>
             </tr> 
           {% endfor %}
         </table>
@@ -160,8 +148,8 @@
       <tr>
         <td>Yelp</td>
         {% if yelp_bad_reviews.count > 0 %}
-        <td style="width: 100%;">
-          <table border="1px"  style="border-collapse: collapse;margin: -9px">
+        <td style="width: 100%; padding: 0px">
+          <table border="1px"  style="border-collapse: collapse;">
 <!--            <tr style="background-color: rgb(179, 177, 177);">-->
 <!--&lt;!&ndash;              <td style="width: 20%;">Reviewer Name</td>&ndash;&gt;-->
 <!--              <td style="width: 90%;">Comment</td>-->

+ 3 - 14
analytics/utils.py

@@ -2,8 +2,6 @@ from django.utils import timezone
 from review.models import Review
 from django.db.models import Count
 
-from name_extractor.utils import get_all_people_names
-
 
 def last_month_reviews(location_id):
     now = timezone.now()
@@ -18,14 +16,6 @@ def last_month_reviews(location_id):
     return ratings
 
 
-def get_staff_names(reviews):
-    all_names = []
-    for review in reviews:
-        names = list(get_all_people_names(review.comment))
-        all_names.extend(names)
-    return list(set(all_names))
-
-
 def weekly_reviews_summary(location_id):
     now = timezone.now()
     date = now - timezone.timedelta(days=7)
@@ -34,11 +24,10 @@ def weekly_reviews_summary(location_id):
         location_id=location_id
     )
     ratings = reviews.values('star_rating')\
-        .annotate(total=Count('star_rating'))
-    revs = reviews.exclude(comment=None)
-    names = get_staff_names(revs)
+        .annotate(total=Count('star_rating'))\
+        .order_by('-star_rating')
 
-    return reviews, ratings, names
+    return reviews, ratings
 
 
 def last_data(date, location_id):

+ 3 - 5
analytics/views.py

@@ -6,7 +6,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin
 
 from gauth.models import Location
 from name_extractor.models import Staff
-from yelp.analytics import get_weekly_summary, get_staff_names
+from yelp.analytics import get_weekly_summary
 
 from .utils import (
     get_review_count_by_month,
@@ -48,8 +48,8 @@ def monthly_report(requests, location_id):
 
 
 def weekly_report(requests, location_id):
-    g_reviews, g_ratings, g_staff_names = weekly_reviews_summary(location_id=location_id)
-    y_reviews, y_ratings, y_staff_names = get_weekly_summary(location_id=location_id)
+    g_reviews, g_ratings = weekly_reviews_summary(location_id=location_id)
+    y_reviews, y_ratings = get_weekly_summary(location_id=location_id)
 
     g_bad_reviews = g_reviews.filter(star_rating__lte=2)
     y_bad_reviews = y_reviews.filter(rating__lte=2)
@@ -57,10 +57,8 @@ def weekly_report(requests, location_id):
     context = {
 
         'google_ratings': g_ratings,
-        'google_staffs': g_staff_names,
         'google_bad_reviews': g_bad_reviews,
         'yelp_ratings': y_ratings,
-        'yelp_staffs': y_staff_names,
         'yelp_bad_reviews': y_bad_reviews
     }
     return render(requests, 'weekly_report.html', context)

+ 0 - 7
name_extractor/utils.py

@@ -14,13 +14,6 @@ def get_all_names(text):
     return names
 
 
-def get_all_people_names(text):
-    res = model_inference(text)
-    entities = res.get('entities')
-    names = {ent.get('value') for ent in entities if ent.get('entity') == 'PERSON'}
-    return names
-
-
 def all_staffs(location):
     names = []
     staffs = Staff.objects.filter(location=location)

+ 13 - 11
review/background_job.py

@@ -70,19 +70,21 @@ def send_email_bad_reviews():
             to.append(location.recipient_email) if location.recipient_email else None
 
             subject = f"A bad review has been post in {location.care_name}."
-            message =f'''
-            <img src="https://ercare24.com/wp-content/uploads/2016/07/signature-care-resized-e1462918690585.png" alt="Avatar" class="image" width=20% >
-            <h1>Bad Review has been posted.</h1>
+            message = f'''
             <p>Hi there, </p>
-            <p>There is {total_reviews} bad reviews has been posted in {location.care_name}.
-            Please, response them ASAP.</p>
-            <h4>Follow those Links to respond: </h4>
-            '''
+            <p>There '''
+            if total_reviews > 1:
+                tt = f'are {total_reviews} bad reviews has'
+            else:
+                tt = f'is a bad review has'
+            message += tt + f'been posted in {location.care_name}.</p>'
             for r in reviews:
-                link = r.location.review_site_url + '?reviewId='+r.review_id
-                text = str(r.star_rating) + ' star review'
-                message += f'<a href="{link}">{text}</a><br>'
-            message += '<p>Regards,</p><p>Byte Trek Limited</p>'
+                # link = r.location.review_site_url + '?reviewId='+r.review_id
+                name = f'<p><b>Reviewer Name:</b> {r.reviewer_name}</p>'
+                rating = f'<p><b>Rating:</b> {str(r.star_rating)} star rating.</p>'
+                comment = f'<p><b>Comment: </b>{r.comment}</p><br>'
+                message += name + rating + comment
+            message += '<p>Regards,</p><p>Byte Trek Team</p>'
 
             send_email(to_list=to, subject=subject, message=message)
 

+ 1 - 0
review_automation/settings/config.py

@@ -26,4 +26,5 @@ EMAIL_PORT = 587
 
 ADMIN_MAINTEINER_EMAILS = [
     'hannan@ercare24.com'
+    # 'ifatmohit@gmail.com'
     ]

+ 3 - 14
yelp/analytics.py

@@ -2,20 +2,9 @@ from django.db.models import Count
 from django.utils import timezone
 from .models import YelpReview
 
-from name_extractor.utils import get_all_people_names
-
-
-def get_staff_names(reviews):
-    all_names = []
-    for review in reviews:
-        names = list(get_all_people_names(review.comment))
-        all_names.extend(names)
-    return list(set(all_names))
-
 
 def get_weekly_summary(location_id):
-    date = timezone.now() - timezone.timedelta(days=100)
+    date = timezone.now() - timezone.timedelta(days=7)
     reviews = YelpReview.objects.filter(location__location_id=location_id, date_posted__gte=date)
-    rating = reviews.values('rating').annotate(total=Count('rating'))
-    names = get_staff_names(reviews)
-    return reviews, rating, names
+    rating = reviews.values('rating').annotate(total=Count('rating')).order_by('-rating')
+    return reviews, rating