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

Added update staff list using datepicker field

Mohidul Islam 4 роки тому
батько
коміт
7485803a2b

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

@@ -179,4 +179,13 @@ body {
 
 .card-body span {
     font-size: 1rem;
+}
+
+
+.bootstrap-datetimepicker-widget table th {
+    height: 20px;
+    line-height: 20px;
+    width: 20px;
+    color: black;
+    font-size: small;
 }

+ 2 - 0
requirements.txt

@@ -9,7 +9,9 @@ cymem==2.0.3
 Django==3.0.4
 django-crispy-forms==1.9.0
 django-crontab==0.7.1
+django-tempus-dominus==5.1.2.13
 djangorestframework==3.11.0
+en-core-web-sm==2.3.1
 facebook-sdk==3.1.0
 google-auth==1.11.2
 google-auth-oauthlib==0.4.1

+ 1 - 0
review_automation/settings/base.py

@@ -21,6 +21,7 @@ DJANGO_DEFAULT_APPS = [
 THIRD_PARTY_APPS = [
     'crispy_forms',
     'django_crontab',
+    'tempus_dominus',
 ]
 
 PROJECTS_APPS = [

+ 39 - 1
user/forms.py

@@ -1,8 +1,15 @@
+from django.utils import timezone
 from django import forms
 from django.contrib.auth.models import User
 from name_extractor.models import Staff
 from django.contrib.auth.forms import UserCreationForm
 
+from tempus_dominus.widgets import DatePicker
+
+NOW = timezone.now()
+TODAY = NOW.date().strftime('%Y-%m-%d')
+ONE_MONTH_BEFORE = (NOW - timezone.timedelta(days=30)).replace(day=NOW.day).date().strftime('%Y-%m-%d')
+
 
 class UserRegisterForm(UserCreationForm):
     email = forms.EmailField()
@@ -16,4 +23,35 @@ class StaffRegistrationForm(forms.ModelForm):
 
     class Meta:
         model = Staff
-        fields = ['name', 'department', 'nick_names']
+        fields = ['name', 'department', 'nick_names']
+
+
+class StaffSheetDateForm(forms.Form):
+    start_date = forms.DateField(
+        required=True,
+        widget=DatePicker(
+            options={
+                'minDate': '2012-01-01',
+                'maxDate': TODAY,
+            },
+            attrs={
+                'append': 'fa fa-calendar',
+                'icon_toggle': True,
+            }
+        ),
+        initial=ONE_MONTH_BEFORE,
+    )
+    end_date = forms.DateField(
+        required=True,
+        widget=DatePicker(
+            options={
+                'minDate': '2012-01-01',
+                'maxDate': TODAY,
+            },
+            attrs={
+                'append': 'fa fa-calendar',
+                'icon_toggle': True,
+            }
+        ),
+        initial=TODAY,
+    )

+ 16 - 1
user/templates/staff_list.html

@@ -3,7 +3,22 @@
 {% block content %}
 
 <!--adding a new staff member.-->
-<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Add a new staff</button>
+  {# Django Tempus Dominus assets are included in `{{ form.media }}` #}
+  {{ date_form.media }}
+    <div>
+      <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Add a new staff</button>
+      <form method="post" class="form" action="{% url 'export-staff-list' %}">
+          {% csrf_token %}
+        <div>
+          {{ date_form.start_date }}
+<!--        </div>-->
+<!--        <div>-->
+          {{ date_form.end_date }}
+        </div>
+        <input type="submit" name="Submit" id="">
+      </form>
+    </div>
+
   <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
     <div class="modal-dialog modal-lg">
       <div class="modal-content">

+ 9 - 0
user/templates/user-base.html

@@ -13,6 +13,15 @@
     <link rel="stylesheet" type="text/css" href="{% static 'user-dashboard.css' %}">
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
 
+
+    {# Include FontAwesome; required for icon display #}
+    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
+
+    {# Include Bootstrap 4 and jQuery #}
+    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
+    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
+
+
   </head>
 
   <body>

+ 2 - 0
user/urls.py

@@ -8,6 +8,7 @@ from .views import (
     ReviewAnalyticsGraph,
     ChartDataAllPlatform,
     StaffLeaderBoard,
+    ExportStaffLeaderBoard,
 )
 
 urlpatterns = [
@@ -20,4 +21,5 @@ urlpatterns = [
     path('api/analytics/all-platform', ChartDataAllPlatform.as_view(), name='location-api-analytics-all-platform'),
     path('graph', ReviewAnalyticsGraph.as_view(), name='location-analytics-graph'),
     path('staff', StaffLeaderBoard.as_view(), name='staff-leaderboard'),
+    path('export-staff-list', ExportStaffLeaderBoard.as_view(), name='export-staff-list'),
 ]

+ 7 - 0
user/utils.py

@@ -254,3 +254,10 @@ def get_review_count_by_month(location_id, platform):
         'curr_month': curr_month
     }
     return response
+
+
+def date_str2datetime(date):
+    year, month, day = date.split('-')
+    date = timezone.datetime(year=int(year), month=int(month), day=int(day))
+    dt_aware = timezone.make_aware(date, timezone.get_current_timezone())
+    return dt_aware

+ 22 - 2
user/views.py

@@ -2,7 +2,7 @@ from django.http import Http404
 from django.shortcuts import render, redirect
 from django.views.generic import View
 from django.contrib import messages
-from .forms import UserRegisterForm, StaffRegistrationForm
+from .forms import UserRegisterForm, StaffRegistrationForm, StaffSheetDateForm
 from .models import UserAccount
 from gauth.models import Location
 from review.models import Review
@@ -11,13 +11,16 @@ from yelp.models import YelpReview
 from name_extractor.models import Staff
 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 
+from name_extractor.utils import extract_names_from_reviews
+
 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,
-    get_review_count_by_month
+    get_review_count_by_month,
+    date_str2datetime
 )
 from rest_framework.views import APIView
 from rest_framework.response import Response
@@ -133,9 +136,11 @@ class StaffLeaderBoard(View):
     def get(self, request, *args, **kwargs):
         staffs = Staff.objects.filter(location=request.user.useraccount.location).order_by('-total_units')
         form = StaffRegistrationForm()
+        date_form = StaffSheetDateForm()
 
         context = {
             'staffs': staffs,
+            'date_form': date_form,
             'form': form
         }
         return render(request, 'staff_list.html', context)
@@ -155,3 +160,18 @@ class StaffLeaderBoard(View):
             )
             messages.success(request, f'A new staff {staff} has been created!')
         return redirect('location-analytics')
+
+
+class ExportStaffLeaderBoard(View):
+
+    def post(self, request, *args, **kwargs):
+        start_date = date_str2datetime(request.POST.get('start_date'))
+        end_date = date_str2datetime(request.POST.get('end_date'))
+        location_id = request.user.useraccount.location_id
+
+        extract_names_from_reviews(
+            start_date=start_date,
+            end_date=end_date,
+            location_id=location_id
+        )
+        return redirect('staff-leaderboard')