def auto_remove(startpost, reason): invalidate_cache(startpost) if startpost.reply_to == None: for post in Post.objects.filter(thread=startpost.pk): if reason.cost == 0: PostVote.objects.filter(post=post).delete() PostEdit.objects.filter(post=post).delete() post.delete() else: PostVote( user=User.objects.get(pk=1), post=post, reason=reason, positive=False, auto=True ).save() modify_rating(post, reason.cost) else: for post in Post.objects.filter(reply_to=startpost.pk): auto_remove(post, reason) if reason.cost == 0: PostVote.objects.filter(post=post).delete() PostEdit.objects.filter(post=post).delete() post.delete() else: PostVote( user=User.objects.get(pk=1), post=post, reason=reason, positive=False, auto=True ).save() modify_rating(post, reason.cost)
def vote_async(request, post_id, positive): post = get_object_or_404(Post, pk=post_id) if post.draft: return {"message": _("EDRAFT")} if not request.user.is_authenticated(): return {"message": _("EACCES")} if post.owner == request.user: return {"message": _("ELOOP")} if request.user.profile.use_force("VOTE"): try: PostVote(post=post, user=request.user, positive=bool(int(positive))).save() except IntegrityError: return {"message": _("EEXIST")} else: request.user.profile.save() else: return {"message": _("ENOFORCE")} if bool(int(positive)): modify_rating(post, 1, True) return {"rating": post.rating+1} else: modify_rating(post, 1, False) return {"rating": post.rating-1}
def save(self, **args): if self.cleaned_data['reason'].cost == 0: auto_remove(startpost, self.cleaned_data['reason']) PostVote.objects.filter(post = startpost).delete() PostEdit.objects.filter(post = startpost).delete() startpost.delete() else: postvote = super(RemoveForm, self).save(commit=False, **args) postvote.post = startpost postvote.user = User.objects.get(pk=1) postvote.positive = False postvote.auto = False postvote.save() modify_rating(startpost, postvote.reason.cost) if startpost != startpost.thread: auto_remove(startpost, postvote.reason)
def vote_generic(request, post_id, positive): post = get_object_or_404(Post, pk=post_id) if post.draft: return error(request, _("EDRAFT")) if post.owner == request.user: return error(request, _("ELOOP")) if request.user.profile.use_force("VOTE"): try: PostVote(post=post, user=request.user, positive=bool(int(positive))).save() except IntegrityError: return error( request, _("EEXIST") ) else: request.user.profile.save() else: return error(request, "VOTE") modify_rating(post, 1, bool(int(positive))) if post.reply_to: paginator = Paginator(Post.objects.filter(thread=post.thread) \ .exclude(pk=post.thread.pk), settings.PAGE_LIMITATIONS["FORUM_COMMENTS"]) for page in paginator.page_range: if post in paginator.page(page).object_list: return HttpResponseRedirect( "%s?offset=%i#post_%i" % ( reverse("forum.views.thread", args=[post.thread.pk]), page, post.pk ) ) else: return HttpResponseRedirect( reverse("forum.views.thread", args=[post.pk]) )