utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from django.utils import timezone
  2. from review.models import Review
  3. from django.db.models import Count
  4. def last_month_reviews(location_id):
  5. now = timezone.now()
  6. date = now.replace(day=1)
  7. day = (date - timezone.timedelta(days=1)).day + 1
  8. prev_date = date - timezone.timedelta(days=day)
  9. res = Review.objects.filter(create_time__range=(prev_date, date), location_id=location_id).\
  10. values('star_rating').annotate(total=Count('star_rating')).order_by('star_rating')
  11. ratings = [0] * 5
  12. for r in res:
  13. ratings[r.get('star_rating') - 1] = r.get('total')
  14. return ratings
  15. def weekly_reviews_summary(location_id):
  16. now = timezone.now()
  17. date = now - timezone.timedelta(days=7)
  18. reviews = Review.objects.filter(
  19. create_time__gte=date,
  20. location_id=location_id
  21. )
  22. ratings = reviews.values('star_rating')\
  23. .annotate(total=Count('star_rating'))\
  24. .order_by('-star_rating')
  25. return reviews, ratings
  26. def last_data(date, location_id):
  27. res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating')\
  28. .annotate(total=Count('star_rating')).order_by('star_rating')
  29. ratings = [0] * 5
  30. for r in res:
  31. ratings[r.get('star_rating')-1] = r.get('total')
  32. return ratings
  33. def get_review_count_by_month(location_id):
  34. now = timezone.now()
  35. date = now.replace(day=1) - timezone.timedelta(days=1)
  36. day = date.day - now.day
  37. first_day_month = now - timezone.timedelta(days=now.day)
  38. this_month = last_data(first_day_month, location_id)
  39. sql = f'''
  40. SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
  41. FROM review_review
  42. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
  43. GROUP BY MONTH(create_time)
  44. ORDER BY DATE(create_time)
  45. '''
  46. qs = Review.objects.raw(sql)
  47. label = [q.month for q in qs]
  48. total_review = [q.total_review for q in qs]
  49. star_ratings = []
  50. for i in range(1, 6):
  51. sql = f'''
  52. SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_ratings
  53. FROM review_review
  54. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
  55. and location_id={location_id} and star_rating={i}
  56. GROUP BY MONTH(create_time)
  57. ORDER BY DATE(create_time)
  58. '''
  59. qs = Review.objects.raw(sql)
  60. ratings = [0]*len(label)
  61. for q in qs:
  62. ratings[label.index(q.month)] = q.total_ratings
  63. star_ratings.append(ratings)
  64. response = {
  65. 'label': label,
  66. 'total_reviews': total_review,
  67. 'one_star': star_ratings[0],
  68. 'two_star': star_ratings[1],
  69. 'three_star': star_ratings[2],
  70. 'four_star': star_ratings[3],
  71. 'five_star': star_ratings[4],
  72. 'this': this_month
  73. }
  74. return response
  75. def get_review_count_by_week(location_id):
  76. now = timezone.now()
  77. day = 6 - now.weekday()
  78. first_day_week = now - timezone.timedelta(days=(now.weekday()+1))
  79. this_week = last_data(first_day_week, location_id)
  80. sql = f'''
  81. SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
  82. FROM review_review
  83. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
  84. GROUP BY WEEK(create_time)
  85. ORDER BY DATE(create_time)
  86. '''
  87. qs = Review.objects.raw(sql)
  88. label = [q.week for q in qs]
  89. total_review = [q.total_review for q in qs]
  90. star_ratings = []
  91. for i in range(1, 6):
  92. sql = f'''
  93. SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_ratings
  94. FROM review_review
  95. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
  96. and location_id={location_id} and star_rating={i}
  97. GROUP BY WEEK(create_time)
  98. ORDER BY DATE(create_time)
  99. '''
  100. qs = Review.objects.raw(sql)
  101. ratings = [0]*len(label)
  102. for q in qs:
  103. ratings[label.index(q.week)] = q.total_ratings
  104. star_ratings.append(ratings)
  105. response = {
  106. 'label': label,
  107. 'total_reviews': total_review,
  108. 'one_star': star_ratings[0],
  109. 'two_star': star_ratings[1],
  110. 'three_star': star_ratings[2],
  111. 'four_star': star_ratings[3],
  112. 'five_star': star_ratings[4],
  113. 'this': this_week
  114. }
  115. return response