views.py 2.9 KB

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