|
@@ -1,3 +1,4 @@
|
|
|
|
+import openpyxl
|
|
from gauth.models import Location, LocationManager
|
|
from gauth.models import Location, LocationManager
|
|
from django.http import Http404
|
|
from django.http import Http404
|
|
from django.shortcuts import render, redirect
|
|
from django.shortcuts import render, redirect
|
|
@@ -19,14 +20,62 @@ from user.utils import (
|
|
get_facebook_report,
|
|
get_facebook_report,
|
|
date_str2datetime
|
|
date_str2datetime
|
|
)
|
|
)
|
|
|
|
+from .forms import ImportStaffSpreadSheet
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+LOC_SYN = {
|
|
|
|
+ 'cypress-1960': 'cypress',
|
|
|
|
+ 'collegestation': 'college station'
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
class LocationListView(View, PermissionRequiredMixin):
|
|
class LocationListView(View, PermissionRequiredMixin):
|
|
permission_required = 'is_staff'
|
|
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):
|
|
def get(self, request, *args, **kwargs):
|
|
locations = Location.objects.all()
|
|
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):
|
|
class LocationAnalytics(LoginRequiredMixin, View):
|