示例#1
0
文件: app.py 项目: spncrlkt/emoji-be
def vote(definition_id):
    body = request.get_json()

    try:
        definition = db.session.query(Definition).filter_by(id=definition_id).one()
    except NoResultFound as ex:
        return jsonify({'error': 'Definition does not exist'})

    try:
        user = db.session.query(User).filter_by(twitter_id=body.get('userId')).one()
    except NoResultFound as ex:
        return jsonify({'error': 'Auth Error'})

    posted_auth_token = body.get('authToken')
    token = encode_value(
        user.oauth_token,
        app.config.get('AUTH_SALT')
    )
    if token != posted_auth_token:
        return jsonify({'error': 'Auth Error'})

    try:
        vote = db.session.query(Vote).filter_by(definition_id=definition.id).\
                filter_by(user_id=user.id).one()
    except NoResultFound as ex:
        vote = Vote()
        vote.definition_id = definition.id
        vote.user_id = user.id

    vote.vote = 1 if body.get('isUpvote') else -1
    db.session.add(vote)
    db.session.commit()

    return jsonify({'vote': { 'id': vote.id}})
示例#2
0
def vote(req):
    if req.method == 'POST':
        vote_target = get_object_or_404(Gentleman, pk=req.POST['gentleman_id'])
        try:
            vote = Vote.objects.get(user=req.user)
        except Vote.DoesNotExist:
            vote = Vote()
            vote.user_id = req.user.id
            
        if 'execution' in req.POST:
            vote.execution = vote_target
            
        if 'grooming' in req.POST:
            vote.grooming = vote_target
                
        if 'creativity' in req.POST:
            vote.creativity = vote_target
            
        vote.save()
        
        messages.warning(req, '%s' % req.user.vote)
    
    return HttpResponseRedirect(reverse('home'))
示例#3
0
def vote(request):
    if request.session['score']==0:
        return HR(u'需要登录后才可以投票!')
    else:
        did=request.POST['did']
        score=request.POST['score']
         #查询是否已投票此(did)discussid
        if Vote.objects.filter(user_id=request.session['uid'],discuss_id=did).count()==1:
            return HR(u'亲,只能投票一次噢!')

        d=Discuss.objects.get(id=did)

        if d.user_id==request.session['uid']:
            return HR(u'亲,不可以为自己投票的哦!')

        #更新vote表
        v=Vote()
        v.user_id=request.session['uid']
        v.discuss_id=did
        v.vote =score
        v.save()
        #更新用户表,增加score
        u=User.objects.get(id=d.user_id)

        if score=='1':
            d.support +=1
            u.score+=1
        else:
            d.oppose +=1
            u.score-=1
            if u.score <1:
                u.score=1
        d.save()
        u.save()
        
        return HR(u'success')