当前位置: 首页>>代码示例>>Python>>正文


Python Vote.vote方法代码示例

本文整理汇总了Python中models.Vote.vote方法的典型用法代码示例。如果您正苦于以下问题:Python Vote.vote方法的具体用法?Python Vote.vote怎么用?Python Vote.vote使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Vote的用法示例。


在下文中一共展示了Vote.vote方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_votes

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import vote [as 别名]
 def test_votes(self):
     u = User('user', 'applez1')
     c = Community('Powerlifting', None, None, None, None)
     post = Posts('This is a Title', 'This is a body', author=u, community=c)
     comment1 = Comments("This is a comment", author=u, post=post)
     v = Vote(u)
     self.assertEqual(u, v.user)
     v = Vote(u, post=post)
     self.assertEqual(v.post, post)
     v = Vote(u, comment=comment1)
     self.assertEqual(v.comment, comment1)
     v.vote(u, post, 1, post)
     self.assertEqual(post.value, 1)
开发者ID:bendominguez011,项目名称:community-project,代码行数:15,代码来源:tests.py

示例2: vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import vote [as 别名]
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}})
开发者ID:spncrlkt,项目名称:emoji-be,代码行数:36,代码来源:app.py

示例3: vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import vote [as 别名]
def vote():
	print "We're here!"
	if request.form.get("vote") == "up": # expression == returns True, if != returns False
		vote_input = 1 # upvote
	else:
		vote_input = -1 # downvote
	post_id = request.form.get("post_id")
	
	vote = Vote.query.filter_by(post_id=post_id, user_id=g.user.id).first()
	
	if vote is None:
		vote = Vote(post_id=post_id, user_id=g.user.id, vote=vote_input) 
		db.session.add(vote)
		db.session.commit()
		db.session.refresh(vote)
	else:
		vote.vote = vote_input
		db.session.commit()

	vote.post.score += vote_input
	# TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int'

	db.session.commit()

	# if vote, Post(score += 1)
	# manual in sql: UPDATE post SET score = (SELECT SUM(vote) FROM vote WHERE post_id = Post.id);

	# change upvote to green if successful?

	# if user upvotes a post, can change to downvote/cancel how?
	# see unique constraint in models.Vote > no that didn't work, see \d vote UNIQUE in psql

	return "Vote recorded!"
开发者ID:amyrlam,项目名称:earthmd,代码行数:35,代码来源:views.py

示例4: update_votes

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import vote [as 别名]
def update_votes():
    '''
    Updates the Vote table with the json retrieved from Kimono.
    If a vote is not in the table it creates one.
    Returns the number of new votes and new orphan players added to the db
    '''
    logger.info('Updating votes...')
    url = settings.KIMONO['votes_url']
    votes = _get_results_collection1(url)
    # Keeping a list of players with votes but not present in the Player table
    # so that they could be added later
    logger.info(' - Updating database...')
    no_new_votes = 0
    no_new_orphans = 0
    for vote in votes:
        p_id = _id_from_url(vote['name']['href'])
        v_day = _day_from_url(vote['url'])
        # Checking if the vote already exists. If not, creates a new one, if it
        # exists it will get the current vote and update it
        try:
            v = Vote.objects.get(player__pk=p_id, day=v_day)
        except Vote.DoesNotExist:
            v = Vote()
            no_new_votes += 1
        # Creating a orphan player if there is not a player for this vote
        try:
            p = Player.objects.get(pk=p_id)
        except Player.DoesNotExist:
            p = Player(pk=p_id)
            p.role = _fix_role(vote['role'])
            p.save()
            no_new_orphans += 1
        v.player = p
        v.vote = _fix_zero(vote['vote'])
        v.gol = _fix_zero(vote['gol'])
        v.assist = _fix_zero(vote['assists'])
        v.penalties_scored_saved = _fix_zero(vote['penalties_scored_saved'])
        v.penalties_missed = _fix_zero(vote['penalties_missed'])
        v.own_gol = _fix_zero(vote['own_gol'])
        v.yellow_cards = _fix_zero(vote['yellow_cards'])
        v.red_cards = _fix_zero(vote['red_cards'])
        v.magicvote = _fix_zero(vote['own_gol'])
        v.day = v_day
        v.sub_in = _sub_in(vote['in']['class'])
        v.sub_out = _sub_out(vote['out']['class'])
        # Storing on the db
        v.save()
    return no_new_votes, no_new_orphans
开发者ID:brescia123,项目名称:fg-backend,代码行数:50,代码来源:db_manager.py

示例5: add_vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import vote [as 别名]
def add_vote(request, uuid, polluuid, choice):
    if request.method == 'GET':
        try:
            vote = Vote.objects.get(voter=uuid, poll=polluuid)
            return HttpResponse(json.dumps('fail: vote exists'),
                        mimetype='application/json')
        except Vote.DoesNotExist:
            vote = Vote()
            _poll = Poll.objects.get(uuid=polluuid)
            vote.voter = User.objects.get(uuid=uuid)
            vote.poll = _poll
            vote.vote = choice
            vote.save()
            _poll.votes = _poll.votes + 1
            _poll.save()
            return HttpResponse(json.dumps('ok'),
                        mimetype='application/json')
    else:
        raise Http404
开发者ID:shaond,项目名称:whichone,代码行数:21,代码来源:views.py

示例6: vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import vote [as 别名]
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')
开发者ID:dhs789520,项目名称:master2015,代码行数:38,代码来源:views-backup.py


注:本文中的models.Vote.vote方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。