views.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import openpyxl
  2. from gauth.models import Location, LocationManager
  3. from django.http import Http404
  4. from django.shortcuts import render, redirect
  5. from django.views.generic import View
  6. from django.contrib import messages
  7. from user.forms import StaffRegistrationForm, StaffSheetDateForm
  8. from review.models import Review
  9. from facebook_app.models import FacebookReview
  10. from yelp.models import YelpReview
  11. from name_extractor.models import Staff
  12. from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
  13. from django.shortcuts import get_object_or_404
  14. from name_extractor.utils import extract_names_from_reviews, make_all_staffs_point_zero
  15. from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
  16. from user.utils import (
  17. get_google_review_report,
  18. get_facebook_report,
  19. date_str2datetime
  20. )
  21. from .forms import ImportStaffSpreadSheet
  22. LOC_SYN = {
  23. 'cypress-1960': 'cypress',
  24. 'collegestation': 'college station'
  25. }
  26. class LocationListView(View, PermissionRequiredMixin):
  27. permission_required = 'is_staff'
  28. def post(self, request, *args, **kwargs):
  29. form = ImportStaffSpreadSheet(request.POST, request.FILES)
  30. if form.is_valid():
  31. staff_file = form.cleaned_data.get('staff_file')
  32. wb = openpyxl.load_workbook(staff_file)
  33. worksheet = wb.worksheets[0]
  34. rows = worksheet.values
  35. col_names = next(rows)
  36. try:
  37. name_idx, loc_idx, dept_idx = col_names.index('Name'), col_names.index('Location'), col_names.index('Job Title')
  38. except ValueError:
  39. messages.warning(request, "Columns in missing. Check columns ['Name', 'Location', 'Job Title'] again.")
  40. return redirect('location-list')
  41. # Clear all previously registered staffs
  42. staffs = Staff.objects.all()
  43. staffs.delete()
  44. for row in rows:
  45. try:
  46. name, loc, dept = row[name_idx], row[loc_idx], row[dept_idx]
  47. except IndexError:
  48. pass
  49. if loc.lower() in ['alllocations', '#n/a']:
  50. continue
  51. if loc.lower() in LOC_SYN.keys():
  52. loc = LOC_SYN[loc.lower()]
  53. location = Location.objects.filter(care_name__icontains=loc).first()
  54. if location:
  55. Staff.objects.create(
  56. name=name,
  57. location=location,
  58. department=dept
  59. )
  60. messages.success(request, 'Staff file upload successfully.')
  61. return redirect('location-list')
  62. def get(self, request, *args, **kwargs):
  63. locations = Location.objects.all()
  64. form = ImportStaffSpreadSheet()
  65. context = {
  66. 'all_locations': locations,
  67. 'file_upload_form': form
  68. }
  69. return render(request, 'locations.html', context=context)
  70. class LocationAnalytics(LoginRequiredMixin, View):
  71. def get(self, request, location_id, *args, **kwargs):
  72. location = Location.objects.get(pk=location_id)
  73. google_report = get_google_review_report(location_id)
  74. facebook_report = get_facebook_report(location_id)
  75. context = {
  76. 'location': location,
  77. 'google_this_month': google_report.get('this_month'),
  78. 'google_last_month': google_report.get('last_month'),
  79. 'facebook_this_month': facebook_report.get('this_month'),
  80. 'facebook_last_month': facebook_report.get('last_month'),
  81. }
  82. return render(request, 'manager-dashboard.html', context=context)
  83. class ReviewListLocationWise(View):
  84. def get(self, request, platform, location_id, *args, **kwargs):
  85. location = Location.objects.get(pk=location_id)
  86. if platform == 'google':
  87. reviews = Review.objects.filter(location_id=location_id).order_by('-update_time')
  88. elif platform == 'facebook':
  89. reviews = FacebookReview.objects.filter(page__location_id=location_id).order_by('-create_time')
  90. elif platform == 'yelp':
  91. reviews = YelpReview.objects.filter(location__location_id=location_id).order_by('-date_posted')
  92. else:
  93. raise Http404()
  94. page = request.GET.get('page', 1)
  95. paginator = Paginator(reviews, 50)
  96. try:
  97. reviews = paginator.page(page)
  98. except PageNotAnInteger:
  99. reviews = paginator.page(1)
  100. except EmptyPage:
  101. reviews = paginator.page(paginator.num_pages)
  102. context = {'reviews': reviews, 'platform': platform, 'location': location}
  103. return render(request, 'review-list-man.html', context=context)
  104. class ReviewAnalyticsGraph(View):
  105. def get(self, request, location_id, *args, **kwargs):
  106. location = Location.objects.get(pk=location_id)
  107. return render(request, 'location-wise-reviews-man.html', context={'location': location})
  108. class StaffLeaderBoard(View):
  109. def get(self, request, location_id, *args, **kwargs):
  110. location = Location.objects.get(pk=location_id)
  111. staffs = Staff.objects.filter(location=location).order_by('-total_units')
  112. form = StaffRegistrationForm()
  113. date_form = StaffSheetDateForm()
  114. context = {
  115. 'location': location,
  116. 'staffs': staffs,
  117. 'date_form': date_form,
  118. 'form': form
  119. }
  120. return render(request, 'staff_list_man.html', context)
  121. def post(self, request, location_id, *args, **kwargs):
  122. form = StaffRegistrationForm(request.POST)
  123. if form.is_valid():
  124. name = form.cleaned_data.get('name')
  125. department = form.cleaned_data.get('department')
  126. nick_names = form.cleaned_data.get('nick_names')
  127. staff = Staff.objects.create(
  128. name=name,
  129. location_id=location_id,
  130. department=department,
  131. nick_names=nick_names
  132. )
  133. messages.success(request, f'A new staff {staff} has been created!')
  134. return redirect('staff-leaderboard-man', location_id=location_id)
  135. class SyncStaffLeaderBoard(View):
  136. def post(self, request, location_id, *args, **kwargs):
  137. start_date = date_str2datetime(request.POST.get('start_date'))
  138. end_date = date_str2datetime(request.POST.get('end_date'))
  139. extract_names_from_reviews(
  140. start_date=start_date,
  141. end_date=end_date,
  142. location_id=location_id
  143. )
  144. return redirect('staff-leaderboard-man', location_id=location_id)
  145. class StaffDelete(View):
  146. def get(self, request, staff_id, *args, **kwargs):
  147. staff = get_object_or_404(Staff, id=staff_id)
  148. staff.delete()
  149. return redirect('staff-leaderboard-man', location_id=staff.location_id)