def test_add_candidate(self): """ Test the add_candidate function. """ create_elections(self.session) self.assertRaises( nuancierlib.NuancierException, nuancierlib.add_candidate, session=self.session, candidate_file='test.png', candidate_name='test image', candidate_author='pingou', candidate_license='CC-BY-SA', candidate_submitter='pingou', submitter_email='[email protected]', candidate_original_url=None, election_id=2, ) nuancierlib.add_candidate( session=self.session, candidate_file='test.png', candidate_name='test image', candidate_author='pingou', candidate_license='CC-BY-SA', candidate_submitter='pingou', submitter_email='[email protected]', candidate_original_url=None, election_id=2, user='******', ) self.session.commit() candidates = nuancierlib.get_candidates(self.session, 2, False) self.assertEqual(1, len(candidates)) self.assertEqual('test image', candidates[0].candidate_name) self.assertEqual('test.png', candidates[0].candidate_file) candidates = nuancierlib.get_candidates(self.session, 2, True) self.assertEqual(0, len(candidates)) self.assertRaises( nuancierlib.NuancierException, nuancierlib.add_candidate, session=self.session, candidate_file='test.png', candidate_name='test image', candidate_author='pingou', candidate_license='CC-BY-SA', candidate_submitter='pingou', submitter_email='[email protected]', candidate_original_url='http://example.org', election_id=2, user='******', )
def test_get_candidates(self): """ Test the get_candidates function. """ create_elections(self.session) create_candidates(self.session) candidates = nuancierlib.get_candidates(self.session, 1) self.assertEqual(2, len(candidates)) self.assertEqual('DSC_0930', candidates[0].candidate_name) self.assertEqual('DSC_0951', candidates[1].candidate_name) candidates = nuancierlib.get_candidates(self.session, 2) self.assertEqual(2, len(candidates)) self.assertEqual('DSC_0922', candidates[0].candidate_name) self.assertEqual('DSC_0923', candidates[1].candidate_name)
def admin_review(election_id): ''' Review a new election. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if election.election_open: flask.flash( 'This election is already open to public votes and can no ' 'longer be changed', 'error') return flask.redirect(flask.url_for('admin_index')) if election.election_public: flask.flash( 'The results of this election are already public, this election' ' can no longer be changed', 'error') return flask.redirect(flask.url_for('admin_index')) candidates = nuancierlib.get_candidates(SESSION, election_id) return flask.render_template( 'admin_review.html', election=election, form=nuancier.forms.ConfirmationForm(), candidates=candidates, picture_folder=os.path.join( APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join( APP.config['CACHE_FOLDER'], election.election_folder))
def admin_review_status(election_id, status): ''' Review a new election depending on the status of the candidates. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if election.election_open: flask.flash( 'This election is already open to public votes and can no ' 'longer be changed', 'error') if election.election_public: flask.flash( 'The results of this election are already public, this election' ' can no longer be changed', 'error') status = flask.request.args.get('status', status) if status == 'all': _status = None elif status in ['pending', 'denied']: _status = False else: _status = True candidates = nuancierlib.get_candidates(SESSION, election_id, approved=_status) if status == 'pending': candidates = [ candidate for candidate in candidates if candidate.approved_motif in [None, ''] ] elif status == 'denied': candidates = [ candidate for candidate in candidates if candidate.approved_motif not in [None, ''] ] template = 'admin_review.html' if election.election_public or election.election_open \ or not nuancier.is_nuancier_admin(flask.g.fas_user): template = 'admin_review_ro.html' return flask.render_template( template, election=election, form=nuancier.forms.ConfirmationForm(), candidates=candidates, picture_folder=os.path.join(APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join(APP.config['CACHE_FOLDER'], election.election_folder), status=status, )
def admin_review_status(election_id, status): ''' Review a new election depending on the status of the candidates. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if election.election_open: flask.flash( 'This election is already open to public votes and can no ' 'longer be changed', 'error') if election.election_public: flask.flash( 'The results of this election are already public, this election' ' can no longer be changed', 'error') status = flask.request.args.get('status', status) if status == 'all': _status = None elif status in ['pending', 'denied']: _status = False else: _status = True candidates = nuancierlib.get_candidates( SESSION, election_id, approved=_status ) if status == 'pending': candidates = [ candidate for candidate in candidates if candidate.approved_motif in [None, ''] ] elif status == 'denied': candidates = [ candidate for candidate in candidates if candidate.approved_motif not in [None, ''] ] template = 'admin_review.html' if election.election_public or election.election_open \ or not nuancier.is_nuancier_admin(flask.g.fas_user): template = 'admin_review_ro.html' return flask.render_template( template, election=election, form=nuancier.forms.ConfirmationForm(), candidates=candidates, picture_folder=os.path.join( APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join( APP.config['CACHE_FOLDER'], election.election_folder), status=status, )
def test_get_candidates(self): """ Test the get_candidates function. """ create_elections(self.session) create_candidates(self.session) candidates = nuancierlib.get_candidates(self.session, 1, False) self.assertEqual(2, len(candidates)) self.assertEqual('Image too narrow', candidates[0].candidate_name) self.assertEqual('Image ok', candidates[1].candidate_name) candidates = nuancierlib.get_candidates(self.session, 1, True) self.assertEqual(0, len(candidates)) candidates = nuancierlib.get_candidates(self.session, 3, False) self.assertEqual(2, len(candidates)) self.assertEqual('Image too small2.0', candidates[0].candidate_name) self.assertEqual('Image too small2.1', candidates[1].candidate_name) candidates = nuancierlib.get_candidates(self.session, 2, True) self.assertEqual(0, len(candidates))
def test_get_candidates(self): """ Test the get_candidates function. """ create_elections(self.session) create_candidates(self.session) candidates = nuancierlib.get_candidates(self.session, 1, False) self.assertEqual(2, len(candidates)) self.assertEqual('Image too narrow', candidates[0].candidate_name) self.assertEqual('Image ok', candidates[1].candidate_name) candidates = nuancierlib.get_candidates(self.session, 1, True) self.assertEqual(0, len(candidates)) candidates = nuancierlib.get_candidates(self.session, 3, False) self.assertEqual(4, len(candidates)) self.assertEqual('Image too small2.0', candidates[0].candidate_name) self.assertEqual('Image too small2.1', candidates[1].candidate_name) self.assertEqual('Image too small2.2', candidates[2].candidate_name) self.assertEqual('Image too small2.3', candidates[3].candidate_name) candidates = nuancierlib.get_candidates(self.session, 2, True) self.assertEqual(0, len(candidates))
def vote(election_id): ''' Give the possibility to the user to vote for an election. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') candidates = nuancierlib.get_candidates(SESSION, election_id, approved=True) if not election.election_open: flask.flash('This election is not open', 'error') return flask.redirect(flask.url_for('index')) if flask.g.fas_user: username = flask.g.fas_user.username if not isinstance(username, six.binary_type): username = username.encode('utf-8') random.seed(int(hashlib.sha1(username).hexdigest(), 16) % 100000) random.shuffle(candidates) # How many votes the user made: votes = nuancierlib.get_votes_user(SESSION, election_id, flask.g.fas_user.username) if len(votes) >= election.election_n_choice: flask.flash( 'You have cast the maximal number of votes ' 'allowed for this election.', 'error') return flask.redirect( flask.url_for('election', election_id=election_id)) if len(votes) > 0: candidate_done = [cdt.candidate_id for cdt in votes] candidates = [ candidate for candidate in candidates if candidate.id not in candidate_done ] return flask.render_template( 'vote.html', election=election, form=nuancier.forms.ConfirmationForm(), candidates=candidates, n_votes_done=len(votes), picture_folder=os.path.join(APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join(APP.config['CACHE_FOLDER'], election.election_folder))
def vote(election_id): ''' Give the possibility to the user to vote for an election. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') candidates = nuancierlib.get_candidates( SESSION, election_id, approved=True) if not election.election_open: flask.flash('This election is not open', 'error') return flask.redirect(flask.url_for('index')) if flask.g.fas_user: random.seed( int( hashlib.sha1(flask.g.fas_user.username).hexdigest(), 16 ) % 100000) random.shuffle(candidates) # How many votes the user made: votes = nuancierlib.get_votes_user(SESSION, election_id, flask.g.fas_user.username) if len(votes) >= election.election_n_choice: flask.flash('You have cast the maximal number of votes ' 'allowed for this election.', 'error') return flask.redirect( flask.url_for('election', election_id=election_id)) if len(votes) > 0: candidate_done = [cdt.candidate_id for cdt in votes] candidates = [candidate for candidate in candidates if candidate.id not in candidate_done] return flask.render_template( 'vote.html', election=election, form=nuancier.forms.ConfirmationForm(), candidates=candidates, n_votes_done=len(votes), picture_folder=os.path.join( APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join( APP.config['CACHE_FOLDER'], election.election_folder) )
def election(election_id): ''' Display the index page of the election will all the candidates submitted. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') # How many votes the user made: votes = [] can_vote = True if hasattr(flask.g, 'fas_user') and flask.g.fas_user: votes = nuancierlib.get_votes_user(SESSION, election_id, flask.g.fas_user.username) if election.election_open and len(votes) < election.election_n_choice: if len(votes) > 0: flask.flash('You have already voted, but you can still vote ' 'on more candidates.') return flask.redirect(flask.url_for('vote', election_id=election_id)) elif election.election_open and len(votes) >= election.election_n_choice: can_vote = False elif not election.election_public: flask.flash('This election is not open', 'error') return flask.redirect(flask.url_for('elections_list')) candidates = nuancierlib.get_candidates(SESSION, election_id, approved=True) if hasattr(flask.g, 'fas_user') and flask.g.fas_user: username = flask.g.fas_user.username if not isinstance(username, six.binary_type): username = username.encode('utf-8') random.seed(int(hashlib.sha1(username).hexdigest(), 16) % 100000) random.shuffle(candidates) return flask.render_template( 'election.html', candidates=candidates, election=election, can_vote=can_vote, picture_folder=os.path.join(APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join(APP.config['CACHE_FOLDER'], election.election_folder))
def election(election_id): ''' Display the index page of the election will all the candidates submitted. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') # How many votes the user made: votes = [] can_vote = True if hasattr(flask.g, 'fas_user') and flask.g.fas_user: votes = nuancierlib.get_votes_user(SESSION, election_id, flask.g.fas_user.username) if election.election_open and len(votes) < election.election_n_choice: if len(votes) > 0: flask.flash('You have already voted, but you can still vote ' 'on more candidates.') return flask.redirect(flask.url_for('vote', election_id=election_id)) elif election.election_open and len(votes) >= election.election_n_choice: can_vote = False elif not election.election_public: flask.flash('This election is not open', 'error') return flask.redirect(flask.url_for('elections_list')) candidates = nuancierlib.get_candidates( SESSION, election_id, approved=True) if hasattr(flask.g, 'fas_user') and flask.g.fas_user: random.seed( int( hashlib.sha1(flask.g.fas_user.username).hexdigest(), 16 ) % 100000) random.shuffle(candidates) return flask.render_template( 'election.html', candidates=candidates, election=election, can_vote=can_vote, picture_folder=os.path.join( APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join( APP.config['CACHE_FOLDER'], election.election_folder) )
def test_add_candidate(self): """ Test the add_candidate function. """ create_elections(self.session) nuancierlib.add_candidate( session=self.session, candidate_file='test.png', candidate_name='test image', candidate_author='pingou', election_id=2, ) self.session.commit() candidates = nuancierlib.get_candidates(self.session, 2) self.assertEqual(1, len(candidates)) self.assertEqual('test image', candidates[0].candidate_name) self.assertEqual('test.png', candidates[0].candidate_file)
def admin_review(election_id): ''' Review a new election. ''' election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if election.election_open: flask.flash( 'This election is already open to public votes and can no ' 'longer be changed', 'error') if election.election_public: flask.flash( 'The results of this election are already public, this election' ' can no longer be changed', 'error') status = flask.request.args.get('status', 'all') if status == 'all': status = None candidates = nuancierlib.get_candidates( SESSION, election_id, approved=status ) template = 'admin_review.html' if election.election_public or election.election_open \ or not nuancier.is_nuancier_admin(flask.g.fas_user): template = 'admin_review_ro.html' return flask.render_template( template, election=election, form=nuancier.forms.ConfirmationForm(), candidates=candidates, picture_folder=os.path.join( APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join( APP.config['CACHE_FOLDER'], election.election_folder))
def process_vote(election_id): ''' Actually register the vote, after checking if the user is actually allowed to vote. ''' form = nuancier.forms.ConfirmationForm() if not form.validate_on_submit(): flask.flash('Wrong input submitted', 'error') return flask.render_template('msg.html') election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if not election.election_open: flask.flash('This election is not open', 'error') return flask.render_template('msg.html') candidates = nuancierlib.get_candidates(SESSION, election_id, approved=True) candidate_ids = set([candidate.id for candidate in candidates]) entries = set( [int(entry) for entry in flask.request.form.getlist('selection')]) # If not enough candidates selected if not entries: flask.flash('You did not select any candidate to vote for.', 'error') return flask.redirect(flask.url_for('vote', election_id=election_id)) # If vote on candidates from other elections if not set(entries).issubset(candidate_ids): flask.flash( 'The selection you have made contains element which are ' 'not part of this election, please be careful.', 'error') return flask.redirect(flask.url_for('vote', election_id=election_id)) # How many votes the user made: votes = nuancierlib.get_votes_user(SESSION, election_id, flask.g.fas_user.username) # Too many votes -> redirect if len(votes) >= election.election_n_choice: flask.flash( 'You have cast the maximal number of votes ' 'allowed for this election.', 'error') return flask.redirect( flask.url_for('election', election_id=election_id)) # Selected more candidates than allowed -> redirect if len(votes) + len(entries) > election.election_n_choice: flask.flash( 'You selected %s wallpapers while you are only allowed ' 'to select %s' % (len(entries), (election.election_n_choice - len(votes))), 'error') return flask.render_template( 'vote.html', form=nuancier.forms.ConfirmationForm(), election=election, candidates=[ nuancierlib.get_candidate(SESSION, candidate_id) for candidate_id in entries ], n_votes_done=len(votes), picture_folder=os.path.join(APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join(APP.config['CACHE_FOLDER'], election.election_folder)) # Allowed to vote, selection sufficient, choice confirmed: process for selection in entries: value = 1 if nuancier.has_weigthed_vote(flask.g.fas_user): value = 2 nuancierlib.add_vote(SESSION, selection, flask.g.fas_user.username, value=value) try: SESSION.commit() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() LOG.debug( 'ERROR: could not process the vote - user: "******" ' 'election: "%s"', flask.g.fas_user.username, election_id) LOG.exception(err) flask.flash( 'An error occured while processing your votes, please ' 'report this to your lovely admin or see logs for ' 'more details', 'error') flask.flash('Your vote has been recorded, thank you for voting on ' '%s %s' % (election.election_name, election.election_year)) if election.election_badge_link: flask.flash('Do not forget to <a href="%s" target="_blank">claim your ' 'badge!</a>' % election.election_badge_link) return flask.redirect(flask.url_for('elections_list'))
def process_vote(election_id): ''' Actually register the vote, after checking if the user is actually allowed to vote. ''' form = nuancier.forms.ConfirmationForm() if not form.validate_on_submit(): flask.flash('Wrong input submitted', 'error') return flask.render_template('msg.html') election = nuancierlib.get_election(SESSION, election_id) if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if not election.election_open: flask.flash('This election is not open', 'error') return flask.render_template('msg.html') candidates = nuancierlib.get_candidates( SESSION, election_id, approved=True) candidate_ids = set([candidate.id for candidate in candidates]) entries = set([int(entry) for entry in flask.request.form.getlist('selection')]) # If not enough candidates selected if not entries: flask.flash('You did not select any candidate to vote for.', 'error') return flask.redirect(flask.url_for('vote', election_id=election_id)) # If vote on candidates from other elections if not set(entries).issubset(candidate_ids): flask.flash('The selection you have made contains element which are ' 'not part of this election, please be careful.', 'error') return flask.redirect(flask.url_for('vote', election_id=election_id)) # How many votes the user made: votes = nuancierlib.get_votes_user(SESSION, election_id, flask.g.fas_user.username) # Too many votes -> redirect if len(votes) >= election.election_n_choice: flask.flash('You have cast the maximal number of votes ' 'allowed for this election.', 'error') return flask.redirect( flask.url_for('election', election_id=election_id)) # Selected more candidates than allowed -> redirect if len(votes) + len(entries) > election.election_n_choice: flask.flash('You selected %s wallpapers while you are only allowed ' 'to select %s' % ( len(entries), (election.election_n_choice - len(votes))), 'error') return flask.render_template( 'vote.html', form=nuancier.forms.ConfirmationForm(), election=election, candidates=[nuancierlib.get_candidate(SESSION, candidate_id) for candidate_id in entries], n_votes_done=len(votes), picture_folder=os.path.join( APP.config['PICTURE_FOLDER'], election.election_folder), cache_folder=os.path.join( APP.config['CACHE_FOLDER'], election.election_folder) ) # Allowed to vote, selection sufficient, choice confirmed: process for selection in entries: value = 1 if nuancier.has_weigthed_vote(flask.g.fas_user): value = 2 nuancierlib.add_vote( SESSION, selection, flask.g.fas_user.username, value=value) try: SESSION.commit() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() LOG.debug('ERROR: could not process the vote - user: "******" ' 'election: "%s"', flask.g.fas_user.username, election_id) LOG.exception(err) flask.flash('An error occured while processing your votes, please ' 'report this to your lovely admin or see logs for ' 'more details', 'error') flask.flash('Your vote has been recorded, thank you for voting on ' '%s %s' % (election.election_name, election.election_year)) if election.election_badge_link: flask.flash('Do not forget to <a href="%s" target="_blank">claim your ' 'badge!</a>' % election.election_badge_link) return flask.redirect(flask.url_for('elections_list'))
def admin_process_review(election_id): ''' Process the reviewing of a new election. ''' if not nuancier.is_nuancier_admin(flask.g.fas_user): flask.flash('You are not an administrator of nuancier', 'error') return flask.redirect(flask.url_for('msg')) status = flask.request.args.get('status', None) endpoint = 'admin_review' if status: endpoint = 'admin_review_status' election = nuancierlib.get_election(SESSION, election_id) form = nuancier.forms.ConfirmationForm() if not form.validate_on_submit(): flask.flash('Wrong input submitted', 'error') return flask.render_template('msg.html') if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if election.election_open: flask.flash( 'This election is already open to public votes and can no ' 'longer be changed', 'error') return flask.redirect(flask.url_for('results_list')) if election.election_public: flask.flash( 'The results of this election are already public, this election' ' can no longer be changed', 'error') return flask.redirect(flask.url_for('results_list')) candidates = nuancierlib.get_candidates(SESSION, election_id) candidates_id = [str(candidate.id) for candidate in candidates] candidates_selected = flask.request.form.getlist('candidates_id') motifs = flask.request.form.getlist('motifs') action = flask.request.form.get('action') if action: action = action.strip() if action not in ['Approved', 'Denied']: flask.flash('Only the actions "Approved" or "Denied" are accepted', 'error') return flask.redirect( flask.url_for(endpoint, election_id=election_id, status=status)) selections = [] for cand in candidates_id: if cand not in candidates_selected: selections.append(None) else: selections.append(cand) if action == 'Denied': req_motif = False if not motifs: req_motif = True for cnt in range(len(motifs)): motif = motifs[cnt] if selections[cnt] and not motif.strip(): req_motif = True break if req_motif: flask.flash('You must provide a reason to deny a candidate', 'error') return flask.redirect( flask.url_for(endpoint, election_id=election_id, status=status)) cnt = 0 for candidate in candidates_selected: if candidate not in candidates_id: flask.flash( 'One of the candidate submitted was not candidate in this ' 'election', 'error') return flask.redirect( flask.url_for(endpoint, election_id=election_id, status=status)) msgs = [] for candidate in selections: if candidate: candidate = nuancierlib.get_candidate(SESSION, candidate) motif = None if len(motifs) > cnt: motif = motifs[cnt].strip() if action == 'Approved': candidate.approved = True candidate.approved_motif = motif else: candidate.approved = False candidate.approved_motif = motif if APP.config.get('NUANCIER_EMAIL_NOTIFICATIONS', False): # pragma: no cover nuancierlib.notifications.email_publish( to_email=candidate.submitter_email, img_title=candidate.candidate_name, motif=motif) else: LOG.warning( 'Should have sent an email to "%s" about "%s" that has' ' been rejected because of "%s"', candidate.submitter_email, candidate.candidate_name, motif) SESSION.add(candidate) msgs.append({ 'topic': 'candidate.%s' % (action.lower()), 'msg': dict( agent=flask.g.fas_user.username, election=election.api_repr(version=1), candidate=candidate.api_repr(version=1), ) }) cnt += 1 try: SESSION.commit() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() LOG.debug( 'User: "******" could not approve/deny candidate(s) for ' 'election "%s"', flask.g.fas_user.username, election_id) LOG.exception(err) flask.flash('Could not approve/deny candidate', 'error') flask.flash('Candidate(s) updated') for msg in msgs: nuancierlib.notifications.publish( topic=msg['topic'], msg=msg['msg'], ) return flask.redirect( flask.url_for(endpoint, election_id=election_id, status=status))
def admin_process_review(election_id): ''' Process the reviewing of a new election. ''' if not nuancier.is_nuancier_admin(flask.g.fas_user): flask.flash('You are not an administrator of nuancier', 'error') return flask.redirect(flask.url_for('msg')) status = flask.request.args.get('status', None) endpoint = 'admin_review' if status: endpoint = 'admin_review_status' election = nuancierlib.get_election(SESSION, election_id) form = nuancier.forms.ConfirmationForm() if not form.validate_on_submit(): flask.flash('Wrong input submitted', 'error') return flask.render_template('msg.html') if not election: flask.flash('No election found', 'error') return flask.render_template('msg.html') if election.election_open: flask.flash( 'This election is already open to public votes and can no ' 'longer be changed', 'error') return flask.redirect(flask.url_for('results_list')) if election.election_public: flask.flash( 'The results of this election are already public, this election' ' can no longer be changed', 'error') return flask.redirect(flask.url_for('results_list')) candidates = nuancierlib.get_candidates(SESSION, election_id) candidates_id = [str(candidate.id) for candidate in candidates] candidates_selected = flask.request.form.getlist('candidates_id') motifs = flask.request.form.getlist('motifs') action = flask.request.form.get('action') if action: action = action.strip() if action not in ['Approved', 'Denied']: flask.flash( 'Only the actions "Approved" or "Denied" are accepted', 'error') return flask.redirect(flask.url_for( endpoint, election_id=election_id, status=status)) selections = [] for cand in candidates_id: if cand not in candidates_selected: selections.append(None) else: selections.append(cand) if action == 'Denied': req_motif = False if not motifs: req_motif = True for cnt in range(len(motifs)): motif = motifs[cnt] if selections[cnt] and not motif.strip(): req_motif = True break if req_motif: flask.flash( 'You must provide a motif to deny a candidate', 'error') return flask.redirect(flask.url_for( endpoint, election_id=election_id, status=status)) cnt = 0 for candidate in candidates_selected: if candidate not in candidates_id: flask.flash( 'One of the candidate submitted was not candidate in this ' 'election', 'error') return flask.redirect(flask.url_for( endpoint, election_id=election_id, status=status)) msgs = [] for candidate in selections: if candidate: candidate = nuancierlib.get_candidate(SESSION, candidate) motif = None if len(motifs) > cnt: motif = motifs[cnt].strip() if action == 'Approved': candidate.approved = True candidate.approved_motif = motif else: candidate.approved = False candidate.approved_motif = motif if APP.config.get( 'NUANCIER_EMAIL_NOTIFICATIONS', False): # pragma: no cover nuancierlib.notifications.email_publish( to_email=candidate.submitter_email, img_title=candidate.candidate_name, motif=motif) else: LOG.warning( 'Should have sent an email to "%s" about "%s" that has' ' been rejected because of "%s"', candidate.submitter_email, candidate.candidate_name, motif) SESSION.add(candidate) msgs.append({ 'topic': 'candidate.%s' % (action.lower()), 'msg': dict( agent=flask.g.fas_user.username, election=election.api_repr(version=1), candidate=candidate.api_repr(version=1), ) }) cnt += 1 try: SESSION.commit() except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() LOG.debug('User: "******" could not approve/deny candidate(s) for ' 'election "%s"', flask.g.fas_user.username, election_id) LOG.exception(err) flask.flash('Could not approve/deny candidate', 'error') flask.flash('Candidate(s) updated') for msg in msgs: nuancierlib.notifications.publish( topic=msg['topic'], msg=msg['msg'], ) return flask.redirect(flask.url_for( endpoint, election_id=election_id, status=status))