Skip to content

Poll create service

Services related to poll creation.

PollCreateService

Services related to CRUD operation on Polls

Source code in apps/polls_management/services/poll_create_service.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class PollCreateService:
    """
    Services related to CRUD operation on Polls 
    """

    @staticmethod
    def create_or_edit_poll(poll_form: PollForm, options: List[str], user) -> PollModel:
        """Create a new poll starting from a PollForm object (or
        apply the edits on the existing object)

        Args:
            poll_form: form containing data of object you wanna create.
            options: list of all options (as strings). 
                They must be at least 2 and at most 10.

        Raises:
            MissingNameOrQuestionExcetion: your form has not all required data.
            TooFewOptionsExcetion: you put in too few options for this type of poll.
            TooManyOptionsExcetion: you put in too many options for this type of poll.

        Returns:
            poll: the initialized and saved PollModel object
        """
        # validate form
        if not poll_form.is_valid():
            raise PollMainDataNotValidException(f"Some data of passed poll_form is not valid. See errors:\n{poll_form.errors}")

        # validate options
        valid_options = list()
        for o in options:
            if o.strip() != "":
                valid_options.append(o)

        # ensuring is passed at least a certain number of options
        if len(valid_options) < poll_form.get_min_options():
            raise TooFewOptionsException(f"{poll_form.data.get('poll_type')} poll " +
                f"(votable_mj={poll_form.data.get('votable_mj')}) needs at least " +
                f"{poll_form.get_min_options()} options, {len(valid_options)} has given")

        # all polls can have at most 10 options
        if len(valid_options) > 10:
            raise TooManyOptionsException(f"A poll accepts at most 10 options, {len(valid_options)} has given")

        # create poll object from form
        poll = poll_form.save(commit=False)
        poll.author = user
        poll.save()


        # check if there are already options in the poll object
        # and delete them (edit case)
        if poll.options():
            for previous_option in poll.options():
                previous_option.delete()

        # create option objects
        for o_str in valid_options:
            option = PollOptionModel(value=o_str)
            option.poll_fk = poll
            option.save()

        return poll

create_or_edit_poll(poll_form, options, user) staticmethod

Create a new poll starting from a PollForm object (or apply the edits on the existing object)

Parameters:

Name Type Description Default
poll_form PollForm

form containing data of object you wanna create.

required
options List[str]

list of all options (as strings). They must be at least 2 and at most 10.

required

Raises:

Type Description
MissingNameOrQuestionExcetion

your form has not all required data.

TooFewOptionsExcetion

you put in too few options for this type of poll.

TooManyOptionsExcetion

you put in too many options for this type of poll.

Returns:

Name Type Description
poll PollModel

the initialized and saved PollModel object

Source code in apps/polls_management/services/poll_create_service.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@staticmethod
def create_or_edit_poll(poll_form: PollForm, options: List[str], user) -> PollModel:
    """Create a new poll starting from a PollForm object (or
    apply the edits on the existing object)

    Args:
        poll_form: form containing data of object you wanna create.
        options: list of all options (as strings). 
            They must be at least 2 and at most 10.

    Raises:
        MissingNameOrQuestionExcetion: your form has not all required data.
        TooFewOptionsExcetion: you put in too few options for this type of poll.
        TooManyOptionsExcetion: you put in too many options for this type of poll.

    Returns:
        poll: the initialized and saved PollModel object
    """
    # validate form
    if not poll_form.is_valid():
        raise PollMainDataNotValidException(f"Some data of passed poll_form is not valid. See errors:\n{poll_form.errors}")

    # validate options
    valid_options = list()
    for o in options:
        if o.strip() != "":
            valid_options.append(o)

    # ensuring is passed at least a certain number of options
    if len(valid_options) < poll_form.get_min_options():
        raise TooFewOptionsException(f"{poll_form.data.get('poll_type')} poll " +
            f"(votable_mj={poll_form.data.get('votable_mj')}) needs at least " +
            f"{poll_form.get_min_options()} options, {len(valid_options)} has given")

    # all polls can have at most 10 options
    if len(valid_options) > 10:
        raise TooManyOptionsException(f"A poll accepts at most 10 options, {len(valid_options)} has given")

    # create poll object from form
    poll = poll_form.save(commit=False)
    poll.author = user
    poll.save()


    # check if there are already options in the poll object
    # and delete them (edit case)
    if poll.options():
        for previous_option in poll.options():
            previous_option.delete()

    # create option objects
    for o_str in valid_options:
        option = PollOptionModel(value=o_str)
        option.poll_fk = poll
        option.save()

    return poll