def api_poll_vote(): poll = request.get_json() poll_title, option = (poll["poll_title"], poll["option"]) join_tables = Polls.query.join(Topics).join(Options) # Get topic and username from the database topic = Topics.query.filter_by(title=poll_title).first() user = Users.query.filter_by(username=session["user"]).first() # filter options option = (join_tables.filter(Topics.title.like(poll_title)).filter( Options.name.like(option)).first()) # check if the user has voted on this poll poll_count = (UserPolls.query.filter_by(topic_id=topic.id).filter_by( user_id=user.id).count()) if poll_count > 0: return jsonify({"message": "Sorry! multiple votes are not allowed"}) if option: # record user and poll user_poll = UserPolls(topic_id=topic.id, user_id=user.id) db.session.add(user_poll) # increment vote_count by 1 if the option was found option.vote_count += 1 db.session.commit() return jsonify({"message": "Thank you for voting"}) return jsonify( {"message": "option or poll was not found please try again"})
def api_poll_vote(): poll = request.get_json() poll_title, option = (poll['poll_title'], poll['option']) join_tables = Polls.query.join(Topics).join(Options) # Get topic and username from the database topic = Topics.query.filter_by(title=poll_title, status=1).first() user = Users.query.filter_by(username=session['user']).first() # if poll was closed in the background before user voted if not topic: return jsonify({'message': 'Sorry! this poll has been closed'}) # filter options option = join_tables.filter(Topics.title.like(poll_title), Topics.status == 1).filter(Options.name.like(option)).first() # check if the user has voted on this poll poll_count = UserPolls.query.filter_by(topic_id=topic.id).filter_by(user_id=user.id).count() if poll_count > 0: return jsonify({'message': 'Sorry! multiple votes are not allowed'}) if option: # record user and poll user_poll = UserPolls(topic_id=topic.id, user_id=user.id) db.session.add(user_poll) # increment vote_count by 1 if the option was found option.vote_count += 1 db.session.commit() return jsonify({'message': 'Thank you for voting'}) return jsonify({'message': 'option or poll was not found please try again'})
def api_poll_vote(): poll = request.get_json() poll_title, option_name = (poll['poll_title'], poll['option']) join_tables = Votes.query.join(Polls).join(Options) # Get votes, poll, username, and option from the database poll = Polls.query.filter_by(title=poll_title, status=True).first() user = Users.query.filter_by(username=session['user']).first() option = Options.query.filter_by(name=option_name).first() # if poll was closed in the background before user voted if not poll: return jsonify({'message': 'Sorry! this poll has been closed'}) # filter option names option_name = join_tables.filter( Polls.title.like(poll_title), Polls.status == True).filter(Options.name.like(option_name)).first() if option_name: # check if the user has voted on this poll poll_count = UserPolls.query.filter_by(poll_id=poll.id).filter_by( user_id=user.id).filter_by(option_id=option.id).count() if poll_count > 0: return jsonify( {'message': 'Sorry! multiple votes are not allowed'}) # record userpoll and vote user_poll = UserPolls(poll_id=poll.id, user_id=user.id, option_id=option.id) user_vote = Votes(poll_id=poll.id, user_id=user.id, option_id=option.id, vote_count=0) db.session.add(user_poll) db.session.add(user_vote) # increment vote_count by 1 if the option was found option_name.vote_count += 1 db.session.commit() return jsonify({'message': 'Thank you for voting'}) return jsonify( {'message': 'Option or poll was not found please try again'})
def api_poll_vote(): poll = request.get_json() poll_title, option = (poll['poll_title'], poll['option']) join_tables = Polls.query.join(Topics).join(Options) # Get topic and username topic = Topics.query.filter_by(title=poll_title, status=True).first() user = _request_ctx_stack.top.current_user user_identifier = user.get('email') or user.get('nickname') # if the user has not verified their email abort if not user.get('email_verified'): return jsonify( {'message': 'You have to verify your email before you vote'}) # if poll was closed in the background before user voted if not topic: return jsonify({'message': 'Sorry! this poll has been closed'}) # filter options option = join_tables.filter(Topics.title.like(poll_title)).\ filter(Options.name.like(option)).first() # check if the user has voted on this poll poll_count = UserPolls.query.filter_by(topic_id=topic.id).\ filter_by(user_identifier=user_identifier).count() if poll_count: return jsonify( {'message': 'Multiple votes are not allowed on this poll'}) if option: # record user and poll user_poll = UserPolls(topic_id=topic.id, user_identifier=user_identifier) db.session.add(user_poll) # increment vote_count by 1 if the option was found option.vote_count += 1 db.session.commit() return jsonify({'message': 'Thank you for voting'}) return jsonify( {'message': 'Option or poll was not found please try again'})