def applycontrib(): """Handler for contributing membership application.""" if current_user.is_contrib(): flash('You are already an SPI contributing member.') return redirect(url_for('mainpage')) applications = get_db().get_applications_by_user(current_user) if not current_user.is_contrib(): for apps in applications: if apps.contribapp and apps.approve is None: flash('You already have an outstanding SPI contributing ' + 'membership application.') return redirect(url_for('mainpage')) form = ContribApplicationForm() if form.validate_on_submit(): application = get_db().create_contrib_application( current_user, form.contrib.data, form.sub_private.data) get_db().update_member_field(current_user.email, 'lastactive', datetime.date.today()) if application: return redirect(url_for('mainpage')) flash('Error creating contributing member application.') else: form.sub_private.data = True return render_template('contrib-application.html', form=form)
def view_vote(voteid): """Handler for viewing a specific vote.""" if not current_user.is_contrib(): return render_template('contrib-only.html') vote = get_db().get_vote(voteid) if not vote: flash('Unknown vote ID!') return redirect(url_for('mainpage')) membervote = get_db().get_membervote(current_user, vote) form = VotingForm() if form.validate_on_submit(): if not vote.is_active(): flash('Vote is not currently running.') elif membervote is None: membervote = get_db().create_membervote(current_user, vote) if vote.is_active() and membervote: if form.vote.data != membervote.votestr(): res = membervote.set_vote(form.vote.data) if isinstance(res, basestring): flash(res) else: get_db().store_membervote(membervote) if membervote: form.vote.data = membervote.votestr() return render_template('vote.html', form=form, membervote=membervote, vote=vote)
def list_votes(): """Handler for listing votes""" if not current_user.is_contrib(): return render_template('contrib-only.html') votes = get_db().get_votes() return render_template('votes.html', votes=votes)
def mainpage(): """Handler for main page. Displays users details.""" applications = get_db().get_applications_by_user(current_user) form = MemberForm() form.name.data = current_user.name contribapp = False if not current_user.is_contrib(): form.sub_private.data = False for apps in applications: if apps.contribapp and not apps.approve: contribapp = True else: form.sub_private.data = current_user.sub_private() return render_template('status.html', db=get_db(), applications=applications, contribapp=contribapp, form=form)
def create_vote(): """Handler that creates a new vote.""" if not current_user.is_contrib(): return render_template('contrib-only.html') if not current_user.can_createvote(): flash('You are not allowed to create new votes.') return redirect(url_for('mainpage')) form = VoteCreationForm() if form.validate_on_submit(): vote = get_db().create_vote(current_user, form.title.data, form.description.data, form.start.data, form.end.data, form.system.data) return redirect(url_for('edit_vote', voteid=vote.voteid)) return render_template('vote-create.html', form=form)
def view_vote_result(voteid): """Handler for viewing a specific vote result.""" if not current_user.is_contrib(): return render_template('contrib-only.html') vote = get_db().get_vote(voteid) if not vote: flash('Unknown vote ID!') return redirect(url_for('mainpage')) if vote.owner.memid != current_user.memid: flash('You can only view results for your own votes.') return redirect(url_for('mainpage')) if not vote.is_over(): flash('Vote must be finished to view results.') return redirect(url_for('mainpage')) # Get our list of member votes, sorted by the result cookie # as we can't do a sort on a method in the jinga2 template. membervotes = sorted(get_db().get_membervotes(vote), key=lambda vote: vote.resultcookie()) # Choose the correct voting system. Must match VOTE_SYSTEMS if vote.system == 0: votesystem = SPI.CondorcetVS(vote, membervotes) elif vote.system == 1: votesystem = SPI.CondorcetVS(vote, membervotes, ignoremissing=False) elif vote.system == 2: votesystem = SPI.OpenSTVVS(vote, membervotes) else: flash('Unsupported voting system ID ' + str(vote.system) + '.') return redirect(url_for('mainpage')) # Run the actual vote votesystem.run() return render_template('vote-result.html', membervotes=membervotes, vote=vote, votesystem=votesystem)
def edit_vote(voteid): """Handler for editing a vote.""" if not current_user.is_contrib(): return render_template('contrib-only.html') vote = get_db().get_vote(voteid) # Do some sanity checking of our inputs. if not vote: flash('Unknown vote ID!') return redirect(url_for('mainpage')) if vote.owner.memid != current_user.memid: flash('You can only edit your own votes.') return redirect(url_for('mainpage')) if vote.is_active() or vote.is_over(): flash('Vote must not have run to be edited.') return redirect(url_for('mainpage')) # Only use the submitted form to update the VoteCreation # details if that's what was actually submitted. if request.form and 'vote-btn' in request.form: form = VoteCreationForm(request.form, vote, prefix="vote") else: form = VoteCreationForm(None, vote, prefix="vote") # We always want a new voting option displayed newoptform = VoteOptionForm(prefix="newopt") # Slightly hacky, to deal with the fact we've got 4 different form # submission options to deal with (in the order they appear below): # - Delete/edit the vote itself # - Add new a voting option # - Delete the last current voting option # - Edit an existing voting option if request.form: if 'vote-btn' in request.form and form.validate_on_submit(): if request.form['vote-btn'] == 'Delete': get_db().delete_vote(voteid) flash('Vote deleted.') return redirect(url_for('mainpage')) elif request.form['vote-btn'] == 'Edit': if form.winners.data >= len(vote.options): flash('Must have more options than required winners.') else: vote.title = form.title.data vote.description = form.description.data vote.start = form.start.data vote.end = form.end.data vote.winners = form.winners.data vote.system = form.system.data vote = get_db().update_vote(vote) elif 'obtn' in request.form and newoptform.validate_on_submit(): if request.form['obtn'] == 'Add': if not newoptform.option.data: flash('Must provide option text!') else: vote = get_db().add_vote_option(vote, newoptform.option.data, newoptform.char.data, newoptform.order.data) elif request.form['obtn'] == 'Delete': vote = get_db().delete_vote_option(vote.options[-1]) else: for i, option in enumerate(vote.options, start=1): if ('obtn' + str(i) in request.form and request.form['obtn' + str(i)] == 'Edit'): oform = VoteOptionForm(prefix="opt" + str(i)) if oform.validate_on_submit(): if not oform.option.data: flash('Must provide option text!') else: option.description = oform.option.data option.char = oform.char.data vote = get_db().update_vote_option(option) # Build our array of voting option forms with what the current # vote looks like (we may have added or removed options above, # so this can't be done earlier) oforms = [] for i, option in enumerate(vote.options, start=1): oform = VoteOptionForm(prefix="opt" + str(i)) oform.order.data = i if (not request.form or 'vote-btn' in request.form or not 'opt'+str(i)+'-char' in request.form): oform.option.data = option.description oform.char.data = option.char oforms.append(oform) # Add the new vote option form. if not vote.options: newoptform.order.data = 1 else: newoptform.order.data = len(vote.options) + 1 newoptform.char.data = chr(64 + newoptform.order.data) newoptform.option.data = '' oforms.append(newoptform) return render_template('vote-edit.html', vote=vote, form=form, oforms=oforms)