Переглянути джерело

Added three field into location table and app-report view handled

Mohidul Islam 5 роки тому
батько
коміт
96a2d8be9f

+ 1 - 1
dashboard/static/main.css

@@ -93,4 +93,4 @@ a.article-title:hover {
    max-height: 90%;
    overflow: auto;
    width: 18%;
-}
+}

+ 6 - 6
dashboard/templates/base.html

@@ -8,8 +8,8 @@
 
     <!-- Bootstrap CSS -->
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
-
     <link rel="stylesheet" type="text/css" href="{% static 'main.css' %}">
+    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
 
 </head>
 <body>
@@ -28,20 +28,21 @@
             <!-- Navbar Right Side -->
             <div class="navbar-nav">
                 <a class="nav-item nav-link" href="#">Analytics</a>
-                <a class="nav-item nav-link" href="#">App Report</a>
+                <a class="nav-item nav-link" href="{% url 'review-report' %}">App Report</a>
             </div>
           </div>
         </div>
       </nav>
         {% if messages %}
         {% for message in messages %}
-              <div class="alert alert-{{ message.tags }}">
-                {{ message }}
-              </div>
+        <div class="alert alert-{{ message.tags }}">
+            {{ message }}
+        </div>
         {% endfor %}
         {% endif %}
     </header>
     <main role="main" class="container mb-5">
+        {% block report %} {% endblock %}
       <div class="row">
         <div class="col-md-8">
           {% block content %}{% endblock %}
@@ -68,6 +69,5 @@
     <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
-
 </body>
 </html>

+ 30 - 0
dashboard/templates/report.html

@@ -0,0 +1,30 @@
+{% extends 'base.html' %}
+{% block report %}
+  <h2 align="center" class="mt-2 mb-4">App Report</h2>
+    <table class="table">
+      <thead class="thead-dark" style="text-align: center">
+        <tr>
+          <th scope="col">Location Name</th>
+          <th scope="col">Average Rating</th>
+          <th scope="col">Total Review (Google API)</th>
+          <th scope="col">Total Review (on DB)</th>
+          <th scope="col">Status</th>
+        </tr>
+      </thead>
+      <tbody style="text-align: center">
+      {% for loc in location_report %}
+        <tr>
+          <th scope="row">{{ loc.care_name }}</th>
+          <td>{{ loc.average_rating }}</td>
+          <td>{{ loc.total_review }}</td>
+          <td>{{ loc.total_review_DB }}</td>
+          {% 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>
+          {% endif %}
+        </tr>
+      {% endfor %}
+      </tbody>
+    </table>
+{% endblock %}

+ 2 - 1
dashboard/urls.py

@@ -1,8 +1,9 @@
 from django.urls import path
-from .views import ReviewListByLocationView, UnRepliedReviewList, ReviewListView
+from .views import ReviewListByLocationView, UnRepliedReviewList, ReviewListView, ReportView
 
 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'),
 ]

+ 7 - 0
dashboard/views.py

@@ -67,3 +67,10 @@ class UnRepliedReviewList(View):
         date = now - timezone.timedelta(days=30)
         reviews = Review.objects.filter(reply=None, update_time__gte=date).order_by('-update_time')
         return render(request, 'dashboard.html', {'reviews': reviews, 'form': form})
+
+
+class ReportView(View):
+
+    def get(self, request, *args, **kwargs):
+        locations = Location.objects.all()
+        return render(request, 'report.html', {'location_report': locations})

+ 23 - 0
gauth/migrations/0007_auto_20200109_0638.py

@@ -0,0 +1,23 @@
+# Generated by Django 3.0 on 2020-01-09 06:38
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('gauth', '0006_location_care_name'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='location',
+            name='average_rating',
+            field=models.FloatField(blank=True, null=True),
+        ),
+        migrations.AddField(
+            model_name='location',
+            name='total_review',
+            field=models.IntegerField(blank=True, null=True),
+        ),
+    ]

+ 18 - 0
gauth/migrations/0008_location_total_review_db.py

@@ -0,0 +1,18 @@
+# Generated by Django 3.0 on 2020-01-09 07:02
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('gauth', '0007_auto_20200109_0638'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='location',
+            name='total_review_DB',
+            field=models.IntegerField(blank=True, null=True),
+        ),
+    ]

+ 3 - 0
gauth/models.py

@@ -17,6 +17,9 @@ class Location(models.Model):
     location_name = models.CharField(max_length=120)
     website_url = models.URLField()
     display_name = models.CharField(max_length=120)
+    average_rating = models.FloatField(null=True, blank=True)
+    total_review = models.IntegerField(null=True, blank=True)
+    total_review_DB = models.IntegerField(null=True, blank=True)
     owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
 
     def __str__(self):

+ 1 - 1
review/admin.py

@@ -15,7 +15,7 @@ class CustomReplyAdmin(admin.ModelAdmin):
 
 
 class ReplyAdmin(admin.ModelAdmin):
-    list_display = ('replied_text',)
+    list_display = ('id', 'review', 'replied_text',)
     ordering = ['-create_time']
 
 

+ 1 - 1
review/background_job.py

@@ -9,7 +9,7 @@ from .review_utils import populate_reviews
 def un_replied_reviews_with_no_comment():
     now = timezone.now()
     date = now - timezone.timedelta(days=30)
-    reviews = Review.objects.filter(comment=None, reply=None, star_rating=2, update_time__gte=date).order_by('update_time')
+    reviews = Review.objects.filter(comment=None, reply=None, star_rating=5, update_time__gte=date).order_by('update_time')
     return reviews
 
 

+ 27 - 1
review/review_utils.py

@@ -82,7 +82,7 @@ def insert_review_into_database(unrecorded_reviews, loc):
         review.save()
 
 
-def fetch_all_review(loc_id):
+def sync_all_review(loc_id):
     loc = Location.objects.get(pk=loc_id)
     max_date = get_max_date(loc_id)
     next_page_token = ''
@@ -103,6 +103,32 @@ def fetch_all_review(loc_id):
             break
 
 
+def update_location_data(loc, average_rating, total_reviews, total_reviews_db):
+    loc.average_rating = average_rating
+    loc.total_review = total_reviews
+    loc.total_review_DB = total_reviews_db
+    loc.save()
+
+
+def fetch_all_review(loc_id):
+    loc = Location.objects.get(pk=loc_id)
+    max_date = get_max_date(loc_id)
+    headers = get_auth_header()
+    url = get_review_list_url(loc_id)
+    res = get(url, headers=headers)
+    if res.status_code == 401:
+        return
+    data = res.json()
+    reviews = data['reviews']
+    average_rating = data.get('averageRating')
+    total_reviews = data.get('totalReviewCount')
+    unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
+    if len(unrecorded_reviews) != 0:
+        insert_review_into_database(unrecorded_reviews, loc)
+    total_reviews_db = Review.objects.filter(location_id=loc_id).count()
+    update_location_data(loc, average_rating, total_reviews, total_reviews_db)
+
+
 def populate_reviews():
     start = timezone.now()
     locations = get_all_location_ids()