utils.py 4.1 KB

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