Example #1
0
    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 Option

        for option in self.options:
            new_option = Option(poll, option.name)
            new_option.description = option.description
            new_option.is_date = option.is_date
            new_option.index = option.index
            session.add(new_option)

        return poll
Example #2
0
def add_option(poll, text, added_options, is_date):
    """Parse the incoming text and create a single option from it."""
    description = None
    description_descriminator = None
    if "--" in text:
        description_descriminator = '--'
    elif "—" in text:
        description_descriminator = '—'
    # Extract the description if existing

    if description_descriminator is not None:
        # Extract and strip the description
        splitted = text.split(description_descriminator, 1)
        text = splitted[0].strip()
        description = splitted[1].strip()
        if description == "":
            description = None

    if option_is_duplicate(poll, text) or text in added_options:
        return None

    option = Option(poll, text)
    option.description = description
    option.is_date = is_date
    poll.options.append(option)

    return option
Example #3
0
def clone_poll(session, original_poll: Poll):
    """Create a clone from the current poll."""
    new_poll = Poll(original_poll.user)
    new_poll.created = True
    session.add(new_poll)

    new_poll.name = original_poll.name
    new_poll.description = original_poll.description
    new_poll.poll_type = original_poll.poll_type
    new_poll.anonymous = original_poll.anonymous
    new_poll.number_of_votes = original_poll.number_of_votes
    new_poll.allow_new_options = original_poll.allow_new_options
    new_poll.option_sorting = original_poll.option_sorting
    new_poll.user_sorting = original_poll.user_sorting
    new_poll.results_visible = original_poll.results_visible
    new_poll.show_percentage = original_poll.show_percentage

    for option in original_poll.options:
        new_option = Option(new_poll, option.name)
        new_option.description = option.description
        new_option.is_date = option.is_date
        new_option.index = option.index
        session.add(new_option)

    return new_poll
Example #4
0
 def test_cascades_delete_vote(self, session, user, poll):
     option = Option(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
Example #5
0
 def test_unique_ordering(self, session, user, poll):
     option = Option(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()
Example #6
0
 def test_cascades_dont_delete_poll(self, session, user, poll):
     option = Option(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
Example #7
0
def add_option(poll: Poll, text: str, added_options: List[str], is_date: bool):
    """Parse the incoming text and create a single option from it.

    We allow option descriptions after an `--` or `—` delimiter.
    `added_options` is a list of names of options that have already
       been added during this single request.
    """
    description = None

    # Check if there's a description delimiter in the line
    description_descriminator = None
    if "--" in text:
        description_descriminator = "--"
    elif "—" in text:
        description_descriminator = "—"

    # Extract the description if existing
    if description_descriminator is not None:
        splitted = text.split(description_descriminator, 1)
        text = splitted[0].strip()
        description = splitted[1].strip()
        if description == "":
            description = None

    # Early return, if this option already exists, or
    # if we already added this option in this request
    if option_is_duplicate(poll, text) or text in added_options:
        return None

    option = Option(poll, text)
    option.description = description
    option.is_date = is_date
    poll.options.append(option)

    added_options.append(text)

    return option