Browse Source

Add yelp review to weekly summery email template and chage template layout

Mohidul Islam 5 years ago
parent
commit
2626f71d00
5 changed files with 110 additions and 28 deletions
  1. 60 18
      analytics/templates/weekly_report.html
  2. 14 2
      analytics/utils.py
  3. 8 8
      analytics/views.py
  4. 7 0
      name_extractor/utils.py
  5. 21 0
      yelp/analytics.py

+ 60 - 18
analytics/templates/weekly_report.html

@@ -64,29 +64,41 @@
           {% 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 -->
 
       <tr>
         <td>Yelp</td>
-        <td>
-          <!-- {% 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>
+          <td>
+            {% if yelp_ratings.count == 0 %}
+            <span style="color: red; font-weight: bold;">No Ratings!</span>
             {% else %}
-              <span>{{ r.star_rating }}* star: {{ r.total }}</span> </br>
+            {% for r in yelp_ratings %}
+              {% if r.rating < 3 %}
+                <span style="color: red; font-weight: bold;">{{ r.rating }}* star: {{ r.total }}</span> </br>
+              {% else %}
+                <span>{{ r.rating }}* star: {{ r.total }}</span> </br>
+              {% endif %}
+            {% endfor %}
             {% endif %}
-          {% endfor %} -->
-        </td>
-        <td>
-          {% for s_n in google_staffs %}
-            {{ s_n }}, 
-          {% endfor %}
-        </td>
+          </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 -->
 
@@ -123,13 +135,13 @@
         <td style="width: 100%;">
           <table border="1px"  style="border-collapse: collapse;margin: -9px">
             <tr style="background-color: rgb(179, 177, 177);">
-              <td style="width: 20%;">Reviewer Name</td>
-              <td style="width: 70%;">Comment</td>
+<!--              <td style="width: 20%;">Reviewer Name</td>-->
+              <td style="width: 90%;">Comment</td>
               <td style="width: 10%;">Rating</td>
-            </tr> 
+            </tr>
           {% for review in google_bad_reviews %}
             <tr>
-              <td>{{ review.reviewer_name }}</td>
+<!--              <td>{{ review.reviewer_name }}</td>-->
               <td>{{ review.comment }}</td>
               <td>{{ review.star_rating }}*</td>
             </tr> 
@@ -137,9 +149,39 @@
         </table>
         </td>
         {% else %}
-        <td>No bad reviews has been posted in google!</td>
+        <td>
+          <span style="color: green; font-weight: bold;">No bad reviews has been posted in Yelp!</span>
+        </td>
         {% endif %} 
-      </tr>    
+      </tr>
+
+      <!--      Yelp bad reviews list-->
+
+      <tr>
+        <td>Yelp</td>
+        {% if yelp_bad_reviews.count > 0 %}
+        <td style="width: 100%;">
+          <table border="1px"  style="border-collapse: collapse;margin: -9px">
+<!--            <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>-->
+<!--              <td style="width: 10%;">Rating</td>-->
+<!--            </tr>-->
+            {% for review in yelp_bad_reviews %}
+              <tr>
+<!--                <td>{{ review.reviewer_name }}</td>-->
+                <td style="width: 90%;">{{ review.comment }}</td>
+                <td style="width: 10%;">{{ review.rating }}*</td>
+              </tr>
+            {% endfor %}
+          </table>
+        </td>
+        {% else %}
+          <td>
+            <span style="color: green; font-weight: bold;">No bad reviews has been posted in Yelp!</span>
+          </td>
+        {% endif %}
+      </tr>
     </table>
     </div>
   </body>

+ 14 - 2
analytics/utils.py

@@ -2,6 +2,8 @@ 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()
@@ -16,17 +18,27 @@ 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=100)
+    date = now - timezone.timedelta(days=7)
     reviews = Review.objects.filter(
         create_time__gte=date,
         location_id=location_id
     )
     ratings = reviews.values('star_rating')\
         .annotate(total=Count('star_rating'))
+    revs = reviews.exclude(comment=None)
+    names = get_staff_names(revs)
 
-    return reviews, ratings
+    return reviews, ratings, names
 
 
 def last_data(date, location_id):

+ 8 - 8
analytics/views.py

@@ -6,6 +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 .utils import (
     get_review_count_by_month,
@@ -47,20 +48,19 @@ def monthly_report(requests, location_id):
 
 
 def weekly_report(requests, location_id):
-    g_reviews, g_ratings = weekly_reviews_summary(location_id=location_id)
-
-    g_staff_names = [
-        'montgomery', 'sarah', 'sarrah', 'sarra',
-        'moss', 'katy', 'neal', 'linda', 'narinesingh', 'rajesh',
-        'neal-domanski', 'kimberly'
-    ]
+    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_bad_reviews = g_reviews.filter(star_rating__lte=2)
+    y_bad_reviews = y_reviews.filter(rating__lte=2)
 
     context = {
 
         'google_ratings': g_ratings,
         'google_staffs': g_staff_names,
-        'google_bad_reviews': g_bad_reviews
+        '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)

+ 7 - 0
name_extractor/utils.py

@@ -14,6 +14,13 @@ 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)

+ 21 - 0
yelp/analytics.py

@@ -0,0 +1,21 @@
+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)
+    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