def canvote(uid): if session['osm_uid'] in config.ADMINS: return True if config.STAGE != 'call' and not isteam(uid): return False return Vote.select().join(Nominee).where( (Vote.user == uid) & (Vote.preliminary) & (Nominee.nomination == session['nomination'])).count() < 5
def wait(): uid = session['osm_uid'] if 'osm_uid' in session else 0 isadmin = uid in config.ADMINS nominees = Nominee.select(Nominee, Vote.user.alias('voteuser')).where(Nominee.status == Nominee.Status.CHOSEN).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (~Vote.preliminary))).naive() # For admin, populate the dict of votes winners = {x: [0, 0] for x in config.NOMINATIONS} if isadmin or config.STAGE == 'results': votesq = Nominee.select(Nominee.id, Nominee.category, fn.COUNT(Vote.id).alias('num_votes')).where(Nominee.status == Nominee.Status.CHOSEN).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (~Vote.preliminary))).group_by(Nominee.id) votes = {} for v in votesq: votes[v.id] = v.num_votes if v.num_votes > winners[v.category][1]: winners[v.category] = (v.id, v.num_votes) else: votes = None # Count total number of voters total = Vote.select(fn.Distinct(Vote.user)).where(~Vote.preliminary).group_by(Vote.user).count() # Update a link in the description desc = g.lang['stages'][config.STAGE]['description'] desc = desc.replace('{', '<a href="{}">'.format( url_for('static', filename='osmawards2020.txt'))).replace('}', '</a>') # Yay, done return render_template('wait.html', nominees=nominees, description=desc, isadmin=isadmin, votes=votes, stage=config.STAGE, total=total, winners=winners, isresults=config.STAGE == 'results', nominations=config.NOMINATIONS, lang=g.lang)
def voting(): """Called from login(), a convenience method.""" if 'osm_token' not in session: return redirect(url_for('login')) if config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] isadmin = uid in config.ADMINS nominees_list = Nominee.select(Nominee, Vote.user.alias('voteuser')).where(Nominee.chosen).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (~Vote.preliminary))).naive() # Shuffle the nominees nominees = [n for n in nominees_list] rnd = Random() rnd.seed(uid) rnd.shuffle(nominees) # For admin, populate the dict of votes if isadmin: votesq = Nominee.select(Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).where(Nominee.chosen).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (~Vote.preliminary))).group_by(Nominee.id) votes = {} for v in votesq: votes[v.id] = v.num_votes else: votes = None # Count total number of voters total = Vote.select(fn.Distinct(Vote.user)).where(~Vote.preliminary).group_by(Vote.user).count() # Yay, done return render_template('voting.html', nominees=nominees, year=date.today().year, isadmin=isadmin, votes=votes, stage=config.STAGE, total=total, nominations=config.NOMINATIONS, lang=g.lang)
def wait(): uid = session['osm_uid'] if 'osm_uid' in session else 0 isadmin = uid in config.ADMINS nominees = Nominee.select(Nominee, Vote.user.alias('voteuser')).where(Nominee.chosen).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (~Vote.preliminary))).naive() # For admin, populate the dict of votes winners = [[0, 0] for x in range(len(config.NOMINATIONS))] if isadmin or config.STAGE == 'results': votesq = Nominee.select(Nominee.id, Nominee.nomination, fn.COUNT(Vote.id).alias('num_votes')).where(Nominee.chosen).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (~Vote.preliminary))).group_by(Nominee.id) votes = {} for v in votesq: votes[v.id] = v.num_votes if v.num_votes > winners[v.nomination][1]: winners[v.nomination] = (v.id, v.num_votes) else: votes = None # Count total number of voters total = Vote.select(fn.Distinct(Vote.user)).where(~Vote.preliminary).group_by(Vote.user).count() # Update a link in the description desc = g.lang['stages'][config.STAGE]['description'] desc = desc.replace('{', '<a href="{}">'.format(url_for('static', filename='osmawards2016.txt'))).replace('}', '</a>') # Yay, done return render_template('wait.html', nominees=nominees, year=date.today().year, description=desc, isadmin=isadmin, votes=votes, stage=config.STAGE, total=total, winners=winners, isresults=config.STAGE == 'results', nominations=config.NOMINATIONS, lang=g.lang)
def vote(): """ Vote on a question. Requires a POST or PUT with JSON dictionary containing: - `_id`: ObjectId of the question to vote on. - `vote`: ObjectId of answer to vote for. """ data = request.json log.debug("vote data: %s", data) question_id = data['id'] log.info("answer lookup: id=%s, question.id=%s", data['vote'], question_id) answer = Answer.query.filter(Answer.question_id == question_id, Answer.id == data['vote']).first() if answer is None: abort(404) log.debug("clean old votes") if not clean_old_votes(question_id): answer.question.modified = datetime.now() log.info("new vote: user=%s, question=%s, answer=%s", g.account.id, question_id, answer.id) vote = Vote(account=g.account, answer=answer) pos = data.get('position') if pos is not None: vote.position_raw = jsonify(pos) if 'coords' in pos: vote.latitude = pos['coords']['latitude'] vote.longitude = pos['coords']['longitude'] db.session.add(vote) db.session.commit() log.debug("populate return data") ret = question_dict(Question.query.filter(Question.id == question_id).first(), {question_id: vote.answer_id}) log.info("commit vote record(s)") commit() log.debug("returning vote info: %s", ret) return render_json(ret)
def vote(user_id, take_id): user = User.query.filter_by(id=user_id).first() if user is None: return failure_response("User not found") take = Take.query.filter_by(id=take_id).first() if take is None: return failure_response("Take not found") if user_id == take.user_id: return failure_response("User cannot vote on their own take") for vote in Vote.query.filter_by(user_id=user_id).all(): if vote.take_id == take_id: return failure_response("User already voted") body = json.loads(request.data) value = body.get("value") if value is None: return failure_response("User must provide a vote value") new_vote = Vote(value=body.get("value"), take=take) db.session.add(new_vote) take.votes.append(new_vote) user.voted.append(new_vote) db.session.commit() return success_response(new_vote.serialize())
def vote_all(): if 'osm_token' not in session or config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] # Delete current votes to replace by with the new ones q = Vote.delete().where((Vote.user == uid) & (~Vote.preliminary)) q.execute() for nom in config.NOMINATIONS: votes = request.form.getlist('vote_{}'.format(nom)) for vote in votes: v = Vote() v.nominee = Nominee.get(Nominee.id == int(vote)) v.user = uid v.preliminary = False v.save() flash(g.lang['thanksvoted']) return redirect(url_for('voting'))
def prevote(nid): if 'osm_token' not in session: return redirect(url_for('login')) uid = session['osm_uid'] if config.STAGE != 'call' and not isteam(uid): return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) try: v = Vote.get((Vote.user == uid) & (Vote.nominee == n) & (Vote.preliminary)) v.delete_instance() except Vote.DoesNotExist: if canvote(uid): v = Vote() v.nominee = n v.user = uid v.preliminary = True v.save() return redirect(url_for('edit_nominees'))
def voting(): """Called from login(), a convenience method.""" if 'osm_token' not in session: return redirect(url_for('login')) if config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] isadmin = uid in config.ADMINS nominees_list = Nominee.select(Nominee, Vote.user.alias('voteuser')).where( Nominee.chosen).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (~Vote.preliminary))).naive() # Shuffle the nominees nominees = [n for n in nominees_list] rnd = Random() rnd.seed(uid) rnd.shuffle(nominees) # For admin, populate the dict of votes if isadmin: votesq = Nominee.select( Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).where(Nominee.chosen).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (~Vote.preliminary))).group_by(Nominee.id) votes = {} for v in votesq: votes[v.id] = v.num_votes else: votes = None # Count total number of voters total = Vote.select(fn.Distinct( Vote.user)).where(~Vote.preliminary).group_by(Vote.user).count() # Yay, done return render_template('voting.html', nominees=nominees, year=date.today().year, isadmin=isadmin, votes=votes, stage=config.STAGE, total=total, nominations=config.NOMINATIONS, lang=g.lang)
def vote(nid): if 'osm_token' not in session or config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] n = Nominee.get(Nominee.id == nid) try: # Delete votes from the same category by this voter v = Vote.select().where((Vote.user == uid) & (~Vote.preliminary)).join(Nominee).where( Nominee.category == n.category).get() v.delete_instance() except Vote.DoesNotExist: pass v = Vote() v.nominee = n v.user = uid v.preliminary = False v.save() return redirect(url_for('voting'))
async def vote_answer(player: "PlayerConnection", session, *, answer_uuid): answer = session.query(GivenAnswer).filter( GivenAnswer.uuid == answer_uuid).first() pig = player.player_in_game(session) # check that answer and pig have same team if answer.player.team == pig.team: res = (session.query(Vote).filter(Vote.answer_id == answer.id).filter( Vote.subplayer_id == pig.id).first()) if res is not None: # already exists return vote = Vote(answer_id=answer.id, subplayer_id=pig.id) try: session.add(vote) await notify_team_of_answer(player, session, answer) except IntegrityError: pass
def vote(nid): if 'osm_token' not in session or config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] n = Nominee.get(Nominee.id == nid) try: # Delete votes from the same category by this voter v = Vote.select().where((Vote.user == uid) & (~Vote.preliminary)).join(Nominee).where( Nominee.nomination == n.nomination).get() v.delete_instance() except Vote.DoesNotExist: pass v = Vote() v.nominee = n v.user = uid v.preliminary = False v.save() return redirect(url_for('voting'))
def voting(): """Called from login(), a convenience method.""" if 'osm_token' not in session: return redirect(url_for('list_chosen')) if config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] isadmin = uid in config.ADMINS nominees_list = Nominee.select(Nominee, Vote.user.alias('voteuser')).where(Nominee.status == Nominee.Status.CHOSEN).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (~Vote.preliminary))).naive() # Shuffle the nominees nominees = [n for n in nominees_list] rnd = Random() rnd.seed(uid) rnd.shuffle(nominees) # Make a dict of categories user voted in cats = set([x.category for x in Nominee.select(Nominee.category).join(Vote, JOIN.INNER, on=((Vote.nominee == Nominee.id) & (~Vote.preliminary) & (Vote.user == uid))).distinct()]) # For admin, populate the dict of votes if isadmin: votesq = Nominee.select(Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).where(Nominee.status == Nominee.Status.CHOSEN).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (~Vote.preliminary))).group_by(Nominee.id) votes = {} for v in votesq: votes[v.id] = v.num_votes else: votes = None # Count total number of voters total = Vote.select(fn.Distinct(Vote.user)).where(~Vote.preliminary).group_by(Vote.user).count() readmore = (g.lang['stages']['voting']['readmore'] .replace('{', '<a href="{}">'.format( g.lang['stages']['voting']['readmore_link'])) .replace('}', '</a>')) # Yay, done return render_template('voting.html', nominees=nominees, isadmin=isadmin, votes=votes, stage=config.STAGE, total=total, voted_cats=cats, readmore=readmore, nominations=config.NOMINATIONS, lang=g.lang)
def vote_all(): if 'osm_token' not in session or config.STAGE != 'voting': return redirect(url_for('login')) uid = session['osm_uid'] for nom in range(len(config.NOMINATIONS)): vote = request.form.get('vote{}'.format(nom), -1, type=int) if vote < 0: continue try: # Delete votes from the same category by this voter v = Vote.select().where((Vote.user == uid) & (~Vote.preliminary)).join(Nominee).where( Nominee.nomination == nom).get() v.delete_instance() except Vote.DoesNotExist: pass if vote > 0: v = Vote() v.nominee = Nominee.get(Nominee.id == vote) v.user = uid v.preliminary = False v.save() flash(g.lang['thanksvoted']) return redirect(url_for('voting'))
def vote_song(self, user, song_id=None, stream_url=None): """Vote for a song""" session = Session() if stream_url: packet = session.query(Packet).filter_by( stream_url=stream_url, player_name=PLAYER_NAME).first() elif song_id is not None: packet = session.query(Packet).filter_by( song_id=song_id, player_name=PLAYER_NAME).first() else: raise Exception('Must specify either song_id or stream_url') if packet: # Song is already queued; add a vote if user == packet.user: session.rollback() raise Exception('User %s has already voted for this song' % user) try: packet.additional_votes.append(Vote(user=user)) session.commit() except FlushError: session.rollback() raise Exception('User %s has already voted for this song' % user) self._update_finish_times(packet.user) else: # Song is not queued; queue it if stream_url: if 'www.youtube.com' in stream_url: try: video_details = get_youtube_video_details(stream_url) packet = Packet(stream_url=stream_url, stream_title=video_details['title'], stream_length=video_details['length'], stream_id=video_details['stream_id'], art_uri=video_details['art_uri'], user=user, arrival_time=self.virtual_time, player_name=PLAYER_NAME) session.add(packet) session.commit() except Exception, e: session.rollback() raise e elif 'soundcloud.com' in stream_url: try: track_obj = get_soundcloud_music_details(stream_url) packet = Packet(stream_url=stream_url, stream_title=track_obj['title'], stream_length=track_obj['length'], stream_id=track_obj['stream_id'], art_uri=track_obj['art_uri'], artist=track_obj['artist'], user=user, arrival_time=self.virtual_time, player_name=PLAYER_NAME) session.add(packet) session.commit() except Exception, e: session.rollback() raise e else: