Jelajahi Sumber

added import excel sheet of staff list

Mohidul Islam 4 tahun lalu
induk
melakukan
5a221c7f7a
4 mengubah file dengan 99 tambahan dan 5 penghapusan
  1. 5 0
      manager/forms.py
  2. 41 1
      manager/templates/locations.html
  3. 50 1
      manager/views.py
  4. 3 3
      review/review_utils.py

+ 5 - 0
manager/forms.py

@@ -0,0 +1,5 @@
+from django import forms
+
+
+class ImportStaffSpreadSheet(forms.Form):
+    staff_file = forms.FileField()

+ 41 - 1
manager/templates/locations.html

@@ -1,4 +1,6 @@
 {% load static %}
+{% load crispy_forms_tags %}
+
 <html lang="en">
   <head>
     <meta charset="utf-8">
@@ -45,6 +47,13 @@
           <a class="nav-link" href="{% url 'home-view' %}" style="color: white; font-size:20px">SignatureCare Review Portal</a>
         </li>
         </ul>
+
+        <ul class="navbar-nav px-3">
+          <li class="nav-item text-nowrap">
+              <button style="background: center; border-color: dimgrey; padding: 7px;" type="button" class="nav-link btn btn-primary" data-toggle="modal" data-target="#fileUploadModal">Import staff</button>
+          </li>
+        </ul>
+
         <ul class="navbar-nav px-3">
           <li class="nav-item text-nowrap">
             <a class="nav-link" href="{% url 'logout' %}">Sign out</a>
@@ -52,7 +61,13 @@
         </ul>
       </div>
     </nav>
-
+    {% if messages %}
+      {% for message in messages %}
+        <div class="alert alert-{{ message.tags }} container">
+          {{ message }}
+        </div>
+      {% endfor %}
+    {% endif %}
     <div class="container">
       <div class="row">
       {% for loc in all_locations %}
@@ -80,6 +95,31 @@
           </div>
         {% endfor %}
       </div>
+      <!-- Modal -->
+        <div class="modal fade" id="fileUploadModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
+          <div class="modal-dialog">
+            <div class="modal-content">
+              <div class="modal-header">
+                <h5 class="modal-title" id="exampleModalLabel">Import Excel File</h5>
+                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                  <span aria-hidden="true">&times;</span>
+                </button>
+              </div>
+              <div class="modal-body">
+              <form method="POST" enctype="multipart/form-data">
+                {% csrf_token %}
+                <fieldset class="form-group">
+                  {{ file_upload_form|crispy }}
+                </fieldset>
+                <div class="form-group" style="display: grid;">
+                  <button class="btn btn-outline-info" type="submit">Submit</button>
+                </div>
+              </form>
+              </div>
+            </div>
+          </div>
+        </div>
+
     </div>
     <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://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

+ 50 - 1
manager/views.py

@@ -1,3 +1,4 @@
+import openpyxl
 from gauth.models import Location, LocationManager
 from django.http import Http404
 from django.shortcuts import render, redirect
@@ -19,14 +20,62 @@ from user.utils import (
     get_facebook_report,
     date_str2datetime
 )
+from .forms import ImportStaffSpreadSheet
+
+
+LOC_SYN = {
+    'cypress-1960': 'cypress',
+    'collegestation': 'college station'
+}
 
 
 class LocationListView(View, PermissionRequiredMixin):
     permission_required = 'is_staff'
 
+    def post(self, request, *args, **kwargs):
+        form = ImportStaffSpreadSheet(request.POST, request.FILES)
+        if form.is_valid():
+            staff_file = form.cleaned_data.get('staff_file')
+            wb = openpyxl.load_workbook(staff_file)
+            worksheet = wb.worksheets[0]
+
+            rows = worksheet.values
+            col_names = next(rows)
+
+            try:
+                name_idx, loc_idx, dept_idx = col_names.index('Name'), col_names.index('Location'), col_names.index('Job Title')
+            except ValueError:
+                messages.warning(request, "Columns in missing. Check columns ['Name', 'Location', 'Job Title'] again.")
+                return redirect('location-list')
+
+            for row in rows:
+                try:
+                    name, loc, dept = row[name_idx], row[loc_idx], row[dept_idx]
+                except IndexError:
+                    pass
+                if loc.lower() in ['alllocations', '#n/a']:
+                    continue
+                if loc.lower() in LOC_SYN.keys():
+                    loc = LOC_SYN[loc.lower()]
+
+                location = Location.objects.filter(care_name__icontains=loc).first()
+                if location:
+                    Staff.objects.create(
+                        name=name,
+                        location=location,
+                        department=dept
+                    )
+            messages.success(request, 'Staff file upload successfully.')
+        return redirect('location-list')
+
     def get(self, request, *args, **kwargs):
         locations = Location.objects.all()
-        return render(request, 'locations.html', {'all_locations': locations})
+        form = ImportStaffSpreadSheet()
+        context = {
+            'all_locations': locations,
+            'file_upload_form': form
+        }
+        return render(request, 'locations.html', context=context)
 
 
 class LocationAnalytics(LoginRequiredMixin, View):

+ 3 - 3
review/review_utils.py

@@ -217,9 +217,9 @@ def get_bad_reviews(location_id, **kwargs):
     :return: QuerySet -> all low rating reviews in last * days/hours/minutes
 
     Example --------------
-    >>> get_bad_reviews(location_id='123456', days=5, hours=2, minute=1)
-    >>> get_bad_reviews(location_id='123456', days=5)
-    >>> get_bad_reviews(location_id='123456', hours=5)
+    => get_bad_reviews(location_id='123456', days=5, hours=2, minute=1)
+    => get_bad_reviews(location_id='123456', days=5)
+    => get_bad_reviews(location_id='123456', hours=5)
     '''
     now = timezone.now()
     date = now - timezone.timedelta(**kwargs)