def add_options(poll, text, is_date=False): """Add a new option to the poll.""" options_to_add = [x.strip() for x in text.split('\n') if x.strip() != ''] added_options = [] for option_to_add in options_to_add: description = None # Extract the description if existing if not is_date and '-' in option_to_add: # Extract and strip the description splitted = option_to_add.split('-', 1) option_to_add = splitted[0].strip() description = splitted[1].strip() if description == '': description = None if option_is_duplicate( poll, option_to_add) or option_to_add in added_options: continue poll_option = PollOption(poll, option_to_add) poll_option.description = description poll_option.is_date = is_date poll.options.append(poll_option) added_options.append(option_to_add) return added_options
def clone(self, session): """Create a clone from the current poll.""" poll = Poll(self.user) poll.created = True session.add(poll) poll.name = self.name poll.description = self.description poll.poll_type = self.poll_type poll.anonymous = self.anonymous poll.number_of_votes = self.number_of_votes poll.allow_new_options = self.allow_new_options poll.option_sorting = self.option_sorting poll.user_sorting = self.user_sorting poll.results_visible = self.results_visible poll.show_percentage = self.show_percentage from pollbot.models import PollOption for option in self.options: new_option = PollOption(poll, option.name) new_option.description = option.description new_option.is_date = option.is_date session.add(new_option) return poll
def test_cascades_delete_vote(self, session, user, poll): option = PollOption(poll, 'option 0') session.add(option) vote = Vote(user, option) vote.priority = 0 session.add(vote) session.commit() session.delete(poll) session.commit() assert session.query(Vote).count() == 0
def test_unique_ordering(self, session, user, poll): option = PollOption(poll, 'option 0') session.add(option) vote = Vote(user, option) vote.priority = 0 session.add(vote) with pytest.raises(IntegrityError): vote_same_index = Vote(user, option) vote_same_index.priority = 0 session.add(vote_same_index) session.commit()
def test_cascades_dont_delete_poll(self, session, user, poll): option = PollOption(poll, 'option 0') session.add(option) vote = Vote(user, option) vote.priority = 0 session.add(vote) session.commit() session.delete(vote) session.commit() poll_exists = session.query( exists().where(Poll.id == poll.id)).scalar() assert poll_exists