Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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')
Ejemplo n.º 3
0
def vote_create(request, post_id=None):
    post = None
    if post_id:
        post = get_object_or_404(Post, pk=post_id)

    # Check user permission
    if not VotePermission(request.user).can_create_vote(post):
        raise exceptions.PermissionDenied

    # Check if user already vote this post
    user = request.user
    if already_voted(user, post):
        return JsonResponse({
            'success': 0,
            'message': 'Bạn không thể vote 1 bài viết 2 lần'
        })

    if request.GET:
        vote_type = request.GET['type']
        vote = Vote(type=vote_type, post=post, created_by=request.user)
        vote.save()

        # Each upvote increases the user's contribution by 1
        if vote_type == Vote.UP_VOTE:
            voted_user_profile = post.created_by.profile
            voted_user_profile.contribution += 1
            voted_user_profile.save()

        return JsonResponse({
            'success': 1,
            'message': 'Vote của bạn đã được gửi đi'
        })
    else:
        return JsonResponse({
            'success': 0,
            'message': 'Bạn cần phải đăng nhập trước khi vote'
        })
Ejemplo n.º 4
0
def vote_create(request, post_id=None):
    post = None
    if post_id:
        post = get_object_or_404(Post, pk=post_id)

    # Check user permission
    if not VotePermission(request.user).can_create_vote(post):
        raise exceptions.PermissionDenied

    # Check if user already vote this post
    user = request.user
    if already_voted(user, post):
        return JsonResponse({
            'success': 0,
            'message': 'You already voted'
        })

    if request.GET:
        vote_type = request.GET['type']
        vote = Vote(type=vote_type, post=post, created_by=request.user)
        vote.save()

        # Each upvote increases the user's contribution by 1
        if vote_type == Vote.UP_VOTE:
            voted_user_profile = post.created_by.profile
            voted_user_profile.contribution += 1
            voted_user_profile.save()

        return JsonResponse({
            'success': 1,
            'message': 'Vote successfully sent'
        })
    else:
        return JsonResponse({
            'success': 0,
            'message': 'Invalid request'
        })
Ejemplo n.º 5
0
def vote(request):
    '''
        点击喜欢
    '''
    msg = {'success': 0,
           'message': ''}
    user = request.user
    if not user.is_authenticated():
        msg['message'] = 'user_not_login'
        return HttpResponse(json.dumps(msg), content_type='application/json')
    try:
        topic_id = int(request.GET.get('topic_id'))
    except ValueError:
        topic_id = Node
    if topic_id:
        try:
            topic = Topic.objects.get(id=topic_id)
        except Topic.DoesNotExist:
            pass
    if not (topic_id and topic):
        msg['message'] = 'topic_not_exist'
        return HttpResponse(json.dumps(msg), content_type='application/json')
    if user.id == topic.author_id:
        msg['message'] = "can_not_vote_your_topic"
        return HttpResponse(json.dumps(msg), content_type='application/json')
    if Vote.objects.filter(trigger_user=user, involved_topic=topic).exists():
        msg['message'] = 'already_voted'
        return HttpResponse(json.dumps(msg), 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()
    msg['message'] = 'thanks_for_your_vote'
    msg['success'] = 1
    return HttpResponse(json.dumps(msg), content_type='application/json')
Ejemplo n.º 6
0
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, )))
Ejemplo n.º 7
0
 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()
Ejemplo n.º 8
0
 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()