views.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from django.shortcuts import render
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. from django.views.generic import View
  5. from django.contrib.auth.mixins import LoginRequiredMixin
  6. from gauth.models import Location
  7. from name_extractor.models import Staff
  8. from yelp.analytics import get_weekly_summary, get_staff_names
  9. from .utils import (
  10. get_review_count_by_month,
  11. get_review_count_by_week,
  12. last_month_reviews,
  13. weekly_reviews_summary
  14. )
  15. class ChartDataByMonth(APIView):
  16. def get(self, request, *args, **kwargs):
  17. location_id = request.GET['location_id']
  18. time_interval = request.GET['time_interval']
  19. if time_interval == 'month':
  20. res = get_review_count_by_month(location_id)
  21. else:
  22. res = get_review_count_by_week(location_id)
  23. return Response(res)
  24. class AnalyticsData(LoginRequiredMixin, View):
  25. def get(self, request, *args, **kwargs):
  26. locations = Location.objects.all()
  27. return render(request, 'charts.html', {'location_list': locations})
  28. def monthly_report(requests, location_id):
  29. last_month_data = last_month_reviews(location_id=location_id)
  30. staffs = Staff.objects.filter(location_id=location_id).\
  31. exclude(name_mentioned=0).order_by('-total_units')
  32. context = {
  33. 'this_month': last_month_data,
  34. 'staffs': staffs
  35. }
  36. return render(requests, 'last_month_report.html', context)
  37. def weekly_report(requests, location_id):
  38. g_reviews, g_ratings, g_staff_names = weekly_reviews_summary(location_id=location_id)
  39. y_reviews, y_ratings, y_staff_names = get_weekly_summary(location_id=location_id)
  40. g_bad_reviews = g_reviews.filter(star_rating__lte=2)
  41. y_bad_reviews = y_reviews.filter(rating__lte=2)
  42. context = {
  43. 'google_ratings': g_ratings,
  44. 'google_staffs': g_staff_names,
  45. 'google_bad_reviews': g_bad_reviews,
  46. 'yelp_ratings': y_ratings,
  47. 'yelp_staffs': y_staff_names,
  48. 'yelp_bad_reviews': y_bad_reviews
  49. }
  50. return render(requests, 'weekly_report.html', context)