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 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 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 add_nominee(): uid = session.get('osm_uid', None) isadmin = uid in config.ADMINS if not uid or not (config.STAGE.startswith('call') or isadmin): return redirect(url_for('login')) form = AddNomineeForm() form.category.choices = g.category_choices if form.validate(): if form.nomid.data.isdigit(): n = Nominee.get(Nominee.id == int(form.nomid.data)) if n.proposedby != uid and not isadmin: return redirect(url_for('edit_nominees')) else: n = Nominee() n.proposedby = session['osm_uid'] n.status = Nominee.Status.SUBMITTED if request.form.get('submit') == g.lang['deletenominee']: if n.id: n.status = Nominee.Status.DELETED n.save() else: form.populate_obj(n) n.save() return redirect(url_for('edit_nominees')) return 'Error in fields:\n{}'.format( '\n'.join(['{}: {}'.format(k, v) for k, v in form.errors.items()]))
def edit_nominees(n=None, form=None): """Called from login(), a convenience method.""" # Temporary redirect to voting if config.STAGE not in ('call', 'select'): return redirect(url_for('login')) if 'osm_token' not in session: return redirect(url_for('login')) if 'nomination' not in session: session['nomination'] = 0 if n is not None: if len(n) == 1 and n.isdigit() and int(n) < len(config.NOMINATIONS): session['nomination'] = int(n) elif n in config.NOMINATIONS: session['nomination'] = config.NOMINATIONS.index(n) nom = session['nomination'] tmp_obj = None if 'tmp_nominee' in session: tmp_obj = session['tmp_nominee'] del session['tmp_nominee'] form = AddNomineeForm(data=tmp_obj) uid = session['osm_uid'] isadmin = uid in config.ADMINS nominees = Nominee.select(Nominee, Vote.user.alias('voteuser')).where(Nominee.nomination == nom).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (Vote.preliminary))).naive() canadd = isadmin or (config.STAGE == 'call' and Nominee.select().where( (Nominee.proposedby == uid) & (Nominee.nomination == nom)).count() < 10) if isteam(uid): votesq = Nominee.select(Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).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 # Now for the team votes votesq = Nominee.select(Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).join(Vote, JOIN.LEFT_OUTER, on=( (Vote.nominee == Nominee.id) & (Vote.preliminary) & (Vote.user << list(config.TEAM)))).group_by(Nominee.id) teamvotes = {} if isadmin: for v in votesq: teamvotes[v.id] = v.num_votes else: votes = None teamvotes = None return render_template('index.html', form=form, nomination=config.NOMINATIONS[nom], nominees=nominees, user=uid, isadmin=isadmin, canvote=canvote(uid), canunvote=config.STAGE == 'call' or isteam(uid), votes=votes, teamvotes=teamvotes, year=date.today().year, stage=config.STAGE, canadd=canadd, nominations=config.NOMINATIONS, lang=g.lang)
def set_status(nid, status=None): if 'osm_token' not in session or session['osm_uid'] not in config.ADMINS or status is None: return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) n.status = status n.save() return redirect(url_for('edit_nominees'))
def list_chosen(): nominees = Nominee.select().where(Nominee.chosen) return render_template('list.html', nominees=nominees, year=date.today().year, 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 delete_nominee(nid): if 'osm_token' not in session or ( not config.STAGE.startswith('call') and session['osm_uid'] not in config.ADMINS): return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) session['tmp_nominee'] = model_to_dict(n) n.delete_instance(recursive=True) return redirect(url_for('edit_nominees'))
def choose_nominee(nid): if 'osm_token' not in session or session['osm_uid'] not in config.ADMINS: return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) n.chosen = not n.chosen print n.chosen n.save() return redirect(url_for('edit_nominees'))
def delete_nominee(nid): if 'osm_token' not in session or ( config.STAGE != 'call' and session['osm_uid'] not in config.ADMINS): return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) session['tmp_nominee'] = model_to_dict(n) n.delete_instance(recursive=True) return redirect(url_for('edit_nominees'))
def edit_nominee(nid): if 'osm_token' not in session or session['osm_uid'] not in config.ADMINS: return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) n2 = model_to_dict(n) n2['nomid'] = nid session['tmp_nominee'] = n2 return redirect(url_for('edit_nominees'))
def choose_nominee(nid): if 'osm_token' not in session or session['osm_uid'] not in config.ADMINS: return redirect(url_for('login')) n = Nominee.get(Nominee.id == nid) if n.status == Nominee.Status.CHOSEN: n.status = Nominee.Status.ACCEPTED elif n.status == Nominee.Status.ACCEPTED: n.status = Nominee.Status.CHOSEN else: raise Exception('Cannot choose non-accepted nominee') n.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('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'] # 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 add_nominee(): if 'osm_token' not in session or not canvote(session['osm_uid']): return redirect(url_for('login')) form = AddNomineeForm() if form.validate(): if form.nomid.data.isdigit(): n = Nominee.get(Nominee.id == int(form.nomid.data)) else: n = Nominee() n.nomination = session['nomination'] n.proposedby = session['osm_uid'] form.populate_obj(n) n.save() return redirect(url_for('edit_nominees')) return 'Error in fields:\n{}'.format('\n'.join( ['{}: {}'.format(k, v) for k, v in form.errors.items()]))
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 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'))
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 add_nominee(): if 'osm_token' not in session or not canvote(session['osm_uid']): return redirect(url_for('login')) form = AddNomineeForm() if form.validate(): if form.nomid.data.isdigit(): n = Nominee.get(Nominee.id == int(form.nomid.data)) else: n = Nominee() n.nomination = session['nomination'] n.proposedby = session['osm_uid'] form.populate_obj(n) n.save() return redirect(url_for('edit_nominees')) return 'Error in fields:\n{}'.format('\n'.join(['{}: {}'.format(k, v) for k, v in form.errors.items()]))
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 edit_nominees(cat=None, edit_id=None): """Called from login(), a convenience method.""" uid = session.get('osm_uid', None) isadmin = uid in config.ADMINS if config.STAGE not in ('call', 'callvote', 'select') and not isadmin: return redirect(url_for('login')) if cat is None: cat = session.get('nomination', 'core') if cat == 'all': cat = None if isadmin else 'mine' if cat == 'mine' and not uid: cat = 'core' if cat in config.NOMINATIONS or cat is None or cat == 'mine': session['nomination'] = cat nom = session.get('nomination', cat) # Prepare editing form edit_obj = None if edit_id and uid and (isadmin or config.STAGE in ('call', 'callvote')): edit_nom = Nominee.get(Nominee.id == edit_id) if (edit_nom.status == Nominee.Status.SUBMITTED and edit_nom.proposedby == uid) or isadmin: edit_obj = model_to_dict(edit_nom) edit_obj['nomid'] = edit_id form = AddNomineeForm(data=edit_obj) form.category.choices = g.category_choices # Select nominees from the database nominees = Nominee.select(Nominee, Vote.user.alias('voteuser')).join( Vote, JOIN.LEFT_OUTER, on=( (Vote.nominee == Nominee.id) & (Vote.user == uid) & (Vote.preliminary) )).order_by(Nominee.id.desc()) if nom in config.NOMINATIONS: nominees = nominees.where(Nominee.category == nom) elif nom == 'mine': nominees = nominees.where(Nominee.proposedby == uid) if nom != 'mine' and not isadmin: min_status = (Nominee.Status.SUBMITTED if config.STAGE in ('call', 'callvote', 'select') else Nominee.Status.ACCEPTED) nominees = nominees.where(Nominee.status >= min_status) # Calculate the number of votes for the selection team if isteam(uid): votesq = Nominee.select(Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).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 # Prepare a list of categories filterables = list(config.NOMINATIONS) if uid: filterables.insert(0, 'mine') if isadmin: filterables.insert(0, 'all') # All done, return the template canadd = isadmin or (uid and config.STAGE.startswith('call') and Nominee.select().where( Nominee.proposedby == uid).count() < config.MAX_NOMINEES_PER_USER) return render_template('index.html', form=form, nomination=nom or 'all', nominees=nominees.naive(), user=uid, isadmin=isadmin, canvote=canvote(uid), canunvote=config.STAGE == 'callvote' or isteam(uid), votes=votes, statuses={k: v for k, v in Nominee.status.choices}, stage=config.STAGE, canadd=canadd, nominations=filterables, lang=g.lang)
def list_chosen(): uid = session.get('osm_uid', None) nominees = Nominee.select().where(Nominee.status == Nominee.Status.CHOSEN) return render_template('list.html', nominees=nominees, user=uid, nominations=config.NOMINATIONS, lang=g.lang)
def edit_nominees(n=None, form=None): """Called from login(), a convenience method.""" # Temporary redirect to voting if config.STAGE not in ('call', 'select'): return redirect(url_for('login')) if 'osm_token' not in session: return redirect(url_for('login')) if 'nomination' not in session: session['nomination'] = 0 if n is not None: if len(n) == 1 and n.isdigit() and int(n) < len(config.NOMINATIONS): session['nomination'] = int(n) elif n in config.NOMINATIONS: session['nomination'] = config.NOMINATIONS.index(n) nom = session['nomination'] tmp_obj = None if 'tmp_nominee' in session: tmp_obj = session['tmp_nominee'] del session['tmp_nominee'] form = AddNomineeForm(data=tmp_obj) uid = session['osm_uid'] isadmin = uid in config.ADMINS nominees = Nominee.select( Nominee, Vote.user.alias('voteuser')).where(Nominee.nomination == nom).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.user == uid) & (Vote.preliminary))).naive() canadd = isadmin or (config.STAGE == 'call' and Nominee.select().where( (Nominee.proposedby == uid) & (Nominee.nomination == nom)).count() < 10) if isteam(uid): votesq = Nominee.select(Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).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 # Now for the team votes votesq = Nominee.select( Nominee.id, fn.COUNT(Vote.id).alias('num_votes')).join( Vote, JOIN.LEFT_OUTER, on=((Vote.nominee == Nominee.id) & (Vote.preliminary) & (Vote.user << list(config.TEAM)))).group_by(Nominee.id) teamvotes = {} if isadmin: for v in votesq: teamvotes[v.id] = v.num_votes else: votes = None teamvotes = None return render_template('index.html', form=form, nomination=config.NOMINATIONS[nom], nominees=nominees, user=uid, isadmin=isadmin, canvote=canvote(uid), canunvote=config.STAGE == 'call' or isteam(uid), votes=votes, teamvotes=teamvotes, year=date.today().year, stage=config.STAGE, canadd=canadd, nominations=config.NOMINATIONS, lang=g.lang)