def post(): values = {} vote_value = request.form['vote'] vote = Vote() for item in request.form.getlist('value[]'): if vote.get_vote_title(item) is not None: values[item] = vote.get_vote_title(item) elif int(item) == Vote.CUSTOM_VOTE_ID and vote_value: values[item] = vote_value if values: # add vote vote.ip = request.remote_addr vote.votes = values try: vote_current = Vote.objects(ip=request.remote_addr, created_at__gte=(datetime.datetime.now() - datetime.timedelta(minutes=15))) if vote_current: raise ValueError("You've already voted!") vote.save() suffix = VoteView.ordinal(len(Vote.objects)) return jsonify({'status': 'ok', 'message': 'Thank your for voting! You are the %s fan of python' % suffix}) except ValueError as e: return jsonify({'status': 'error', 'message': e.message}) except Exception: return jsonify({'status': 'error', 'message': 'Error while saving vote. Sorry :('}) else: return jsonify({'status': 'error', 'message': 'Choose any option to vote'})
def api_record_vote(): token = session['token'] user = User.query.filter_by(token=token).first() if user.vote.first(): return abort(403) choices = request.args.get('choices') choices_list = choices.split(',') choices_list = choices_list[:len(choices_list) - 1] # remove the last item if len(choices_list) > app.config['MAX_CHOICES']: return abort(403) vote = Vote(owner=user.id) options_list = [] for choice in choices_list: options_list.append(Option.query.filter_by(id=choice).first()) vote.votes = options_list db.session.add(vote) db.session.commit() return jsonify(status="OK")
def post(): values = {} vote_value = request.form['vote'] vote = Vote() for item in request.form.getlist('value[]'): if vote.get_vote_title(item) is not None: values[item] = vote.get_vote_title(item) elif int(item) == Vote.CUSTOM_VOTE_ID and vote_value: values[item] = vote_value if values: # add vote vote.ip = request.remote_addr vote.votes = values try: vote_current = Vote.objects( ip=request.remote_addr, created_at__gte=(datetime.datetime.now() - datetime.timedelta(minutes=15))) if vote_current: raise ValueError("You've already voted!") vote.save() suffix = VoteView.ordinal(len(Vote.objects)) return jsonify({ 'status': 'ok', 'message': 'Thank your for voting! You are the %s fan of python' % suffix }) except ValueError as e: return jsonify({'status': 'error', 'message': e.message}) except Exception: return jsonify({ 'status': 'error', 'message': 'Error while saving vote. Sorry :(' }) else: return jsonify({ 'status': 'error', 'message': 'Choose any option to vote' })