from django.conf import settings from django.db.models import Count from django.utils import timezone from .models import FacebookReview from gauth.models import Location from analytics.send_email import send_email def get_facebook_weekly_summary(location_id): date = timezone.now() - timezone.timedelta(days=7) reviews = FacebookReview.objects.filter(page__location_id=location_id, create_time__gte=date) rating = reviews.values('recommendation_type')\ .annotate(total=Count('recommendation_type'))\ .order_by('-recommendation_type') return reviews, rating def send_email_bad_reviews(): locations = Location.objects.all() for location in locations: to = settings.ADMIN_MAINTEINER_EMAILS # date = timezone.now() - timezone.timedelta(hours=12) date = timezone.now() - timezone.timedelta(days=20) reviews = FacebookReview.objects.filter( page__location_id=location.location_id, create_time__gte=date, recommendation_type=False ) total_reviews = reviews.count() if total_reviews > 0: # Add location maintainer email if it has an email to.append(location.recipient_email) if location.recipient_email else None subject = f"A negative Facebook review has been post in {location.care_name}." message = f'''

Hi there,

''' if total_reviews > 1: tt = f'{total_reviews} negative Facebook reviews have' else: tt = f'A negative Facebook review has ' message += tt + f'been posted in {location.care_name}.

' for r in reviews: link = f'

Link: https://facebook.com/{r.id}

' comment = f'

Comment: {r.review_text if r.review_text else "No comment"}

' message += comment + link message += '

Regards,

Byte Trek Ltd.

' send_email(to_list=to, subject=subject, message=message) if location.recipient_email in to: to.remove(location.recipient_email)