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


Python Vote.get_vote_by_post_and_user_id方法代码示例

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


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

示例1: process_vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import get_vote_by_post_and_user_id [as 别名]
def process_vote(post_id):
    """this is the function that process users' votes, so it updates the database and refresh the post-details
    page to show the updated votes and vote allocation"""
    if session.get('loggedin', None):
        choice_id = request.form.get("choice_id")
        user_id = session['loggedin']

        post = Post.get_post_by_id(post_id)
        previous_vote = post.check_choice_on_post_by_user_id(user_id)

        if previous_vote:  # if there is a previous vote, compare to the new vote see if they pointed to the same choice, update vote
            if previous_vote != choice_id:
                vote_id = Vote.get_vote_by_post_and_user_id(post.post_id, user_id)
                Vote.update_vote(vote_id, choice_id)
        else:
            Vote.create(user_id=user_id, choice_id=choice_id)  # if it's first time vote, create a new vote

        vote_dict, total_votes, chart_dict = post.count_votes()

        bar_chart_gender = post.bar_chart_gender()
        geo_chart_location = post.count_votes_by_location()
        bar_chart_age = post.count_votes_by_age()
        total_votes_percent = {}
        for vote in vote_dict:
            total_votes_percent[vote] = float(vote_dict[vote]) / total_votes

        return json.dumps(
            [vote_dict, total_votes_percent, total_votes, chart_dict, bar_chart_gender, geo_chart_location,
             bar_chart_age])
    else:
        return json.dumps("undefined")
开发者ID:PeggyZheng,项目名称:Opinionated,代码行数:33,代码来源:server.py


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