views.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. f_reviews, f_ratings = get_facebook_weekly_summary(location_id=location_id)
  41. g_bad_reviews = g_reviews.filter(star_rating__lte=2)
  42. f_bad_reviews = f_reviews.filter(recommendation_type=False)
  43. context = {
  44. 'google_ratings': g_ratings,
  45. 'google_bad_reviews': g_bad_reviews,
  46. 'facebook_ratings': f_ratings,
  47. 'facebook_bad_reviews': f_bad_reviews
  48. }
  49. return render(requests, 'weekly_report.html', context)