background_job.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.utils import timezone
  2. from gauth.models import Location
  3. from review.models import Review
  4. from .send_email import send_email
  5. from django.db.models import Count
  6. from review.review_utils import get_bad_reviews
  7. def send_email_bad_reviews():
  8. locations = Location.objects.all()
  9. for location in locations:
  10. reviews = get_bad_reviews(location.location_id, hours=6)
  11. total_reviews = reviews.count()
  12. if total_reviews > 0:
  13. to = [location.recipient_email if location.recipient_email else 'bt@bytetrek.com.bd']
  14. subject = f"A bad review has been post in {location.care_name}."
  15. message =f'''
  16. <img src="https://ercare24.com/wp-content/uploads/2016/07/signature-care-resized-e1462918690585.png" alt="Avatar" class="image" width=20% >
  17. <h1>Bad Review has been posted.</h1>
  18. <p>Hi there, </p>
  19. <p>There is {total_reviews} bad reviews has been posted in {location.care_name}.
  20. Please, response them ASAP.</p>
  21. <h4>Links: </h4>
  22. '''
  23. for r in reviews:
  24. link = r.location.review_site_url + '?reviewId='+r.review_id
  25. text = str(r.star_rating) + ' star review'
  26. message += f'<a href="{link}">{text}</a><br>'
  27. message += '<p>Regards,</p><p>Byte Trek Limited</p>'
  28. send_email(to_list=to, subject=subject, message=message)
  29. def weekly_report(location_id):
  30. date = timezone.now() - timezone.timedelta(days=7)
  31. reviews = Review.objects.filter(location_id=location_id, create_time__gte=date)
  32. res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating')\
  33. .annotate(total=Count('star_rating')).order_by('star_rating')
  34. # TODO:
  35. # make a table with reviews count and bad reviews and send mail to a particular email add.