analytics.py 2.1 KB

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