Browse Source

Dashboard for location owner and review list has done

Mohidul Islam 4 years ago
parent
commit
87cb1991b4

+ 18 - 0
dashboard/static/user-dashboard.css

@@ -129,4 +129,22 @@ body {
     font-size: medium;
     font-size: medium;
     font-family: revert;
     font-family: revert;
     font-weight: unset;
     font-weight: unset;
+}
+
+.rounded-circle {
+    height: 60px;
+    margin-right: 1rem;
+}
+
+.review-content {
+    display: flex;
+    border: 1px solid #e2e2e2;
+    padding: 1rem;
+    margin: 1rem;
+    background: #f7f9f9;
+}
+
+.review-header-divider {
+    margin-bottom: 10px;
+    border-bottom: 1px solid #dadada;
 }
 }

+ 21 - 0
user/templates/_facebook_review.html

@@ -0,0 +1,21 @@
+<div class="review-content">
+  <div style="flex-grow: 10">
+    <div class="review-header-divider">
+      {% if review.recommendation_type == True %}
+        <span style="color: green">Recommended</span>
+      {% else %}
+        <span style="color: red">Not Recommended</span>
+      {% endif %}
+      <span class="text-muted mb-2">at {{ review.create_time|date:"F d, Y" }}</span>
+      <span style="float: right"> <a href="https://facebook.com/{{ review.id }}">Link</a></span>
+    </div>
+    {% if review.review_text %}
+        <p >{{ review.review_text }}</p>
+    {% endif %}
+    {% if review.facebookreviewreply.replied_text %}
+        <p class="alert alert-success">Reply: {{ review.facebookreviewreply.replied_text }}</p>
+    {% else %}
+     <p class="alert alert-warning">Not Replied Yet!</p>
+    {% endif %}
+  </div>
+</div>

+ 20 - 0
user/templates/_google_review.html

@@ -0,0 +1,20 @@
+<div class="review-content">
+  <div>
+  <img class="rounded-circle article-img" src="{{ review.reviewer_photo }}">
+  </div>
+    <div style="flex-grow: 10">
+    <div class="review-header-divider">
+      <span class="mr-2"><b>{{ review.reviewer_name }}</b></span>
+      <small class="text-muted">{{ review.update_time|date:"F d, Y" }}</small>
+      <span style="color: #a41515; float: right">{{ review.star_rating }} X &#9733</span>
+    </div>
+    {% if review.comment %}
+        <p >{{ review.comment }}</p>
+    {% endif %}
+    {% if review.reply.replied_text %}
+        <p class="alert alert-success">Reply: {{ review.reply.replied_text }}</p>
+    {% else %}
+     <p class="alert alert-warning">Not Replied Yet!</p>
+    {% endif %}
+  </div>
+</div>

+ 13 - 17
user/templates/_navbar.html

@@ -5,23 +5,19 @@
       <img class="img-thumbnail" align="center" width="100%" src="https://ercare24.com/wp-content/uploads/2016/07/signature-care-resized-e1462918690585.png">
       <img class="img-thumbnail" align="center" width="100%" src="https://ercare24.com/wp-content/uploads/2016/07/signature-care-resized-e1462918690585.png">
 
 
       <p><b>SignatureCare Emergency Center</b></p>
       <p><b>SignatureCare Emergency Center</b></p>
-      <p><i class="fa fa-map-marker" aria-hidden="true"></i>Mission Bend Sugar Land</p>
-      <p><i class="fa fa-envelope" aria-hidden="true"></i>missionbend@ercare24.com</p>
+
+      <p><i class="fa fa-map-marker" aria-hidden="true"></i>{{ user.useraccount.location.care_name }}</p>
+      <p><i class="fa fa-envelope" aria-hidden="true"></i>{{ user.email }}</p>
     </div>
     </div>
+   <textarea hidden id="location_id"> {{ user.useraccount.location_id }}</textarea>
 
 
     <hr>
     <hr>
 
 
     <ul class="nav flex-column">
     <ul class="nav flex-column">
       <li class="nav-item">
       <li class="nav-item">
-        <a class="nav-link active" href="#">
+        <a class="nav-link" href="{% url 'location-analytics' %}">
           <span data-feather="home"></span>
           <span data-feather="home"></span>
-          Dashboard <span class="sr-only">(current)</span>
-        </a>
-      </li>
-      <li class="nav-item">
-        <a class="nav-link" href="#">
-          <span data-feather="list"></span>
-          Review List
+          Dashboard</span>
         </a>
         </a>
       </li>
       </li>
       <li class="nav-item">
       <li class="nav-item">
@@ -45,28 +41,28 @@
     </ul>
     </ul>
 
 
     <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
     <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
-      <span>Saved reports</span>
+      <span>Review List</span>
       <a class="d-flex align-items-center text-muted" href="#">
       <a class="d-flex align-items-center text-muted" href="#">
         <span data-feather="plus-circle"></span>
         <span data-feather="plus-circle"></span>
       </a>
       </a>
     </h6>
     </h6>
     <ul class="nav flex-column mb-2">
     <ul class="nav flex-column mb-2">
       <li class="nav-item">
       <li class="nav-item">
-        <a class="nav-link" href="#">
+        <a class="nav-link" href="{% url 'location-wise-review-list' 'google' %}">
           <span data-feather="file-text"></span>
           <span data-feather="file-text"></span>
-          Current month
+          Google
         </a>
         </a>
       </li>
       </li>
       <li class="nav-item">
       <li class="nav-item">
-        <a class="nav-link" href="#">
+        <a class="nav-link" href="{% url 'location-wise-review-list' 'facebook' %}">
           <span data-feather="file-text"></span>
           <span data-feather="file-text"></span>
-          Last quarter
+          Facebook
         </a>
         </a>
       </li>
       </li>
       <li class="nav-item">
       <li class="nav-item">
-        <a class="nav-link" href="#">
+        <a class="nav-link" href="{% url 'location-wise-review-list' 'yelp' %}">
           <span data-feather="file-text"></span>
           <span data-feather="file-text"></span>
-          Social engagement
+          Yelp
         </a>
         </a>
       </li>
       </li>
     </ul>
     </ul>

+ 12 - 0
user/templates/_yelp_review.html

@@ -0,0 +1,12 @@
+<div class="review-content">
+  <div style="flex-grow: 10">
+    <div class="review-header-divider">
+      <span class="mr-2"><b>{{ review.reviewer_name }}</b></span>
+      <small class="text-muted">{{ review.date_posted|date:"F d, Y" }}</small>
+      <span style="color: #a41515; float: right">{{ review.rating }} X &#9733</span>
+    </div>
+    {% if review.comment %}
+        <p >{{ review.comment }}</p>
+    {% endif %}
+  </div>
+</div>

+ 38 - 0
user/templates/review-list.html

@@ -0,0 +1,38 @@
+{% extends 'user-base.html' %}
+{% block content %}
+<h2 style="text-align: center;">All reviews posted on {{ platform|title }}</h2>
+{% for review in reviews %}
+  {% if platform == 'google' %}
+    {% include '_google_review.html' %}
+  {% elif platform == 'facebook' %}
+    {% include '_facebook_review.html' %}
+  {% elif platform == 'yelp' %}
+    {% include '_yelp_review.html' %}
+  {% endif %}
+{% endfor %}
+
+<div align="center">
+  {% if reviews.has_other_pages %}
+
+    {% if reviews.has_previous %}
+      <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
+      <a class="btn btn-outline-info mb-4" href="?page={{ reviews.previous_page_number }}">Previous</a>
+    {% endif %}
+
+    {% for num in reviews.paginator.page_range %}
+      {% if reviews.number == num %}
+        <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
+      {% elif num > reviews.number|add:'-3' and num < reviews.number|add:'3' %}
+        <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
+      {% endif %}
+    {% endfor %}
+
+    {% if reviews.has_next %}
+      <a class="btn btn-outline-info mb-4" href="?page={{ reviews.next_page_number }}">Next</a>
+      <a class="btn btn-outline-info mb-4" href="?page={{ reviews.paginator.num_pages }}">Last</a>
+    {% endif %}
+
+  {% endif %}
+</div>
+
+{% endblock %}

+ 89 - 43
user/templates/user-dashboard.html

@@ -1,6 +1,7 @@
 {% extends 'user-base.html' %}
 {% extends 'user-base.html' %}
 {% block content %}
 {% block content %}
 
 
+
 <!--cart-->
 <!--cart-->
 <div class="row">
 <div class="row">
 
 
@@ -11,11 +12,11 @@
           <table class="table">
           <table class="table">
             <tr>
             <tr>
               <td><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  Total Review</td>
               <td><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  Total Review</td>
-              <td>200</td>
+              <td>{{ google_total }}</td>
             </tr>
             </tr>
             <tr>
             <tr>
               <td><i class="fa fa-line-chart" aria-hidden="true"></i>  Growth</td>
               <td><i class="fa fa-line-chart" aria-hidden="true"></i>  Growth</td>
-              <td>-2.4%</td>
+              <td>{{ google_total_growth }} %</td>
             </tr>
             </tr>
           </table>
           </table>
         </div>
         </div>
@@ -29,11 +30,11 @@
           <table class="table">
           <table class="table">
             <tr>
             <tr>
               <td><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  Total Review</td>
               <td><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  Total Review</td>
-              <td>200</td>
+              <td>{{ yelp_total }}</td>
             </tr>
             </tr>
             <tr>
             <tr>
               <td><i class="fa fa-line-chart" aria-hidden="true"></i>  Growth</td>
               <td><i class="fa fa-line-chart" aria-hidden="true"></i>  Growth</td>
-              <td>-2.4%</td>
+              <td>{{ yelp_total_growth }}%</td>
             </tr>
             </tr>
           </table>
           </table>
         </div>
         </div>
@@ -47,11 +48,11 @@
           <table class="table">
           <table class="table">
             <tr>
             <tr>
               <td><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  Total Review</td>
               <td><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  Total Review</td>
-              <td>200</td>
+              <td>{{ facebook_total }}</td>
             </tr>
             </tr>
             <tr>
             <tr>
               <td><i class="fa fa-line-chart" aria-hidden="true"></i>  Growth</td>
               <td><i class="fa fa-line-chart" aria-hidden="true"></i>  Growth</td>
-              <td>-2.4%</td>
+              <td>{{ facebook_total_growth }} %</td>
             </tr>
             </tr>
           </table>
           </table>
         </div>
         </div>
@@ -80,80 +81,125 @@
           <td>Google</td>
           <td>Google</td>
           <td>{{ google_pos }}</td>
           <td>{{ google_pos }}</td>
           {% if google_pos_gr > 0 %}
           {% if google_pos_gr > 0 %}
-            <td class="table-success"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ google_pos_gr }}</td>
+            <td class="table-success"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ google_pos_gr }} %</td>
           {% else %}
           {% else %}
-            <td class="table-danger"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ google_pos_gr }};</td>
+            <td class="table-danger"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ google_pos_gr }} %</td>
           {% endif %}
           {% endif %}
           <td>{{ google_neg }}</td>
           <td>{{ google_neg }}</td>
           {% if google_neg_gr < 0 %}
           {% if google_neg_gr < 0 %}
-            <td class="table-success"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ google_pos_gr }}</td>
+            <td class="table-success"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ google_neg_gr }} %</td>
           {% else %}
           {% else %}
-            <td class="table-danger"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ google_pos_gr }};</td>
+            <td class="table-danger"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ google_neg_gr }} %</td>
           {% endif %}
           {% endif %}
         </tr>
         </tr>
         <tr>
         <tr>
           <td>Yelp</td>
           <td>Yelp</td>
           <td>{{ yelp_pos }}</td>
           <td>{{ yelp_pos }}</td>
           {% if yelp_pos_gr > 0 %}
           {% if yelp_pos_gr > 0 %}
-            <td class="table-success"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ yelp_pos_gr }}</td>
+            <td class="table-success"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ yelp_pos_gr }} %</td>
           {% else %}
           {% else %}
-            <td class="table-danger"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ yelp_pos_gr }};</td>
+            <td class="table-danger"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ yelp_pos_gr }} %</td>
           {% endif %}
           {% endif %}
           <td>{{ yelp_neg }}</td>
           <td>{{ yelp_neg }}</td>
           {% if yelp_neg_gr < 0 %}
           {% if yelp_neg_gr < 0 %}
-            <td class="table-success"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ yelp_pos_gr }}</td>
+            <td class="table-success"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ yelp_neg_gr }} %</td>
           {% else %}
           {% else %}
-            <td class="table-danger"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ yelp_pos_gr }};</td>
+            <td class="table-danger"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ yelp_neg_gr }} %</td>
           {% endif %}
           {% endif %}
         </tr>
         </tr>
-        <tr>
+        <tr style="border-bottom: 1px solid #dee2e6">
           <td>Facebook</td>
           <td>Facebook</td>
           <td>{{ facebook_pos }}</td>
           <td>{{ facebook_pos }}</td>
           {% if facebook_pos_gr > 0 %}
           {% if facebook_pos_gr > 0 %}
-            <td class="table-success"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ facebook_pos_gr }}</td>
+            <td class="table-success"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ facebook_pos_gr }} %</td>
           {% else %}
           {% else %}
-            <td class="table-danger"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ facebook_pos_gr }};</td>
+            <td class="table-danger"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ facebook_pos_gr }} %</td>
           {% endif %}
           {% endif %}
           <td>{{ facebook_neg }}</td>
           <td>{{ facebook_neg }}</td>
           {% if facebook_neg_gr < 0 %}
           {% if facebook_neg_gr < 0 %}
-            <td class="table-success"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ facebook_pos_gr }}</td>
+            <td class="table-success"><i class="fa fa-arrow-circle-o-down mr-2" aria-hidden="true"></i>{{ facebook_neg_gr }} %</td>
           {% else %}
           {% else %}
-            <td class="table-danger"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ facebook_pos_gr }};</td>
+            <td class="table-danger"><i class="fa fa-arrow-circle-o-up mr-2" aria-hidden="true"></i>{{ facebook_neg_gr }} %</td>
           {% endif %}
           {% endif %}
         </tr>
         </tr>
       </tbody>
       </tbody>
     </table>
     </table>
   </div>
   </div>
 
 
-
+<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
+<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js" integrity="sha256-TQq84xX6vkwR0Qs1qH5ADkP+MvH0W+9E7TdHJsoIQiM=" crossorigin="anonymous"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
 
 
 <script>
 <script>
   var ctx = document.getElementById("myChart");
   var ctx = document.getElementById("myChart");
-  var myChart = new Chart(ctx, {
-    type: 'line',
-    data: {
-      labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
-      datasets: [{
-        data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
-        lineTension: 0,
-        backgroundColor: 'transparent',
-        borderColor: '#007bff',
-        borderWidth: 4,
-        pointBackgroundColor: '#007bff'
-      }]
+  var location_id = document.getElementById("location_id").value;
+  console.log(location_id.value)
+  var endpoint = '/user/api/analytics';
+  params = {
+      "location_id":location_id.trim()
+  }
+  console.log(params)
+  $.ajax({
+        type: "get",
+        url: endpoint,
+        data: params,
+        dataType: 'json',
+        success: function(data) {
+        console.log(data)
+          var myBarChart = new Chart(ctx, {
+          type: 'bar',
+          data: {
+              labels: data.label,
+              datasets: [
+                  {
+                    label: 'Google',
+                    data: data.google,
+                    borderColor: 'rgb(255, 204, 0)',
+                    backgroundColor: 'rgb(255, 204, 0)',
+                    borderWidth: 1,
+                    fill: false
+                },
+                {
+                    label: 'Facebook',
+                    data: data.facebook,
+                    borderColor: 'rgb(0, 51, 204)',
+                    backgroundColor: 'rgb(0, 51, 204)',
+                    borderWidth: 1,
+                    fill: false
+                },
+                {
+                    label: 'Yelp',
+                    data: data.yelp,
+                    borderColor: 'rgb(204, 51, 0)',
+                    backgroundColor: 'rgb(204, 51, 0)',
+                    borderWidth: 1,
+                    fill: false
+                }]
+            },
+            options: {
+                scales: {
+                    yAxes: [{
+                        ticks: {
+                            beginAtZero: true
+                        }
+                    }],
+                    xAxes: [{
+                        gridLines: {
+                            drawOnChartArea:false
+                        }
+                    }]
+                },
+                title: {
+                    display: true,
+                    fontSize: 16,
+                    text: "Review of this month in all platforms."
+                }
+            }
+        });
     },
     },
-    options: {
-      scales: {
-        yAxes: [{
-          ticks: {
-            beginAtZero: false
-          }
-        }]
-      },
-      legend: {
-        display: false,
-      }
+
+    error: function(error_data) {
+       console.log(error_data);
     }
     }
   });
   });
 </script>
 </script>

+ 3 - 1
user/urls.py

@@ -1,10 +1,12 @@
 from django.urls import path
 from django.urls import path
 from django.contrib.auth import views as auth_views
 from django.contrib.auth import views as auth_views
-from .views import RegistrationView, LocationAnalytics
+from .views import RegistrationView, LocationAnalytics, ChartDataThisMonth, ReviewListLocationWise
 
 
 urlpatterns = [
 urlpatterns = [
     path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
     path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
     path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
     path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
     path('signup', RegistrationView.as_view(), name='registration'),
     path('signup', RegistrationView.as_view(), name='registration'),
     path('analytics', LocationAnalytics.as_view(), name='location-analytics'),
     path('analytics', LocationAnalytics.as_view(), name='location-analytics'),
+    path('reviews/<platform>', ReviewListLocationWise.as_view(), name='location-wise-review-list'),
+    path('api/analytics', ChartDataThisMonth.as_view(), name='location-api-analytics'),
 ]
 ]

+ 164 - 0
user/utils.py

@@ -0,0 +1,164 @@
+from django.utils import timezone
+from django.db.models import Count
+from review.models import Review
+from facebook_app.models import FacebookReview
+from yelp.models import YelpReview
+
+
+now = timezone.now()
+beginning_of_month = now.replace(day=1, hour=0, minute=0, second=0)
+beginning_of_last_month = now - timezone.timedelta(days=now.day+31)
+
+
+def get_google_review_report(location_id):
+    pos = Review.objects.filter(
+        location_id=location_id,
+        create_time__range=(beginning_of_month, now),
+        star_rating__gte=3
+    ).count()
+    neg = Review.objects.filter(
+        location_id=location_id,
+        create_time__range=(beginning_of_month, now),
+        star_rating__lte=2
+    ).count()
+    pos_last_month = Review.objects.filter(
+        location_id=location_id,
+        create_time__range=(beginning_of_last_month, beginning_of_month),
+        star_rating__gte=3
+    ).count()
+    neg_last_month = Review.objects.filter(
+        location_id=location_id,
+        create_time__range=(beginning_of_last_month, beginning_of_month),
+        star_rating__lte=2
+    ).count()
+    pos_growth = 'inf' if pos_last_month == 0 else round(((((pos / now.day) * 30) - pos_last_month) / pos_last_month) * 100, 2)
+    neg_growth = 'inf' if neg_last_month == 0 else round(((((neg / now.day) * 30) - neg_last_month) / neg_last_month) * 100, 2)
+    total = pos + neg
+    last_month_total = pos_last_month + neg_last_month
+    total_growth = 'inf' if last_month_total == 0 else round(((((total / now.day) * 30) - last_month_total) / last_month_total) * 100, 2)
+
+    return {
+        'positive': pos,
+        'positive_growth': pos_growth,
+        'negative': neg,
+        'negative_growth': neg_growth,
+        'total': total,
+        'total_growth': total_growth
+    }
+
+
+def get_facebook_report(location_id):
+    pos = FacebookReview.objects.filter(
+        page__location_id=location_id,
+        create_time__range=(beginning_of_month, now),
+        recommendation_type=True
+    ).count()
+    neg = FacebookReview.objects.filter(
+        page__location_id=location_id,
+        create_time__range=(beginning_of_month, now),
+        recommendation_type=False
+    ).count()
+    pos_last_month = FacebookReview.objects.filter(
+        page__location_id=location_id,
+        create_time__range=(beginning_of_last_month, beginning_of_month),
+        recommendation_type=True
+    ).count()
+    neg_last_month = FacebookReview.objects.filter(
+        page__location_id=location_id,
+        create_time__range=(beginning_of_last_month, beginning_of_month),
+        recommendation_type=False
+    ).count()
+    pos_growth = 'inf' if pos_last_month == 0 else round(((((pos / now.day) * 30) - pos_last_month) / pos_last_month) * 100, 2)
+    neg_growth = 'inf' if neg_last_month == 0 else round(((((neg / now.day) * 30) - neg_last_month) / neg_last_month) * 100, 2)
+    total = pos + neg
+    last_month_total = pos_last_month + neg_last_month
+    total_growth = 'inf' if last_month_total == 0 else round(((((total / now.day) * 30) - last_month_total) / last_month_total) * 100, 2)
+
+    return {
+        'positive': pos,
+        'positive_growth': pos_growth,
+        'negative': neg,
+        'negative_growth': neg_growth,
+        'total': total,
+        'total_growth': total_growth
+    }
+
+
+def get_yelp_review_report(location_id):
+    pos = YelpReview.objects.filter(
+        location__location_id=location_id,
+        date_posted__range=(beginning_of_month, now),
+        rating__gte=3
+    ).count()
+    neg = YelpReview.objects.filter(
+        location__location_id=location_id,
+        date_posted__range=(beginning_of_month, now),
+        rating__lte=2
+    ).count()
+    pos_last_month = YelpReview.objects.filter(
+        location__location_id=location_id,
+        date_posted__range=(beginning_of_last_month, beginning_of_month),
+        rating__gte=3
+    ).count()
+    neg_last_month = YelpReview.objects.filter(
+        location__location_id=location_id,
+        date_posted__range=(beginning_of_last_month, beginning_of_month),
+        rating__lte=2
+    ).count()
+    pos_growth = 'inf' if pos_last_month == 0 else round(((((pos / now.day) * 30) - pos_last_month) / pos_last_month) * 100, 2)
+    neg_growth = 'inf' if neg_last_month == 0 else round(((((neg / now.day) * 30) - neg_last_month) / neg_last_month) * 100, 2)
+    total = pos + neg
+    last_month_total = pos_last_month + neg_last_month
+    total_growth = 'inf' if last_month_total == 0 else round(((((total / now.day) * 30) - last_month_total) / last_month_total) * 100, 2)
+
+    return {
+        'positive': pos,
+        'positive_growth': pos_growth,
+        'negative': neg,
+        'negative_growth': neg_growth,
+        'total': total,
+        'total_growth': total_growth
+    }
+
+
+def get_this_month_analytics(location_id):
+    google_qs = Review.objects.filter(
+        create_time__range=(beginning_of_month, now),
+        location_id=location_id
+    )\
+        .values('create_time__day')\
+        .annotate(total=Count('create_time__day'))
+    google_qs_dict = {q['create_time__day']: q['total'] for q in google_qs}
+
+    yelp_qs = YelpReview.objects.filter(
+        location__location_id=location_id,
+        date_posted__range=(beginning_of_month, now),
+    )\
+        .values('date_posted__day')\
+        .annotate(total=Count('date_posted__day'))
+    yelp_qs_dict = {q['date_posted__day']: q['total'] for q in yelp_qs}
+
+    facebook_qs = FacebookReview.objects.filter(
+        page__location_id=location_id,
+        create_time__range=(beginning_of_month, now),
+    )\
+        .values('create_time__day')\
+        .annotate(total=Count('create_time__day'))
+    facebook_qs_dict = {q['create_time__day']: q['total'] for q in facebook_qs}
+
+    label = []
+    facebook = []
+    yelp = []
+    google = []
+    for day in range(1, now.day+1):
+        label.append(day)
+        facebook.append(facebook_qs_dict.get(day, 0))
+        yelp.append(yelp_qs_dict.get(day, 0))
+        google.append(google_qs_dict.get(day, 0))
+
+    return {
+        'label': label,
+        'google': google,
+        'facebook': facebook,
+        'yelp': yelp
+    }

+ 74 - 15
user/views.py

@@ -1,9 +1,24 @@
+from django.http import Http404
 from django.shortcuts import render, redirect
 from django.shortcuts import render, redirect
 from django.views.generic import View
 from django.views.generic import View
 from django.contrib import messages
 from django.contrib import messages
 from .forms import UserRegisterForm
 from .forms import UserRegisterForm
 from .models import UserAccount
 from .models import UserAccount
 from gauth.models import Location
 from gauth.models import Location
+from review.models import Review
+from facebook_app.models import FacebookReview
+from yelp.models import YelpReview
+from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
+
+from django.contrib.auth.mixins import LoginRequiredMixin
+from .utils import (
+    get_google_review_report,
+    get_facebook_report,
+    get_yelp_review_report,
+    get_this_month_analytics
+)
+from rest_framework.views import APIView
+from rest_framework.response import Response
 
 
 
 
 class RegistrationView(View):
 class RegistrationView(View):
@@ -28,20 +43,64 @@ class RegistrationView(View):
         return render(request, 'signup.html', {'form': form})
         return render(request, 'signup.html', {'form': form})
 
 
 
 
-class LocationAnalytics(View):
-    def get(self, requests, *args, **kwargs):
+class LocationAnalytics(LoginRequiredMixin, View):
+    def get(self, request, *args, **kwargs):
+        # print(dir(request.user))
+        user_location_id = request.user.useraccount.location_id
+
+        google_report = get_google_review_report(user_location_id)
+        facebook_report = get_facebook_report(user_location_id)
+        yelp_report = get_yelp_review_report(user_location_id)
+
         context = {
         context = {
-            'google_pos': 400,
-            'google_neg': 60,
-            'google_pos_gr': -5,
-            'google_neg_gr': -3,
-            'yelp_pos': 323,
-            'yelp_neg': 32,
-            'yelp_pos_gr': -2.4,
-            'yelp_neg_gr': 4,
-            'facebook_pos': 430,
-            'facebook_neg': 40,
-            'facebook_pos_gr': 2.5,
-            'facebook_neg_gr': -2.3,
+            'google_pos': google_report.get('positive'),
+            'google_pos_gr': google_report.get('positive_growth'),
+            'google_neg': google_report.get('negative'),
+            'google_neg_gr': google_report.get('negative_growth'),
+            'google_total': google_report.get('total'),
+            'google_total_growth': google_report.get('total_growth'),
+            'yelp_pos': yelp_report.get('positive'),
+            'yelp_pos_gr': yelp_report.get('positive_growth'),
+            'yelp_neg': yelp_report.get('negative'),
+            'yelp_neg_gr': yelp_report.get('negative_growth'),
+            'yelp_total': yelp_report.get('total'),
+            'yelp_total_growth': yelp_report.get('total_growth'),
+            'facebook_pos': facebook_report.get('positive'),
+            'facebook_pos_gr': facebook_report.get('positive_growth'),
+            'facebook_neg': facebook_report.get('negative'),
+            'facebook_neg_gr': facebook_report.get('negative_growth'),
+            'facebook_total': facebook_report.get('total'),
+            'facebook_total_growth': facebook_report.get('total_growth'),
         }
         }
-        return render(requests, 'user-dashboard.html', context=context)
+        return render(request, 'user-dashboard.html', context=context)
+
+
+class ChartDataThisMonth(APIView):
+    def get(self, request, *args, **kwargs):
+        location_id = request.GET['location_id']
+        res = get_this_month_analytics(location_id)
+        return Response(res)
+
+
+class ReviewListLocationWise(View):
+    def get(self, request, platform,  *args, **kwargs):
+        location_id = request.user.useraccount.location_id
+        if platform == 'google':
+            reviews = Review.objects.filter(location_id=location_id).order_by('-update_time')
+        elif platform == 'facebook':
+            reviews = FacebookReview.objects.filter(page__location_id=location_id).order_by('-create_time')
+        elif platform == 'yelp':
+            reviews = YelpReview.objects.filter(location__location_id=location_id).order_by('-date_posted')
+        else:
+            raise Http404()
+
+        page = request.GET.get('page', 1)
+        paginator = Paginator(reviews, 50)
+        try:
+            reviews = paginator.page(page)
+        except PageNotAnInteger:
+            reviews = paginator.page(1)
+        except EmptyPage:
+            reviews = paginator.page(paginator.num_pages)
+        context = {'reviews': reviews, 'platform': platform}
+        return render(request, 'review-list.html', context=context)