123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import re
- import json
- import requests as http_requests
- from django.conf import settings
- from django.utils import timezone
- from django.shortcuts import render
- from rest_framework.views import APIView
- from rest_framework.response import Response
- from django.views.generic import View
- from django.contrib.auth.mixins import LoginRequiredMixin
- from gauth.models import Location
- from name_extractor.models import Staff
- from facebook_app.analytics import get_facebook_weekly_summary
- from yelp.analytics import get_yelp_weekly_summary
- from .utils import get_list_of_reviews
- from .utils import (
- get_review_count_by_month,
- get_review_count_by_week,
- last_month_reviews,
- weekly_reviews_summary
- )
- NER_MODEL_URL = getattr(settings, 'NER_SERVER_URI')
- class ChartDataByMonth(APIView):
- def get(self, request, *args, **kwargs):
- location_id = request.GET['location_id']
- time_interval = request.GET['time_interval']
- if time_interval == 'month':
- res = get_review_count_by_month(location_id)
- else:
- res = get_review_count_by_week(location_id)
- return Response(res)
- class AnalyticsData(LoginRequiredMixin, View):
- def get(self, request, *args, **kwargs):
- locations = Location.objects.all()
- return render(request, 'charts.html', {'location_list': locations})
- def monthly_report(requests, location_id):
- last_month_data = last_month_reviews(location_id=location_id)
- staffs = Staff.objects.filter(location_id=location_id).\
- exclude(name_mentioned=0).order_by('-total_units')
- context = {
- 'this_month': last_month_data,
- 'staffs': staffs
- }
- return render(requests, 'last_month_report.html', context)
- def weekly_report(requests, location_id):
- location = Location.objects.get(pk=location_id)
- start_date = timezone.now()
- end_date = start_date - timezone.timedelta(days=7)
- g_reviews, g_ratings = weekly_reviews_summary(location_id=location_id)
- f_reviews, f_ratings = get_facebook_weekly_summary(location_id=location_id)
- y_reviews, y_rating = get_yelp_weekly_summary(location_id=location_id)
- all_revs = get_list_of_reviews(g_reviews, f_reviews, y_reviews)
- payload = {
- 'reviews': all_revs,
- }
- headers = {'content-type': 'application/json'}
- url = f'{NER_MODEL_URL}/displacy/google'
- response = http_requests.post(url, data=json.dumps(payload), headers=headers)
- google_pos_reviews = response.json()
- all_reviews = []
- for rev in google_pos_reviews:
- rev['comment'] = re.sub(
- r'>ORG</span>|>PERSON</span>|>GPE</span>|>PRODUCT</span>|>DATE</span>|>TIME</span>|>MONEY</span>',
- '></span>',
- rev['comment']
- )
- all_reviews.append(rev)
- context = {
- 'location': location,
- 'google_ratings': g_ratings,
- 'facebook_ratings': f_ratings,
- 'yelp_ratings': y_rating,
- 'google_pos_reviews': all_reviews,
- 'date': {
- 'start': start_date,
- 'end': end_date
- }
- }
- return render(requests, 'weekly_report.html', context)
- def weeklyreportlist(request):
- locations = Location.objects.all()
- return render(request, 'weekly_report_list.html', {'locs': locations})
|