from gauth.models import Location, LocationManager from django.http import Http404 from django.shortcuts import render, redirect from django.views.generic import View from django.contrib import messages from user.forms import StaffRegistrationForm, StaffSheetDateForm from review.models import Review from facebook_app.models import FacebookReview from yelp.models import YelpReview from name_extractor.models import Staff from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.shortcuts import get_object_or_404 from name_extractor.utils import extract_names_from_reviews, make_all_staffs_point_zero from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from user.utils import ( get_google_review_report, get_facebook_report, date_str2datetime ) class LocationListView(View, PermissionRequiredMixin): permission_required = 'is_staff' def get(self, request, *args, **kwargs): locations = Location.objects.all() return render(request, 'locations.html', {'all_locations': locations}) class LocationAnalytics(LoginRequiredMixin, View): def get(self, request, location_id, *args, **kwargs): location = Location.objects.get(pk=location_id) google_report = get_google_review_report(location_id) facebook_report = get_facebook_report(location_id) context = { 'location': location, 'google_this_month': google_report.get('this_month'), 'google_last_month': google_report.get('last_month'), 'facebook_this_month': facebook_report.get('this_month'), 'facebook_last_month': facebook_report.get('last_month'), } return render(request, 'manager-dashboard.html', context=context) class ReviewListLocationWise(View): def get(self, request, platform, location_id, *args, **kwargs): location = Location.objects.get(pk=location_id) if platform == 'google': reviews = Review.objects.filter(location_id=location_id).order_by('-update_time') elif platform == 'facebook': reviews = FacebookReview.objects.filter(page__location_id=location_id).order_by('-create_time') elif platform == 'yelp': reviews = YelpReview.objects.filter(location__location_id=location_id).order_by('-date_posted') else: raise Http404() page = request.GET.get('page', 1) paginator = Paginator(reviews, 50) try: reviews = paginator.page(page) except PageNotAnInteger: reviews = paginator.page(1) except EmptyPage: reviews = paginator.page(paginator.num_pages) context = {'reviews': reviews, 'platform': platform, 'location': location} return render(request, 'review-list-man.html', context=context) class ReviewAnalyticsGraph(View): def get(self, request, location_id, *args, **kwargs): location = Location.objects.get(pk=location_id) return render(request, 'location-wise-reviews-man.html', context={'location': location}) class StaffLeaderBoard(View): def get(self, request, location_id, *args, **kwargs): location = Location.objects.get(pk=location_id) staffs = Staff.objects.filter(location=location).order_by('-total_units') form = StaffRegistrationForm() date_form = StaffSheetDateForm() context = { 'location': location, 'staffs': staffs, 'date_form': date_form, 'form': form } return render(request, 'staff_list_man.html', context) def post(self, request, location_id, *args, **kwargs): form = StaffRegistrationForm(request.POST) if form.is_valid(): name = form.cleaned_data.get('name') department = form.cleaned_data.get('department') nick_names = form.cleaned_data.get('nick_names') staff = Staff.objects.create( name=name, location_id=location_id, department=department, nick_names=nick_names ) messages.success(request, f'A new staff {staff} has been created!') return redirect('staff-leaderboard-man', location_id=location_id) class SyncStaffLeaderBoard(View): def post(self, request, location_id, *args, **kwargs): start_date = date_str2datetime(request.POST.get('start_date')) end_date = date_str2datetime(request.POST.get('end_date')) extract_names_from_reviews( start_date=start_date, end_date=end_date, location_id=location_id ) return redirect('staff-leaderboard-man', location_id=location_id) class StaffDelete(View): def get(self, request, staff_id, *args, **kwargs): staff = get_object_or_404(Staff, id=staff_id) staff.delete() return redirect('staff-leaderboard-man', location_id=staff.location_id)