def clear_user_rating(request, ctype_id, object_id, decoration=1): """ Clear previous rating by the user and render get-rating page for new rating """ # Update rating by the user for object_id to zero. user = User.objects.all()[0] stars = Stars.objects.get(user=user, content_type_id=ctype_id, object_id=object_id) previous_rating = stars.rating stars.rating = 0.0 stars.save() print "+=" * 80, previous_rating # If user didn't clere his rating if previous_rating != 0.0: # Update AggragateStars model update_aggregate_stars(ctype_id, object_id, -1 * previous_rating) template = 'stars/get_rating.html' ctx = { 'ctype_id': ctype_id, 'object_id': object_id, 'decoration': int(decoration), } ctx = RequestContext(request, ctx) html = render_to_string(template, ctx) response = {'status': AJAX_OK, 'data': html} return response
def clear_user_rating(request, ctype_id, object_id, decoration=1): """ Clear previous rating by the user and render get-rating page for new rating """ # Update rating by the user for object_id to zero. user = User.objects.all()[0] stars = Stars.objects.get(user=user, content_type_id=ctype_id, object_id=object_id) previous_rating = stars.rating stars.rating = 0.0 stars.save() print "+="*80, previous_rating # If user didn't clere his rating if previous_rating != 0.0: # Update AggragateStars model update_aggregate_stars(ctype_id, object_id, -1*previous_rating) template = 'stars/get_rating.html' ctx = { 'ctype_id': ctype_id, 'object_id': object_id, 'decoration': int(decoration), } ctx = RequestContext(request, ctx) html = render_to_string(template, ctx) response = { 'status': AJAX_OK, 'data' : html } return response
def submit_user_rating(request, ctype_id, object_id, decoration=1): """ Save the rating submitted by user """ template = 'stars/show_user_rating.html' rating_id = 'rating-%s-%s' % (ctype_id, object_id) rating = request.POST.get(rating_id, 1.0) rating = float(rating) # Check if user has already submitted rating user = User.objects.all()[0] stars = Stars.objects.filter(user=user, content_type_id=ctype_id, object_id=object_id) # If not, create new object Else update existing rating if not stars: star = Stars(user=user, content_type_id=ctype_id, object_id=object_id, rating=rating) star.save() else: star = stars[0] star.rating = rating star.save() # Update AggragateStars model update_aggregate_stars(ctype_id, object_id, rating) stars_count = calculate_stars(rating) ctx = { 'ctype_id': ctype_id, 'object_id': object_id, 'full_stars': range(stars_count[0]), 'half_stars': range(stars_count[1]), 'empty_stars': range(stars_count[2]), 'user_rating': rating, 'show_check': 1, 'decoration': int(decoration), } ctx = RequestContext(request, ctx) html = render_to_string(template, ctx) response = {'status': AJAX_OK, 'data': html} return response
def submit_user_rating(request, ctype_id, object_id, decoration=1): """ Save the rating submitted by user """ template = 'stars/show_user_rating.html' rating_id = 'rating-%s-%s' % (ctype_id, object_id) rating = request.POST.get(rating_id, 1.0) rating = float(rating) # Check if user has already submitted rating user = User.objects.all()[0] stars = Stars.objects.filter(user=user, content_type_id=ctype_id, object_id=object_id) # If not, create new object Else update existing rating if not stars: star = Stars(user=user, content_type_id=ctype_id, object_id=object_id, rating=rating) star.save() else: star = stars[0] star.rating = rating star.save() # Update AggragateStars model update_aggregate_stars(ctype_id, object_id, rating) stars_count = calculate_stars(rating) ctx = { 'ctype_id': ctype_id, 'object_id': object_id, 'full_stars': range(stars_count[0]), 'half_stars': range(stars_count[1]), 'empty_stars': range(stars_count[2]), 'user_rating': rating, 'show_check': 1, 'decoration': int(decoration), } ctx = RequestContext(request, ctx) html = render_to_string(template, ctx) response = { 'status': AJAX_OK, 'data' : html } return response