Skip to content

Single Option Services

Single Option services related to vote.

SingleOptionVoteService

Handle vote procedures like:

  • perform a vote on a simple poll (one choice)
  • calculate results on a simple poll
Source code in apps/votes_results/services/single_option_vote_service.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class SingleOptionVoteService: 
    """
    Handle vote procedures like:

    - perform a vote on a simple poll (one choice)
    - calculate results on a simple poll
    """

    @staticmethod
    def perform_vote(poll_id: str, poll_choice_id: str) -> VoteModel:
        """
        Perform a vote on a survey.
        Args:
            poll_id: id of poll you want to vote
            poll_choice_id: the voted option
        Raises: 
            PollDoesNotExistException: you tried to vote a not existent poll
            PollOptionUnvalidException: you tried to vote an unvalid option 
                (id doesn't exists or it doesn't belong to this poll)
        Returns:
            vote: the vote model of the result
        """

        vote_builder = VoteBuilder()

        vote_builder.set_poll(poll_id)
        vote_builder.set_voted_option(poll_choice_id)

        vote: VoteModel = vote_builder.perform_creation()
        vote.save()

        return vote

    @staticmethod
    def get_vote_by_id(vote_id: str) -> VoteModel:
        """
        Retrieve a vote by its ID 
        (temporary method! In future we will wanna use <poll_id, user_id>)
        Args:
            vote_id: id of the vote object you want to retrieve
        Raises:
            VoteDoesNotExistException: if vote does not exists
        Returns:
            VoteModel: the vote object by the specified id
        """

        try:
            return VoteModel.objects.get(id=vote_id)
        except ObjectDoesNotExist:
            raise VoteDoesNotExistException()

    @staticmethod
    def calculate_result(poll_id: str, user = None) -> PollResult:
        """
        Calculate result of a poll.
        Args:
            poll_id: id of poll you want to calculate results
        Raises:
            PollDoesNotExistException: you tried to calculate results on a non-existent poll
            ResultsNotAvailableException: you tried to access results on a poll that are not available
        Returns:
            PollResult: results of the poll specified by id
        """

        try:
            poll: PollModel = PollModel.objects.get(id=poll_id)
        except ObjectDoesNotExist:
            raise PollDoesNotExistException(f"Poll with id={poll_id} does not exist")

        # check if the results can be viewed
        if not poll.are_results_visible(user):
            raise ResultsNotAvailableException(f"Results of poll with id={poll_id} are not available")



        return PollResult(poll)

calculate_result(poll_id, user=None) staticmethod

Calculate result of a poll.

Parameters:

Name Type Description Default
poll_id str

id of poll you want to calculate results

required

Raises:

Type Description
PollDoesNotExistException

you tried to calculate results on a non-existent poll

ResultsNotAvailableException

you tried to access results on a poll that are not available

Returns:

Name Type Description
PollResult PollResult

results of the poll specified by id

Source code in apps/votes_results/services/single_option_vote_service.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@staticmethod
def calculate_result(poll_id: str, user = None) -> PollResult:
    """
    Calculate result of a poll.
    Args:
        poll_id: id of poll you want to calculate results
    Raises:
        PollDoesNotExistException: you tried to calculate results on a non-existent poll
        ResultsNotAvailableException: you tried to access results on a poll that are not available
    Returns:
        PollResult: results of the poll specified by id
    """

    try:
        poll: PollModel = PollModel.objects.get(id=poll_id)
    except ObjectDoesNotExist:
        raise PollDoesNotExistException(f"Poll with id={poll_id} does not exist")

    # check if the results can be viewed
    if not poll.are_results_visible(user):
        raise ResultsNotAvailableException(f"Results of poll with id={poll_id} are not available")



    return PollResult(poll)

get_vote_by_id(vote_id) staticmethod

Retrieve a vote by its ID (temporary method! In future we will wanna use )

Parameters:

Name Type Description Default
vote_id str

id of the vote object you want to retrieve

required

Raises:

Type Description
VoteDoesNotExistException

if vote does not exists

Returns:

Name Type Description
VoteModel VoteModel

the vote object by the specified id

Source code in apps/votes_results/services/single_option_vote_service.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@staticmethod
def get_vote_by_id(vote_id: str) -> VoteModel:
    """
    Retrieve a vote by its ID 
    (temporary method! In future we will wanna use <poll_id, user_id>)
    Args:
        vote_id: id of the vote object you want to retrieve
    Raises:
        VoteDoesNotExistException: if vote does not exists
    Returns:
        VoteModel: the vote object by the specified id
    """

    try:
        return VoteModel.objects.get(id=vote_id)
    except ObjectDoesNotExist:
        raise VoteDoesNotExistException()

perform_vote(poll_id, poll_choice_id) staticmethod

Perform a vote on a survey.

Parameters:

Name Type Description Default
poll_id str

id of poll you want to vote

required
poll_choice_id str

the voted option

required

Raises:

Type Description
PollDoesNotExistException

you tried to vote a not existent poll

PollOptionUnvalidException

you tried to vote an unvalid option (id doesn't exists or it doesn't belong to this poll)

Returns:

Name Type Description
vote VoteModel

the vote model of the result

Source code in apps/votes_results/services/single_option_vote_service.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@staticmethod
def perform_vote(poll_id: str, poll_choice_id: str) -> VoteModel:
    """
    Perform a vote on a survey.
    Args:
        poll_id: id of poll you want to vote
        poll_choice_id: the voted option
    Raises: 
        PollDoesNotExistException: you tried to vote a not existent poll
        PollOptionUnvalidException: you tried to vote an unvalid option 
            (id doesn't exists or it doesn't belong to this poll)
    Returns:
        vote: the vote model of the result
    """

    vote_builder = VoteBuilder()

    vote_builder.set_poll(poll_id)
    vote_builder.set_voted_option(poll_choice_id)

    vote: VoteModel = vote_builder.perform_creation()
    vote.save()

    return vote