utils.py 4.1 KB

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