def get_vote(request): user = request.user if not user.is_authenticated(): return HttpResponse(json.dumps({ 'success': 0, 'message': 'user_not_login' }), content_type='application/json') try: topic_id = int(request.GET.get('topic_id')) except (TypeError, ValueError): topic_id = None topic = None if topic_id: try: topic = Topic.objects.select_related('author').get(pk=topic_id) except Topic.DoesNotExist: pass if not (topic_id and topic): return HttpResponse(json.dumps({ 'success': 0, 'message': 'topic_not_exist' }), content_type='application/json') if user.id == topic.author.id: return HttpResponse(json.dumps({ 'success': 0, 'message': 'can_not_vote_your_topic' }), content_type='application/json') if Vote.objects.filter(trigger_user=user, involved_topic=topic).exists(): return HttpResponse(json.dumps({ 'success': 0, 'message': 'already_voted' }), content_type='application/json') vote = Vote(trigger_user=user, involved_type=0, involved_topic=topic, \ involved_user=topic.author, status=0, occurrence_time=timezone.now()) vote.save() # 更新话题作者声誉 topic_time_diff = timezone.now() - topic.created reputation = topic.author.reputation or 0 reputation = reputation + 2 * math.log( (user.reputation or 0) + topic_time_diff.days + 10, 10) ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation) return HttpResponse(json.dumps({ 'success': 1, 'message': 'thanks_for_your_vote' }), content_type='application/json')
def voting(request, pk, vote): if request.method == "POST": post = get_object_or_404(Post, id=pk) try: obj = Vote.objects.get(user=request.user, post=post) if obj.value != vote: if vote == 1 and obj.value == 2: post.votes_up += 1 post.votes_down -= 1 elif vote == 2 and obj.value == 1: post.votes_up -= 1 post.votes_down += 1 obj.value = vote obj.save() post.save() return HttpResponseRedirect(reverse('post-detail', args=(pk, ))) except Vote.DoesNotExist: vote_new = Vote() vote_new.user = request.user vote_new.post = post vote_new.value = vote if vote == 1: post.votes_up += 1 elif vote == 2: post.votes_down += 1 vote_new.save() post.save() return HttpResponseRedirect( reverse('post-detail', args=(post.id, ))) else: post = get_object_or_404(Post, id=pk) return HttpResponseRedirect(reverse('post-detail', args=(post.id, )))
def process_vote_action(self, value): self.update_node_score(value) vote = Vote(node=self.node, user=self.user, action=self, value=value) vote.save()