Parcourir la source

Add sync locaion view and handle its functionallity

Mohidul Islam il y a 5 ans
Parent
commit
03d06e28a7

+ 1 - 1
dashboard/templates/report.html

@@ -21,7 +21,7 @@
           {% if loc.total_review <= loc.total_review_DB %}
             <td><i class="fa fa-check-square-o" style="font-size:36px;color:green"></i></td>
           {% else %}
-            <td><a href="#"><i class="fa fa-refresh" style="font-size:36px;color:#fc9003"></i></span></a></td>
+            <td><a href="{% url 'sync-location' loc.location_id %}"><i class="fa fa-refresh" style="font-size:36px;color:#fc9003"></i></span></a></td>
           {% endif %}
         </tr>
       {% endfor %}

+ 2 - 1
dashboard/urls.py

@@ -1,9 +1,10 @@
 from django.urls import path
-from .views import ReviewListByLocationView, UnRepliedReviewList, ReviewListView, ReportView
+from .views import ReviewListByLocationView, UnRepliedReviewList, ReviewListView, ReportView, sync_location
 
 urlpatterns = [
     path('', UnRepliedReviewList.as_view(), name='un-replied-review'),
     path('list/', ReviewListView.as_view(), name='review-list'),
     path('list/<location_id>/', ReviewListByLocationView.as_view(), name='review-list-by-location'),
     path('report/', ReportView.as_view(), name='review-report'),
+    path('report/sync/<location_id>', sync_location, name='sync-location'),
 ]

+ 6 - 0
dashboard/views.py

@@ -5,6 +5,7 @@ from django.views.generic import View
 from review.models import Review, Reply
 from review.forms import ReplyForm
 from gauth.models import Location
+from review.review_utils import sync_all_review
 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 
 
@@ -74,3 +75,8 @@ class ReportView(View):
     def get(self, request, *args, **kwargs):
         locations = Location.objects.all()
         return render(request, 'report.html', {'location_report': locations})
+
+
+def sync_location(request, location_id):
+    sync_all_review(location_id)
+    return redirect('review-report')

+ 0 - 1
review/background_job.py

@@ -25,7 +25,6 @@ def generate_reply(review):
     return replied_text
 
 
-
 def task():
     while True:
         reviews = un_replied_reviews_with_no_comment()

+ 11 - 4
review/review_utils.py

@@ -83,8 +83,12 @@ def insert_review_into_database(unrecorded_reviews, loc):
 
 
 def sync_all_review(loc_id):
+    '''
+    Sync a location if any bad thing occur for example any network break.
+    :param: loc_id -> Location id of a particular location
+    :return: None -> It just update all reviews of this location and return nothing.
+    '''
     loc = Location.objects.get(pk=loc_id)
-    max_date = get_max_date(loc_id)
     next_page_token = ''
     headers = get_auth_header()
     while True:
@@ -95,12 +99,15 @@ def sync_all_review(loc_id):
             continue
         data = res.json()
         reviews = data['reviews']
-        unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
-        if len(unrecorded_reviews) != 0:
-            insert_review_into_database(unrecorded_reviews, loc)
+        if len(reviews) != 0:
+            insert_review_into_database(reviews, loc)
         next_page_token = data.get('nextPageToken')
         if next_page_token is None:
             break
+    average_rating = data.get('averageRating')
+    total_reviews = data.get('totalReviewCount')
+    total_reviews_db = Review.objects.filter(location_id=loc_id).count()
+    update_location_data(loc, average_rating, total_reviews, total_reviews_db)
 
 
 def update_location_data(loc, average_rating, total_reviews, total_reviews_db):

+ 1 - 1
review_automation/settings.py

@@ -10,7 +10,7 @@ SECRET_KEY = '0t@j@klm8b@2rdl@po$$24pirh$&cg#p6f#)@$@n8^kn7k2%9b'
 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True
 
-ALLOWED_HOSTS = []
+ALLOWED_HOSTS = ['10.0.0.17', '127.0.0.1']
 
 
 # Application definition

+ 4 - 0
review_automation/urls.py

@@ -10,3 +10,7 @@ urlpatterns = [
     path('dashboard/', include('dashboard.urls')),
 ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
 
+
+admin.site.site_header = "SignatureCare Review Admin"
+admin.site.site_title = "SignatureCare Admin Portal"
+admin.site.index_title = "Welcome to SignatureCare Review Admin Portal"