Skip to content

Majority Judgment Services

Majority Judgment services related to vote.

MajorityJudjmentVoteService

Class that handles vote procedures for the majority vote case

Source code in apps/votes_results/services/majority_judgment_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
 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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class MajorityJudjmentVoteService:
    """Class that handles vote procedures for the majority vote case"""

    @staticmethod
    def perform_vote(rating_options: List[dict], poll_id: str) -> MajorityVoteModel:
        """
        Perform a vote on a majority vote poll
        Args:
            poll_id: the id of the poll.
            rating_options: List of dictionaries of ratings assigned to an option [{'poll_choice_id': ..., 'rating': ...}, ...].
        Raises:
            PollDoesNotExistException: execption raised when the poll selected is not present in the database
            PollOptionRatingUnvalidException: exception raised when there are no rating choices in the options (None)
            MajorityNumberOfRatingsNotValid: exception raised when the rating number is not between 1 and 5
        Returns:
            vote: the majority vote model created.
        """

        # check if poll exists
        try:
            poll: PollModel = PollModel.objects.get(id=poll_id)
            # todo: add here "is open" filter
        except ObjectDoesNotExist:
            raise PollDoesNotExistException(f"Error: Poll with id={poll_id} does not exist")

        # check if every option has a value assigned

        num_poll_options: int = PollOptionModel.objects.filter(poll_fk=poll_id).count()
        if num_poll_options != len(rating_options):
            raise PollOptionRatingUnvalidException(f"Error: the poll option doesn't have a rating assigned")

        # check if rating assigned is a number from 1 to 5
        for num_ratings in rating_options:
            rating_value = num_ratings.get('rating')
            if rating_value < 1 or rating_value > 5:
                raise MajorityNumberOfRatingsNotValid(f"Error: the poll option has an invalid rating assigned")

        # todo: add a check if user alredy voted this

        # create majority vote object
        vote: MajorityVoteModel = MajorityVoteModel()
        vote.poll = poll
        vote.save()

        for num_ratings in rating_options:
            rating_key = num_ratings.get('poll_choice_id')
            rating_value = num_ratings.get('rating')

            temp_majority_option: MajorityJudgmentModel = MajorityJudgmentModel()
            temp_majority_option.poll_option = PollOptionModel.objects.filter(poll_fk=poll_id).get(id=rating_key)
            temp_majority_option.rating = rating_value
            temp_majority_option.majority_poll_vote = vote
            temp_majority_option.save()

        return vote

    @staticmethod
    def calculate_result(poll_id: str, user = None) -> IMajorityJudgmentResults:
        """Calculate result of a majority poll.

        Args:
            poll_id: The id of the poll.

        Raises:
            PollDoesNotExistException: If the poll does not exist.
            ResultsNotAvailableException: you tried to access results on a poll that are not available        
        Returns: 
            List[MajorityPollResultData]: List of calculated result for each option.
        """

        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")

        # results = NoParityMJResults(poll)
        results = ParityMJResults(poll)
        results.calculate()

        return results

    @staticmethod
    def get_vote_by_id(vote_id: str) -> MajorityVoteModel:
        """
        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:
            MajorityVoteModel: the majority vote model with the specified id
        """

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

calculate_result(poll_id, user=None) staticmethod

Calculate result of a majority poll.

Parameters:

Name Type Description Default
poll_id str

The id of the poll.

required

Raises:

Type Description
PollDoesNotExistException

If the poll does not exist.

ResultsNotAvailableException

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

Returns:

Type Description
IMajorityJudgmentResults

List[MajorityPollResultData]: List of calculated result for each option.

Source code in apps/votes_results/services/majority_judgment_vote_service.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@staticmethod
def calculate_result(poll_id: str, user = None) -> IMajorityJudgmentResults:
    """Calculate result of a majority poll.

    Args:
        poll_id: The id of the poll.

    Raises:
        PollDoesNotExistException: If the poll does not exist.
        ResultsNotAvailableException: you tried to access results on a poll that are not available        
    Returns: 
        List[MajorityPollResultData]: List of calculated result for each option.
    """

    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")

    # results = NoParityMJResults(poll)
    results = ParityMJResults(poll)
    results.calculate()

    return results

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
MajorityVoteModel MajorityVoteModel

the majority vote model with the specified id

Source code in apps/votes_results/services/majority_judgment_vote_service.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@staticmethod
def get_vote_by_id(vote_id: str) -> MajorityVoteModel:
    """
    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:
        MajorityVoteModel: the majority vote model with the specified id
    """

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

perform_vote(rating_options, poll_id) staticmethod

Perform a vote on a majority vote poll

Parameters:

Name Type Description Default
poll_id str

the id of the poll.

required
rating_options List[dict]

List of dictionaries of ratings assigned to an option [{'poll_choice_id': ..., 'rating': ...}, ...].

required

Raises:

Type Description
PollDoesNotExistException

execption raised when the poll selected is not present in the database

PollOptionRatingUnvalidException

exception raised when there are no rating choices in the options (None)

MajorityNumberOfRatingsNotValid

exception raised when the rating number is not between 1 and 5

Returns:

Name Type Description
vote MajorityVoteModel

the majority vote model created.

Source code in apps/votes_results/services/majority_judgment_vote_service.py
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
@staticmethod
def perform_vote(rating_options: List[dict], poll_id: str) -> MajorityVoteModel:
    """
    Perform a vote on a majority vote poll
    Args:
        poll_id: the id of the poll.
        rating_options: List of dictionaries of ratings assigned to an option [{'poll_choice_id': ..., 'rating': ...}, ...].
    Raises:
        PollDoesNotExistException: execption raised when the poll selected is not present in the database
        PollOptionRatingUnvalidException: exception raised when there are no rating choices in the options (None)
        MajorityNumberOfRatingsNotValid: exception raised when the rating number is not between 1 and 5
    Returns:
        vote: the majority vote model created.
    """

    # check if poll exists
    try:
        poll: PollModel = PollModel.objects.get(id=poll_id)
        # todo: add here "is open" filter
    except ObjectDoesNotExist:
        raise PollDoesNotExistException(f"Error: Poll with id={poll_id} does not exist")

    # check if every option has a value assigned

    num_poll_options: int = PollOptionModel.objects.filter(poll_fk=poll_id).count()
    if num_poll_options != len(rating_options):
        raise PollOptionRatingUnvalidException(f"Error: the poll option doesn't have a rating assigned")

    # check if rating assigned is a number from 1 to 5
    for num_ratings in rating_options:
        rating_value = num_ratings.get('rating')
        if rating_value < 1 or rating_value > 5:
            raise MajorityNumberOfRatingsNotValid(f"Error: the poll option has an invalid rating assigned")

    # todo: add a check if user alredy voted this

    # create majority vote object
    vote: MajorityVoteModel = MajorityVoteModel()
    vote.poll = poll
    vote.save()

    for num_ratings in rating_options:
        rating_key = num_ratings.get('poll_choice_id')
        rating_value = num_ratings.get('rating')

        temp_majority_option: MajorityJudgmentModel = MajorityJudgmentModel()
        temp_majority_option.poll_option = PollOptionModel.objects.filter(poll_fk=poll_id).get(id=rating_key)
        temp_majority_option.rating = rating_value
        temp_majority_option.majority_poll_vote = vote
        temp_majority_option.save()

    return vote