analytics.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.db.models import Count
  2. from django.utils import timezone
  3. from .models import FacebookReview
  4. from gauth.models import Location, LocationManager
  5. from analytics.background_job import send_email
  6. def get_facebook_weekly_summary(location_id):
  7. date = timezone.now() - timezone.timedelta(days=7)
  8. reviews = FacebookReview.objects.filter(page__location_id=location_id, create_time__gte=date)
  9. rating = reviews.values('recommendation_type')\
  10. .annotate(total=Count('recommendation_type'))\
  11. .order_by('-recommendation_type')
  12. return reviews, rating
  13. def send_email_bad_reviews():
  14. locations = Location.objects.all()
  15. for location in locations:
  16. date = timezone.now() - timezone.timedelta(hours=12)
  17. # date = timezone.now() - timezone.timedelta(days=20)
  18. reviews = FacebookReview.objects.filter(
  19. page__location_id=location.location_id,
  20. create_time__gte=date,
  21. recommendation_type=False
  22. )
  23. total_reviews = reviews.count()
  24. if total_reviews > 0:
  25. # Add location maintainer email if it has an email
  26. to = list(LocationManager.objects.filter(location_id=location.location_id).values_list('email', flat=True))
  27. subject = f"A negative Facebook review has been posted in {location.care_name}."
  28. message = '<p>Hi there, </p><p>'
  29. if total_reviews > 1:
  30. tt = f'{total_reviews} negative Facebook reviews have'
  31. else:
  32. tt = 'A negative Facebook review has '
  33. message += tt + f'been posted in {location.care_name}.</p>'
  34. for r in reviews:
  35. link = f'<p>Link: https://facebook.com/{r.id}</p>'
  36. comment = f'<p><b>Comment: </b>{r.review_text if r.review_text else "No comment"}</p>'
  37. message += comment + link
  38. message += '<p>Regards,</p><p>SignatureCare Review Team.</p>'
  39. send_email(subject=subject, message_body=message, to_list=to)