本文整理汇总了Python中models.Vote.create方法的典型用法代码示例。如果您正苦于以下问题:Python Vote.create方法的具体用法?Python Vote.create怎么用?Python Vote.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Vote
的用法示例。
在下文中一共展示了Vote.create方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_vote
# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import create [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")
示例2: create_subject
# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import create [as 别名]
def create_subject(topic, group, deadline, votetype, options, creator):
if not get_group(group):
raise Exception('Group not exist')
if votetype not in ['0', '1']:
raise Exception('Vote type error')
options = [o for o in options if o]
if not options:
raise Exception('No options')
subject = Subject.create(topic, deadline, votetype, group, creator)
for option in options:
Vote.create(subject.id, option)