1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- from review.models import Review
- def get_review_count_by_month(location_id):
- sql = f'''
- SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
- FROM review_review
- WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time and location_id={location_id}
- GROUP BY MONTH(create_time)
- ORDER BY DATE(create_time)
- '''
- qs = Review.objects.raw(sql)
- label = [q.month for q in qs]
- total_review = [q.total_review for q in qs]
- star_ratings = []
- for i in range(1, 6):
- sql = f'''
- SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_ratings
- FROM review_review
- WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time
- and location_id={location_id} and star_rating={i}
- GROUP BY MONTH(create_time)
- ORDER BY DATE(create_time)
- '''
- qs = Review.objects.raw(sql)
- ratings = [0]*len(label)
- for q in qs:
- ratings[label.index(q.month)] = q.total_ratings
- star_ratings.append(ratings)
- response = {
- 'label': label,
- 'total_reviews': total_review,
- 'one_star': star_ratings[0],
- 'two_star': star_ratings[1],
- 'three_star': star_ratings[2],
- 'four_star': star_ratings[3],
- 'five_star': star_ratings[4]
- }
- return response
- def get_review_count_by_week(location_id):
- sql = f'''
- SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
- FROM review_review
- WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time and location_id={location_id}
- GROUP BY WEEK(create_time)
- ORDER BY DATE(create_time)
- '''
- qs = Review.objects.raw(sql)
- label = [q.week for q in qs]
- total_review = [q.total_review for q in qs]
- star_ratings = []
- for i in range(1, 6):
- sql = f'''
- SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_ratings
- FROM review_review
- WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time
- and location_id={location_id} and star_rating={i}
- GROUP BY WEEK(create_time)
- ORDER BY DATE(create_time)
- '''
- qs = Review.objects.raw(sql)
- ratings = [0]*len(label)
- for q in qs:
- ratings[label.index(q.week)] = q.total_ratings
- star_ratings.append(ratings)
- response = {
- 'label': label,
- 'total_reviews': total_review,
- 'one_star': star_ratings[0],
- 'two_star': star_ratings[1],
- 'three_star': star_ratings[2],
- 'four_star': star_ratings[3],
- 'five_star': star_ratings[4]
- }
- return response
|