views.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. for row in rows:
  42. try:
  43. name, loc, dept = row[name_idx], row[loc_idx], row[dept_idx]
  44. except IndexError:
  45. pass
  46. if loc.lower() in ['alllocations', '#n/a']:
  47. continue
  48. if loc.lower() in LOC_SYN.keys():
  49. loc = LOC_SYN[loc.lower()]
  50. location = Location.objects.filter(care_name__icontains=loc).first()
  51. if location:
  52. Staff.objects.create(
  53. name=name,
  54. location=location,
  55. department=dept
  56. )
  57. messages.success(request, 'Staff file upload successfully.')
  58. return redirect('location-list')
  59. def get(self, request, *args, **kwargs):
  60. locations = Location.objects.all()
  61. form = ImportStaffSpreadSheet()
  62. context = {
  63. 'all_locations': locations,
  64. 'file_upload_form': form
  65. }
  66. return render(request, 'locations.html', context=context)
  67. class LocationAnalytics(LoginRequiredMixin, View):
  68. def get(self, request, location_id, *args, **kwargs):
  69. location = Location.objects.get(pk=location_id)
  70. google_report = get_google_review_report(location_id)
  71. facebook_report = get_facebook_report(location_id)
  72. context = {
  73. 'location': location,
  74. 'google_this_month': google_report.get('this_month'),
  75. 'google_last_month': google_report.get('last_month'),
  76. 'facebook_this_month': facebook_report.get('this_month'),
  77. 'facebook_last_month': facebook_report.get('last_month'),
  78. }
  79. return render(request, 'manager-dashboard.html', context=context)
  80. class ReviewListLocationWise(View):
  81. def get(self, request, platform, location_id, *args, **kwargs):
  82. location = Location.objects.get(pk=location_id)
  83. if platform == 'google':
  84. reviews = Review.objects.filter(location_id=location_id).order_by('-update_time')
  85. elif platform == 'facebook':
  86. reviews = FacebookReview.objects.filter(page__location_id=location_id).order_by('-create_time')
  87. elif platform == 'yelp':
  88. reviews = YelpReview.objects.filter(location__location_id=location_id).order_by('-date_posted')
  89. else:
  90. raise Http404()
  91. page = request.GET.get('page', 1)
  92. paginator = Paginator(reviews, 50)
  93. try:
  94. reviews = paginator.page(page)
  95. except PageNotAnInteger:
  96. reviews = paginator.page(1)
  97. except EmptyPage:
  98. reviews = paginator.page(paginator.num_pages)
  99. context = {'reviews': reviews, 'platform': platform, 'location': location}
  100. return render(request, 'review-list-man.html', context=context)
  101. class ReviewAnalyticsGraph(View):
  102. def get(self, request, location_id, *args, **kwargs):
  103. location = Location.objects.get(pk=location_id)
  104. return render(request, 'location-wise-reviews-man.html', context={'location': location})
  105. class StaffLeaderBoard(View):
  106. def get(self, request, location_id, *args, **kwargs):
  107. location = Location.objects.get(pk=location_id)
  108. staffs = Staff.objects.filter(location=location).order_by('-total_units')
  109. form = StaffRegistrationForm()
  110. date_form = StaffSheetDateForm()
  111. context = {
  112. 'location': location,
  113. 'staffs': staffs,
  114. 'date_form': date_form,
  115. 'form': form
  116. }
  117. return render(request, 'staff_list_man.html', context)
  118. def post(self, request, location_id, *args, **kwargs):
  119. form = StaffRegistrationForm(request.POST)
  120. if form.is_valid():
  121. name = form.cleaned_data.get('name')
  122. department = form.cleaned_data.get('department')
  123. nick_names = form.cleaned_data.get('nick_names')
  124. staff = Staff.objects.create(
  125. name=name,
  126. location_id=location_id,
  127. department=department,
  128. nick_names=nick_names
  129. )
  130. messages.success(request, f'A new staff {staff} has been created!')
  131. return redirect('staff-leaderboard-man', location_id=location_id)
  132. class SyncStaffLeaderBoard(View):
  133. def post(self, request, location_id, *args, **kwargs):
  134. start_date = date_str2datetime(request.POST.get('start_date'))
  135. end_date = date_str2datetime(request.POST.get('end_date'))
  136. extract_names_from_reviews(
  137. start_date=start_date,
  138. end_date=end_date,
  139. location_id=location_id
  140. )
  141. return redirect('staff-leaderboard-man', location_id=location_id)
  142. class StaffDelete(View):
  143. def get(self, request, staff_id, *args, **kwargs):
  144. staff = get_object_or_404(Staff, id=staff_id)
  145. staff.delete()
  146. return redirect('staff-leaderboard-man', location_id=staff.location_id)