def save(self): """ Saves a new rating - authenticated users can update the value if they've previously rated. """ user = self.request.user rating_value = self.cleaned_data["value"] rating_name = self.target_object.get_ratingfield_name() rating_manager = getattr(self.target_object, rating_name) if user.is_authenticated(): try: rating_instance = rating_manager.get(user=user) except Rating.DoesNotExist: rating_instance = Rating(user=user, value=rating_value) rating_manager.add(rating_instance) else: if rating_instance.value != int(rating_value): rating_instance.value = rating_value rating_instance.save() else: # User submitted the same rating as previously, # which we treat as undoing the rating (like a toggle). rating_instance.delete() else: rating_instance = Rating(value=rating_value) rating_manager.add(rating_instance) return rating_instance
def handle(self, **options): if options["follow_old"]: self.follow_old() return try: user_id = User.objects.filter(is_superuser=1)[0].id except IndexError: return for url in options["urls"]: for entry in parse(url).entries: link = self.entry_to_link_dict(entry) if options["follow"]: try: link["link"] = self.follow_redirects(link["link"]) except Exception as e: print("%s - skipping %s" % (e, link["link"])) continue link["user_id"] = user_id try: obj = Link.objects.get(link=link["link"]) except Link.DoesNotExist: obj = Link.objects.create(**link) obj.rating.add(Rating(value=1, user_id=user_id), bulk=False) print("Added %s" % obj)
def test_comment_ratings(self): """ Test that a generic relation defined on one of Mezzanine's generic models (in this case ratings of comments) correctly sets its extra fields. """ blog_post = BlogPost.objects.create(title="Post with comments", user=self._user) content_type = ContentType.objects.get_for_model(blog_post) kwargs = { "content_type": content_type, "object_pk": blog_post.id, "site_id": settings.SITE_ID, "comment": "First!!!11" } comment = ThreadedComment.objects.create(**kwargs) comment.rating.create(value=settings.RATINGS_RANGE[0]) comment.rating.add(Rating(value=settings.RATINGS_RANGE[-1])) comment = ThreadedComment.objects.get(pk=comment.pk) self.assertEqual(len(comment.rating.all()), comment.rating_count) self.assertEqual( comment.rating_average, (settings.RATINGS_RANGE[0] + settings.RATINGS_RANGE[-1]) / 2)
def rating(request): """ Handle a ``RatingForm`` submission and redirect back to its related object. """ try: model = get_model(*request.POST["content_type"].split(".", 1)) obj = model.objects.get(id=request.POST["object_pk"]) url = obj.get_absolute_url() + "#rating-%s" % obj.id except (KeyError, TypeError, AttributeError, ObjectDoesNotExist): # Something was missing from the post so abort. return HttpResponseRedirect("/") try: rating_value = int(request.POST["value"]) except (KeyError, ValueError): return HttpResponseRedirect(url) ratings = request.COOKIES.get("mezzanine-rating", "").split(",") rating_string = "%s.%s" % (request.POST["content_type"], request.POST["object_pk"]) if rating_string in ratings: # Already rated so abort. return HttpResponseRedirect(url) obj.rating.add(Rating(value=rating_value)) response = HttpResponseRedirect(url) ratings.append(rating_string) expiry = 60 * 60 * 24 * 365 set_cookie(response, "mezzanine-rating", ",".join(ratings), expiry) return response
def rating(request): """ Handle a ``RatingForm`` submission and redirect back to its related object. """ try: model = get_model(*request.POST["content_type"].split(".", 1)) obj = model.objects.get(id=request.POST["object_pk"]) url = obj.get_absolute_url() + "#rating-%s" % obj.id except (KeyError, TypeError, AttributeError, ObjectDoesNotExist): # Something was missing from the post so abort. return HttpResponseRedirect("/") try: rating_value = int(request.POST["value"]) except (KeyError, ValueError): return HttpResponseRedirect(url) # There can only be one ``RatingField``, find its manager. for field in obj._meta.many_to_many: if isinstance(field, RatingField): rating_manager = getattr(obj, field.name) break else: raise TypeError("%s doesn't contain a RatingField." % obj) ratings = request.COOKIES.get("mezzanine-rating", "").split(",") rating_string = "%s.%s" % (request.POST["content_type"], request.POST["object_pk"]) if rating_string in ratings: # Already rated so abort. return HttpResponseRedirect(url) rating_manager.add(Rating(value=rating_value)) response = HttpResponseRedirect(url) ratings.append(rating_string) expiry = 60 * 60 * 24 * 365 set_cookie(response, "mezzanine-rating", ",".join(ratings), expiry) return response
def handle(self, *urls, **options): try: user_id = User.objects.filter(is_superuser=1)[0].id except IndexError: return for url in urls: for entry in parse(url).entries: link = self.entry_to_link_dict(entry) link["user_id"] = user_id try: obj = Link.objects.get(link=link["link"]) except Link.DoesNotExist: obj = Link.objects.create(**link) obj.rating.add(Rating(value=1, user_id=user_id))