views.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_yelp_weekly_summary
  9. from facebook_app.analytics import get_facebook_weekly_summary
  10. from .utils import (
  11. get_review_count_by_month,
  12. get_review_count_by_week,
  13. last_month_reviews,
  14. weekly_reviews_summary
  15. )
  16. class ChartDataByMonth(APIView):
  17. def get(self, request, *args, **kwargs):
  18. location_id = request.GET['location_id']
  19. time_interval = request.GET['time_interval']
  20. if time_interval == 'month':
  21. res = get_review_count_by_month(location_id)
  22. else:
  23. res = get_review_count_by_week(location_id)
  24. return Response(res)
  25. class AnalyticsData(LoginRequiredMixin, View):
  26. def get(self, request, *args, **kwargs):
  27. locations = Location.objects.all()
  28. return render(request, 'charts.html', {'location_list': locations})
  29. def monthly_report(requests, location_id):
  30. last_month_data = last_month_reviews(location_id=location_id)
  31. staffs = Staff.objects.filter(location_id=location_id).\
  32. exclude(name_mentioned=0).order_by('-total_units')
  33. context = {
  34. 'this_month': last_month_data,
  35. 'staffs': staffs
  36. }
  37. return render(requests, 'last_month_report.html', context)
  38. def weekly_report(requests, location_id):
  39. g_reviews, g_ratings = weekly_reviews_summary(location_id=location_id)
  40. y_reviews, y_ratings = get_yelp_weekly_summary(location_id=location_id)
  41. f_reviews, f_ratings = get_facebook_weekly_summary(location_id=location_id)
  42. g_bad_reviews = g_reviews.filter(star_rating__lte=2)
  43. y_bad_reviews = y_reviews.filter(rating__lte=2)
  44. f_bad_reviews = f_reviews.filter(recommendation_type=False)
  45. context = {
  46. 'google_ratings': g_ratings,
  47. 'google_bad_reviews': g_bad_reviews,
  48. 'yelp_ratings': y_ratings,
  49. 'yelp_bad_reviews': y_bad_reviews,
  50. 'facebook_ratings': f_ratings,
  51. 'facebook_bad_reviews': f_bad_reviews
  52. }
  53. return render(requests, 'weekly_report.html', context)