Browse Source

Merge branch 'pdf-gen'

Mohidul Islam 5 năm trước cách đây
mục cha
commit
2801b326a8

+ 17 - 0
analytics/send_email.py

@@ -0,0 +1,17 @@
+import requests
+from django.core.mail import EmailMessage
+
+
+def send_email(to_list, subject, message, sender="Byte Trek Ltd. <noreply@bytetrek.com.bd>"):
+    msg = EmailMessage(subject, message, sender, to_list)
+    msg.content_subtype = "html"  # Main content is now text/html
+    return msg.send()
+
+
+to_list = ['ifatmohit@gmail.com']
+subject = 'Last month report'
+message = requests.get('http://127.0.0.1:8000/analytics/monthly-report/16389487648212004696').text
+
+
+def send_mail():
+    send_email(to_list, subject, message)

+ 78 - 0
analytics/templates/last_month_report.html

@@ -0,0 +1,78 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <style>
+      .customers {
+        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
+        border-collapse: collapse;
+        width: 100%;
+
+      }
+
+      .customers td, .customers th {
+        border: 1px solid #ddd;
+        padding: 8px;
+        text-align: center;
+      }
+
+      .customers tr:nth-child(even){background-color: #f2f2f2;}
+
+      .customers tr:hover {background-color: #ddd;}
+
+      .customers th {
+        padding-top: 12px;
+        padding-bottom: 12px;
+        text-align: left;
+        background-color: #a60c15;
+        color: white;
+        text-align: center;
+      }
+    </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 Month Review Report</h2>
+    </div>
+    <div align="center">
+      <h4>Total Review Ratings</h4>
+      <table class="customers">
+      <tr>
+        <th>Star ---></th>
+        <th>One</th>
+        <th>Two</th>
+        <th>Three</th>
+        <th>Four</th>
+        <th>Five</th>
+      </tr>
+      <tr>
+        <td>Last Month</td>
+        {% for r in this_month %}
+          <td>{{ r }}</td>
+        {% endfor %}
+      </tr>
+    </table>
+    </div>
+    <div align="center">
+      <h4>Staffs Report</h4>
+      <table class="customers">
+      <tr>
+        <th>Name</th>
+        <th>Department</th>
+        <th>Total Mentioned</th>
+        <th>Total Unit</th>
+      </tr>
+      <tr>
+        {% for staff in staffs %}
+          <tr>
+            <td>{{ staff.name }}</td>
+            <td>{{ staff.department }}</td>
+            <td>{{ staff.name_mentioned }}</td>
+            <td>{{ staff.total_units }}</td>
+          </tr>
+        {% endfor %}
+      </tr>
+    </table>
+    </div>
+  </body>
+</html>

+ 2 - 1
analytics/urls.py

@@ -1,7 +1,8 @@
 from django.urls import path
-from .views import ChartDataByMonth, AnalyticsData
+from .views import ChartDataByMonth, AnalyticsData, monthly_report
 
 urlpatterns = [
     path('', AnalyticsData.as_view(), name='analytics'),
     path('analytic-data/', ChartDataByMonth.as_view()),
+    path('monthly-report/<location_id>', monthly_report),
 ]

+ 13 - 0
analytics/utils.py

@@ -3,6 +3,19 @@ from review.models import Review
 from django.db.models import Count
 
 
+def last_month_reviews(location_id):
+    now = timezone.now()
+    date = now.replace(day=1)
+    day = (date - timezone.timedelta(days=1)).day + 1
+    prev_date = date - timezone.timedelta(days=day)
+    res = Review.objects.filter(create_time__range=(prev_date, date), location_id=location_id).\
+        values('star_rating').annotate(total=Count('star_rating')).order_by('star_rating')
+    ratings = [0] * 5
+    for r in res:
+        ratings[r.get('star_rating') - 1] = r.get('total')
+    return ratings
+
+
 def last_data(date, location_id):
     res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating')\
         .annotate(total=Count('star_rating')).order_by('star_rating')

+ 14 - 1
analytics/views.py

@@ -4,8 +4,9 @@ from rest_framework.response import Response
 from django.views.generic import View
 
 from gauth.models import Location
+from name_extractor.models import Staff
 
-from .utils import get_review_count_by_month, get_review_count_by_week
+from .utils import get_review_count_by_month, get_review_count_by_week, last_month_reviews
 
 
 class ChartDataByMonth(APIView):
@@ -23,3 +24,15 @@ class AnalyticsData(View):
     def get(self, request, *args, **kwargs):
         locations = Location.objects.all()
         return render(request, 'charts.html', {'location_list': locations})
+
+
+def monthly_report(requests, location_id):
+    last_month_data = last_month_reviews(location_id=location_id)
+
+    staffs = Staff.objects.filter(location_id=location_id).exclude(name_mentioned=0).order_by('-total_units')
+    context = {
+
+        'this_month': last_month_data,
+        'staffs': staffs
+    }
+    return render(requests, 'last_month_report.html', context)

+ 16 - 1
review_automation/settings.py

@@ -103,7 +103,7 @@ AUTH_PASSWORD_VALIDATORS = [
 
 LANGUAGE_CODE = 'en-us'
 
-TIME_ZONE = 'UTC'
+TIME_ZONE = 'Asia/Dhaka'
 
 USE_I18N = True
 
@@ -139,3 +139,18 @@ CRONJOBS = [
 
 # MODEL = spacy.load('en_core_web_md')
 MODEL = 'NLP NER MODEL'
+
+
+# Configure Email Server
+EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
+
+EMAIL_USE_TLS = True
+
+EMAIL_HOST = 'smtp.gmail.com'
+
+EMAIL_HOST_USER = 'username@gmail.com'
+
+# Must generate specific password for your app in [gmail settings][1]
+EMAIL_HOST_PASSWORD = 'mys3cr3tp@ssw0rd'
+
+EMAIL_PORT = 587