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 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 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 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 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 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 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 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'] # 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 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 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)