views.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import re
  2. import json
  3. import requests as http_requests
  4. from django.conf import settings
  5. from django.utils import timezone
  6. from django.shortcuts import render
  7. from rest_framework.views import APIView
  8. from rest_framework.response import Response
  9. from django.views.generic import View
  10. from django.contrib.auth.mixins import LoginRequiredMixin
  11. from gauth.models import Location
  12. from name_extractor.models import Staff
  13. from facebook_app.analytics import get_facebook_weekly_summary
  14. from yelp.analytics import get_yelp_weekly_summary
  15. from .utils import get_list_of_reviews
  16. from .utils import (
  17. get_review_count_by_month,
  18. get_review_count_by_week,
  19. last_month_reviews,
  20. weekly_reviews_summary
  21. )
  22. NER_MODEL_URL = getattr(settings, 'NER_SERVER_URI')
  23. class ChartDataByMonth(APIView):
  24. def get(self, request, *args, **kwargs):
  25. location_id = request.GET['location_id']
  26. time_interval = request.GET['time_interval']
  27. if time_interval == 'month':
  28. res = get_review_count_by_month(location_id)
  29. else:
  30. res = get_review_count_by_week(location_id)
  31. return Response(res)
  32. class AnalyticsData(LoginRequiredMixin, View):
  33. def get(self, request, *args, **kwargs):
  34. locations = Location.objects.all()
  35. return render(request, 'charts.html', {'location_list': locations})
  36. def monthly_report(requests, location_id):
  37. last_month_data = last_month_reviews(location_id=location_id)
  38. staffs = Staff.objects.filter(location_id=location_id).\
  39. exclude(name_mentioned=0).order_by('-total_units')
  40. context = {
  41. 'this_month': last_month_data,
  42. 'staffs': staffs
  43. }
  44. return render(requests, 'last_month_report.html', context)
  45. def weekly_report(requests, location_id):
  46. location = Location.objects.get(pk=location_id)
  47. start_date = timezone.now()
  48. end_date = start_date - timezone.timedelta(days=7)
  49. g_reviews, g_ratings = weekly_reviews_summary(location_id=location_id)
  50. f_reviews, f_ratings = get_facebook_weekly_summary(location_id=location_id)
  51. y_reviews, y_rating = get_yelp_weekly_summary(location_id=location_id)
  52. all_revs = get_list_of_reviews(g_reviews, f_reviews, y_reviews)
  53. payload = {
  54. 'reviews': all_revs,
  55. }
  56. headers = {'content-type': 'application/json'}
  57. url = f'{NER_MODEL_URL}/displacy/google'
  58. response = http_requests.post(url, data=json.dumps(payload), headers=headers)
  59. google_pos_reviews = response.json()
  60. all_reviews = []
  61. for rev in google_pos_reviews:
  62. rev['comment'] = re.sub(
  63. r'>ORG</span>|>PERSON</span>|>GPE</span>|>PRODUCT</span>|>DATE</span>|>TIME</span>|>MONEY</span>',
  64. '></span>',
  65. rev['comment']
  66. )
  67. all_reviews.append(rev)
  68. context = {
  69. 'location': location,
  70. 'google_ratings': g_ratings,
  71. 'facebook_ratings': f_ratings,
  72. 'yelp_ratings': y_rating,
  73. 'google_pos_reviews': all_reviews,
  74. 'date': {
  75. 'start': start_date,
  76. 'end': end_date
  77. }
  78. }
  79. return render(requests, 'weekly_report.html', context)
  80. def weeklyreportlist(request):
  81. locations = Location.objects.all()
  82. return render(request, 'weekly_report_list.html', {'locs': locations})