views.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from gauth.models import Location, LocationManager
  2. from django.http import Http404
  3. from django.shortcuts import render, redirect
  4. from django.views.generic import View
  5. from django.contrib import messages
  6. from user.forms import StaffRegistrationForm, StaffSheetDateForm
  7. from review.models import Review
  8. from facebook_app.models import FacebookReview
  9. from yelp.models import YelpReview
  10. from name_extractor.models import Staff
  11. from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
  12. from django.shortcuts import get_object_or_404
  13. from name_extractor.utils import extract_names_from_reviews, make_all_staffs_point_zero
  14. from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
  15. from user.utils import (
  16. get_google_review_report,
  17. get_facebook_report,
  18. date_str2datetime
  19. )
  20. class LocationListView(View, PermissionRequiredMixin):
  21. permission_required = 'is_staff'
  22. def get(self, request, *args, **kwargs):
  23. locations = Location.objects.all()
  24. return render(request, 'locations.html', {'all_locations': locations})
  25. class LocationAnalytics(LoginRequiredMixin, View):
  26. def get(self, request, location_id, *args, **kwargs):
  27. location = Location.objects.get(pk=location_id)
  28. google_report = get_google_review_report(location_id)
  29. facebook_report = get_facebook_report(location_id)
  30. context = {
  31. 'location': location,
  32. 'google_this_month': google_report.get('this_month'),
  33. 'google_last_month': google_report.get('last_month'),
  34. 'facebook_this_month': facebook_report.get('this_month'),
  35. 'facebook_last_month': facebook_report.get('last_month'),
  36. }
  37. return render(request, 'manager-dashboard.html', context=context)
  38. class ReviewListLocationWise(View):
  39. def get(self, request, platform, location_id, *args, **kwargs):
  40. location = Location.objects.get(pk=location_id)
  41. if platform == 'google':
  42. reviews = Review.objects.filter(location_id=location_id).order_by('-update_time')
  43. elif platform == 'facebook':
  44. reviews = FacebookReview.objects.filter(page__location_id=location_id).order_by('-create_time')
  45. elif platform == 'yelp':
  46. reviews = YelpReview.objects.filter(location__location_id=location_id).order_by('-date_posted')
  47. else:
  48. raise Http404()
  49. page = request.GET.get('page', 1)
  50. paginator = Paginator(reviews, 50)
  51. try:
  52. reviews = paginator.page(page)
  53. except PageNotAnInteger:
  54. reviews = paginator.page(1)
  55. except EmptyPage:
  56. reviews = paginator.page(paginator.num_pages)
  57. context = {'reviews': reviews, 'platform': platform, 'location': location}
  58. return render(request, 'review-list-man.html', context=context)
  59. class ReviewAnalyticsGraph(View):
  60. def get(self, request, location_id, *args, **kwargs):
  61. location = Location.objects.get(pk=location_id)
  62. return render(request, 'location-wise-reviews-man.html', context={'location': location})
  63. class StaffLeaderBoard(View):
  64. def get(self, request, location_id, *args, **kwargs):
  65. location = Location.objects.get(pk=location_id)
  66. staffs = Staff.objects.filter(location=location).order_by('-total_units')
  67. form = StaffRegistrationForm()
  68. date_form = StaffSheetDateForm()
  69. context = {
  70. 'location': location,
  71. 'staffs': staffs,
  72. 'date_form': date_form,
  73. 'form': form
  74. }
  75. return render(request, 'staff_list_man.html', context)
  76. def post(self, request, location_id, *args, **kwargs):
  77. form = StaffRegistrationForm(request.POST)
  78. if form.is_valid():
  79. name = form.cleaned_data.get('name')
  80. department = form.cleaned_data.get('department')
  81. nick_names = form.cleaned_data.get('nick_names')
  82. staff = Staff.objects.create(
  83. name=name,
  84. location_id=location_id,
  85. department=department,
  86. nick_names=nick_names
  87. )
  88. messages.success(request, f'A new staff {staff} has been created!')
  89. return redirect('staff-leaderboard-man', location_id=location_id)
  90. class SyncStaffLeaderBoard(View):
  91. def post(self, request, location_id, *args, **kwargs):
  92. start_date = date_str2datetime(request.POST.get('start_date'))
  93. end_date = date_str2datetime(request.POST.get('end_date'))
  94. extract_names_from_reviews(
  95. start_date=start_date,
  96. end_date=end_date,
  97. location_id=location_id
  98. )
  99. return redirect('staff-leaderboard-man', location_id=location_id)
  100. class StaffDelete(View):
  101. def get(self, request, staff_id, *args, **kwargs):
  102. staff = get_object_or_404(Staff, id=staff_id)
  103. staff.delete()
  104. return redirect('staff-leaderboard-man', location_id=staff.location_id)