def rating(request): """ Handle a ``RatingForm`` submission and redirect back to its related object. """ response = initial_validation(request, "rating") if isinstance(response, HttpResponse): return response obj, post_data = response url = add_cache_bypass(obj.get_absolute_url().split("#")[0]) response = redirect(url + "#rating-%s" % obj.id) rating_form = RatingForm(request, obj, post_data) if rating_form.is_valid(): rating_form.save() if request.is_ajax(): # Reload the object and return the rating fields as json. obj = obj.__class__.objects.get(id=obj.id) rating_name = obj.get_ratingfield_name() json = {} for f in ("average", "count", "sum"): json["rating_" + f] = getattr(obj, "%s_%s" % (rating_name, f)) response = HttpResponse(dumps(json)) if rating_form.undoing: ratings = set(rating_form.previous) ^ set([rating_form.current]) else: ratings = rating_form.previous + [rating_form.current] set_cookie(response, "mezzanine-rating", ",".join(ratings)) return response
def rating(request): """ Handle a ``RatingForm`` submission and redirect back to its related object. """ response = initial_validation(request, "rating") if isinstance(response, HttpResponse): return response obj, post_data = response url = add_cache_bypass(obj.get_absolute_url().split("#")[0]) response = redirect(url + "#rating-%s" % obj.id) rating_form = RatingForm(request, obj, post_data) if rating_form.is_valid(): rating_form.save() if request.is_ajax(): # Reload the object and return the rating fields as json. obj = obj.__class__.objects.get(id=obj.id) rating_name = obj.get_ratingfield_name() json = {} for f in ("average", "count", "sum"): json["rating_" + f] = getattr(obj, "%s_%s" % (rating_name, f)) response = HttpResponse(dumps(json)) ratings = ",".join(rating_form.previous + [rating_form.current]) set_cookie(response, "mezzanine-rating", ratings) return response
def test_rating(self): """ Test that ratings can be posted and avarage/count are calculated. """ blog_post = BlogPost.objects.create(title="Ratings", user=self._user, status=CONTENT_STATUS_PUBLISHED) if settings.RATINGS_ACCOUNT_REQUIRED: self.client.login(username=self._username, password=self._password) data = RatingForm(None, blog_post).initial for value in settings.RATINGS_RANGE: data["value"] = value response = self.client.post(reverse("rating"), data=data) # Django doesn't seem to support unicode cookie keys correctly on # Python 2. See https://code.djangoproject.com/ticket/19802 response.delete_cookie(native_str("mezzanine-rating")) blog_post = BlogPost.objects.get(id=blog_post.id) count = len(settings.RATINGS_RANGE) _sum = sum(settings.RATINGS_RANGE) average = _sum / count if settings.RATINGS_ACCOUNT_REQUIRED: self.assertEqual(blog_post.rating_count, 1) self.assertEqual(blog_post.rating_sum, settings.RATINGS_RANGE[-1]) self.assertEqual(blog_post.rating_average, settings.RATINGS_RANGE[-1] / 1) else: self.assertEqual(blog_post.rating_count, count) self.assertEqual(blog_post.rating_sum, _sum) self.assertEqual(blog_post.rating_average, average)
def test_rating(self): """ Test that ratings can be posted and avarage/count are calculated. """ blog_post = BlogPost.objects.create(title="Ratings", user=self._user, status=CONTENT_STATUS_PUBLISHED) if settings.RATINGS_ACCOUNT_REQUIRED: self.client.login(username=self._username, password=self._password) data = RatingForm(None, blog_post).initial for value in settings.RATINGS_RANGE: data["value"] = value response = self.client.post(reverse("rating"), data=data) response.delete_cookie("mezzanine-rating") blog_post = BlogPost.objects.get(id=blog_post.id) count = len(settings.RATINGS_RANGE) _sum = sum(settings.RATINGS_RANGE) average = _sum / count if settings.RATINGS_ACCOUNT_REQUIRED: self.assertEqual(blog_post.rating_count, 1) self.assertEqual(blog_post.rating_sum, settings.RATINGS_RANGE[-1]) self.assertEqual(blog_post.rating_average, settings.RATINGS_RANGE[-1] / 1) else: self.assertEqual(blog_post.rating_count, count) self.assertEqual(blog_post.rating_sum, _sum) self.assertEqual(blog_post.rating_average, average)
def rating_for(context, obj): """ Provides a generic context variable name for the object that ratings are being rendered for, and the rating form. """ context["object_for_rating"] = obj context["form_for_rating"] = RatingForm(obj) return context
def rating_for(context, obj): """ Provides a generic context variable name for the object that ratings are being rendered for, and the rating form. """ context["rating_obj"] = obj context["rating_form"] = RatingForm(obj) for field in obj._meta.many_to_many: if isinstance(field, RatingField): context["rating_average"] = getattr(obj, "%s_average" % field.name) context["rating_count"] = getattr(obj, "%s_count" % field.name) break return context
def rating_for(context, obj): """ Provides a generic context variable name for the object that ratings are being rendered for, and the rating form. """ context["rating_object"] = context["rating_obj"] = obj context["rating_form"] = RatingForm(context["request"], obj) ratings = context["request"].COOKIES.get("mezzanine-rating", "") rating_string = "%s.%s" % (obj._meta, obj.pk) context["rated"] = (rating_string in ratings) rating_name = obj.get_ratingfield_name() for f in ("average", "count", "sum"): context["rating_" + f] = getattr(obj, "%s_%s" % (rating_name, f)) return context
def rating_for(context, obj): """ Provides a generic context variable name for the object that ratings are being rendered for, and the rating form. """ context["rating_object"] = context["rating_obj"] = obj context["rating_form"] = RatingForm(context["request"], obj) ratings = context["request"].COOKIES.get("mezzanine-rating", "") rating_string = f"{obj._meta}.{obj.pk}" context["rated"] = rating_string in ratings rating_name = obj.get_ratingfield_name() for f in ("average", "count", "sum"): context["rating_" + f] = getattr(obj, f"{rating_name}_{f}") return context.flatten()
def rating(request): """ Handle a ``RatingForm`` submission and redirect back to its related object. """ response = initial_validation(request, "rating") if isinstance(response, HttpResponse): return response obj, post_data = response url = obj.get_absolute_url() url = add_cache_bypass(url.split("#")[0]) + "#rating-%s" % obj.id response = redirect(url) rating_form = RatingForm(request, obj, post_data) if rating_form.is_valid(): rating_form.save() if request.is_ajax(): # Reload the object and return the new rating. obj = obj.__class__.objects.get(id=obj.id) fields = ("rating_avg", "rating_count", "rating_sum") json = dumps(dict([(f, getattr(obj, f)) for f in fields])) response = HttpResponse(json) ratings = ",".join(rating_form.previous + [rating_form.current]) set_cookie(response, "mezzanine-rating", ratings) return response
def test_rating(self): """ Test that ratings can be posted and avarage/count are calculated. """ blog_post = BlogPost.objects.create(title="Ratings", user=self._user, status=CONTENT_STATUS_PUBLISHED) data = RatingForm(blog_post).initial for value in RATING_RANGE: data["value"] = value response = self.client.post(reverse("rating"), data=data) response.delete_cookie("mezzanine-rating") blog_post = BlogPost.objects.get(id=blog_post.id) count = len(RATING_RANGE) average = sum(RATING_RANGE) / float(count) self.assertEqual(blog_post.rating_count, count) self.assertEqual(blog_post.rating_average, average)