def vote(): if get_setting('stage') != 'vote': return redirect(url_for('index')) form = VoteForm() form.selection.choices = split_response() error = False if request.method == 'POST': if not form.validate(): error = '폼을 채워 주세요' else: voter = Voters.query.filter_by(key=form.key.data).first() if not voter: error = '키를 잘못 입력했어요' elif voter.voted: error = '이미 투표하셨네요' else: voter.voted = True vote = Vote(int(form.selection.data)) db.session.add(vote) remain = get_remain() if remain == 0: set_stage('result') base = os.path.abspath(os.path.dirname(__file__)) shutil.copy2(os.path.join(base, 'app.db'), os.path.join(base, 'static/app.db')) db.session.commit() return render_template('result.html', vote=vote, select=list(split_response())[vote.selection][1], remain=remain) form.selection.choices = split_response() return render_template('vote.html', form=form, error=error, remain=get_remain())
def vote(recipe_id): form = VoteForm(request.form) if request.method == 'POST' and form.validate(): vote = Vote.query.filter_by( recipe_id=recipe_id, user_id=current_user.id ).first() if vote: vote.value = int(request.form['value']) else: vote = Vote( value=int(request.form['value']), user_id=current_user.id, recipe_id=recipe_id) db.session.add(vote) db.session.commit() recipe = Recipe.query.filter_by(id=recipe_id).first() recipe.calculate_average() db.session.add(recipe) db.session.commit() flash('Vote added successfully', 'success') return redirect(url_for('all_recipes'))
def vote(id): form = VoteForm(request.form) if request.method == "POST" and form.validate(): if _validate_user(form.facebook_uid.data, form.facebook_token.data): if _make_vote(form.service_id.data, form.facebook_uid.data, form.facebook_name.data): return jsonify(result="success") else: logging.info("could not make vote..") else: logging.info("could not validate user...") else: logging.info(len(form.errors)) for error in form.errors: logging.info(error) return abort(403)
def submit_vote(): result = { "success" : 0, "message" : "An error occurred" } if phase() != 2: result["message"] = "Not voting phase!" return json.dumps(result), 200 # return 200 so message displays if not current_user.is_authenticated: # rather than login_required, this allows returning a json result["message"] = "Not logged in" return json.dumps(result), 200 # fetch via with_for_update to lock this row user = User.query.filter_by( id=current_user.id).with_for_update().first_or_404() form = VoteForm() if form.validate() or True: try: nom_id = int(form.nomid.data) except: return json.dumps(result), 200 nom = Nomination.query.filter_by(id=nom_id).first() if nom is None: return json.dumps(result), 200 result["no_vote"] = [] rems = set(nom.award.nominations).intersection(user.selections) for rem in rems: # take away vote from other nom in this category # clicking same button will simply remove the vote user.selections.remove(rem) result["no_vote"].append(str(rem.id)) if nom in rems: # we removed the vote, so we are done result["success"] = 1 result["message"] = "Vote removed" else: # only add vote if it was a different nomination's button nom.voters.append(user) result["success"] = 2 result["message"] = "Vote submitted" result["vote"] = str(nom.id) db.session.commit() return json.dumps(result), 200
def vove(): form = VoteForm(request.form) if request.method == 'POST' and form.validate(): if voted(form.suggestionid.data,str(session['uuid'])): vote = models.Vote(form.suggestionid.data,str(session['uuid']),form.votestatus.data) vote.status = form.votestatus.data models.db.session.add(vote) models.db.session.commit() flash('Thanks, suggestion down-voted') return redirect(url_for('getonesuggestion',suggestid=form.suggestionid.data)) vote = models.Vote(form.suggestionid.data,str(session['uuid']),form.votestatus.data) models.db.session.add(vote) models.db.session.commit() flash('Thanks, suggestion up-voted') return redirect(url_for('getonesuggestion',suggestid=form.suggestionid.data)) return redirect(url_for('getonesuggestion',suggestid=form.suggestionid.data))
def submit_vote(): result = {"success": 0, "message": "An error occurred"} if phase() != 2: result["message"] = "Not voting phase!" return json.dumps(result), 200 # return 200 so message displays if not current_user.is_authenticated: # rather than login_required, this allows returning a json result["message"] = "Not logged in" return json.dumps(result), 200 form = VoteForm() if form.validate() or True: try: nom_id = int(form.nomid.data) except: return json.dumps(result), 200 nom = Nomination.query.filter_by(id=nom_id).first() if nom is None: return json.dumps(result), 200 for sel in current_user.selections: if sel in nom.award.nominations: # take away vote from other nom in this category # clicking same button will simply remove the vote current_user.selections.remove(sel) result["no_vote"] = str(sel.id) if sel == nom: # we removed the vote, so we are done result["success"] = 1 result["message"] = "Vote removed" db.session.commit() return json.dumps(result), 200 break # only add vote if it was a different nomination's button nom.voters.append(current_user) result["success"] = 2 result["message"] = "Vote submitted" result["vote"] = str(nom.id) db.session.commit() return json.dumps(result), 200
def vote(recipe_id): form = VoteForm(request.form) if request.method == 'POST' and form.validate(): vote = Vote.query.filter_by(recipe_id=recipe_id, user_id=current_user.id).first() if vote: vote.value = int(request.form['value']) else: vote = Vote(value=int(request.form['value']), user_id=current_user.id, recipe_id=recipe_id) db.session.add(vote) db.session.commit() recipe = Recipe.query.filter_by(id=recipe_id).first() recipe.calculate_average() db.session.add(recipe) db.session.commit() flash('Vote added successfully', 'success') return redirect(url_for('show_recipe', recipe_id=recipe_id)) flash('Error: You can\'t give an empty or out of range vote', 'danger') return redirect(url_for('show_recipe', recipe_id=recipe_id))