Skip to content

Schulze Method Services

Schulze Method services related to vote.

SchulzeMethodVoteService

Class that handles vote procedures for the majority vote case

Source code in apps/votes_results/services/schulze_method_vote_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class SchulzeMethodVoteService:
    """Class that handles vote procedures for the majority vote case"""

    @staticmethod
    def perform_vote(options_rated: List[str], poll_id: str) -> SchulzeVoteModel:
        """
        Perform a vote on a majority vote poll
        Args:
            poll_id: the id of the poll.
            options_rated: List of string of option id [12,14,13,15] 
        Raises:
            PollDoesNotExistException: execption raised when the poll selected is not present in the database
        Returns:
            vote: the schulze vote model created.
        """

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


        # create schulze vote object
        vote: SchulzeVoteModel = SchulzeVoteModel()
        vote.poll = poll
        user_order :List = []
        for opt in options_rated:
            user_order.append(int(opt))
        vote.set_order(user_order)
        vote.save()

        return vote

    @staticmethod
    def calculate_result(poll: PollModel, user = None) -> SchulzeResultsAdapter:
        """
        Calculate result of a poll.
        Args:
            poll: the poll you want to calculate results
        Raises:
            PollDoesNotExistException: you tried to calculate results on a non-existent poll
            ResultsNotAvailableException: raised if you try to check results not visible
        Returns:
            result: list of PollOptions sorted by ranking number as Schulze algorithm results
        """

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

        result: SchulzeResultsAdapter = SchulzeResultsAdapter(poll)
        result.calculate()

        return result

    @staticmethod
    def get_vote_by_id(vote_id: str) -> SchulzeVoteModel:
        """
        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:
            SchulzeVoteModel: schulze vote by the specified id
        """

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

calculate_result(poll, user=None) staticmethod

Calculate result of a poll.

Parameters:

Name Type Description Default
poll PollModel

the poll you want to calculate results

required

Raises:

Type Description
PollDoesNotExistException

you tried to calculate results on a non-existent poll

ResultsNotAvailableException

raised if you try to check results not visible

Returns:

Name Type Description
result SchulzeResultsAdapter

list of PollOptions sorted by ranking number as Schulze algorithm results

Source code in apps/votes_results/services/schulze_method_vote_service.py
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 calculate_result(poll: PollModel, user = None) -> SchulzeResultsAdapter:
    """
    Calculate result of a poll.
    Args:
        poll: the poll you want to calculate results
    Raises:
        PollDoesNotExistException: you tried to calculate results on a non-existent poll
        ResultsNotAvailableException: raised if you try to check results not visible
    Returns:
        result: list of PollOptions sorted by ranking number as Schulze algorithm results
    """

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

    result: SchulzeResultsAdapter = SchulzeResultsAdapter(poll)
    result.calculate()

    return result

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

schulze vote by the specified id

Source code in apps/votes_results/services/schulze_method_vote_service.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@staticmethod
def get_vote_by_id(vote_id: str) -> SchulzeVoteModel:
    """
    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:
        SchulzeVoteModel: schulze vote by the specified id
    """

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

perform_vote(options_rated, 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
options_rated List[str]

List of string of option id [12,14,13,15]

required

Raises:

Type Description
PollDoesNotExistException

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

Returns:

Name Type Description
vote SchulzeVoteModel

the schulze vote model created.

Source code in apps/votes_results/services/schulze_method_vote_service.py
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
@staticmethod
def perform_vote(options_rated: List[str], poll_id: str) -> SchulzeVoteModel:
    """
    Perform a vote on a majority vote poll
    Args:
        poll_id: the id of the poll.
        options_rated: List of string of option id [12,14,13,15] 
    Raises:
        PollDoesNotExistException: execption raised when the poll selected is not present in the database
    Returns:
        vote: the schulze vote model created.
    """

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


    # create schulze vote object
    vote: SchulzeVoteModel = SchulzeVoteModel()
    vote.poll = poll
    user_order :List = []
    for opt in options_rated:
        user_order.append(int(opt))
    vote.set_order(user_order)
    vote.save()

    return vote