Skip to content

Poll Token Service

Services related to tokens generations and management.

PollTokenService

Service class for poll tokens management

Source code in apps/polls_management/services/poll_token_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
 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class PollTokenService:

    """Service class for poll tokens management"""

    @staticmethod
    def create_tokens(token_number: int, poll: PollModel) -> List[PollTokens]:

        """Create tokens for the poll and store them in the database.
        Args:
            token_number: number of tokens to create.
            poll: poll to assign tokens created.
        Returns:
            tokens: list of tokens created.
        """

        tokens: List[PollTokens] = []

        # creation of a token link for as many times as dictated
        for x in range(token_number):

            # creation of a phantom user to assign a token and database integrity control
            # (if a user with the same name already exists)
            unique_id = get_random_string(length=8)
            while (User.objects.filter(username=unique_id).exists()):
                unique_id = get_random_string(length=8)

            phantomuser: User = User.objects.create_user(username=unique_id)

            # creation of database table for new token
            new_token: PollTokens = PollTokens(token_user=phantomuser, poll_fk=poll)
            new_token.save()

            tokens.append(new_token)

        return tokens

    @staticmethod
    def get_poll_token_by_user(user: User) -> PollTokens:

        """Get a poll token by its user.
        Args:
            user: user whom the token is assigned to.
        Raises:
            TokenDoesNotExistException: raised when you retrieve a non-existent token.
        Returns:
            poll_token: token associated with the user.
        """

        try:
            poll_token: PollTokens = PollTokens.objects.get(token_user=user)
        except ObjectDoesNotExist:
            raise TokenDoesNotExistException(f"Error: token with user={user} does not exit")    

        return poll_token

    @staticmethod
    def is_single_option_token_used(token: PollTokens) -> bool:

        """Check if a token is used for a single option poll.
        Args:
            token: token model with all necessary information.
        Returns:
            Bool indicating if token is used for a single option poll or not.
        """

        if token.single_option_use:
            return True
        else:
            return False

    @staticmethod
    def is_majority_token_used(token: PollTokens) -> bool:

        """Check if a token is used for a majority poll.
        Args:
            token: token model with all necessary information.
        Returns:
            Bool indicating if token is used for a majority judgment poll or not.
        """

        if token.majority_use:
            return True
        else:
            return False

    @staticmethod
    def check_single_option(token: PollTokens):

        """Make token for single option as already used.
        Args:
            token: token model with all necessary information.
        """

        token.single_option_use = True
        token.save()

    @staticmethod
    def check_majority_option(token: PollTokens):

        """Make token for majority option as already used.
        Args:
            token: token model with all necessary information.
        """

        token.majority_use = True
        token.save()

    @staticmethod
    def available_token_list(poll: PollModel) -> List[PollTokens]:

        """Return a list of available tokens.
        Args:
            poll: the poll the tokens belong to.
        Returns:
            List of available token links.
        """

        return PollTokens.objects.filter(
                Q(poll_fk=poll) & Q(single_option_use=False) 
                & Q(majority_use=False)
            ).order_by('-created_at').all()

    def delete_tokens(poll: PollModel):

        """Delete tokens and related phantom users for the specified list.
        Args:
            poll: the poll the tokens belong to.
        """

        # get all tokens for the specified poll, be them available or not
        tokens: PollTokens = PollTokens.objects.filter(poll_fk=poll)

        # if there are tokens for the poll, delete the phantom users and then their tokens
        if tokens:
            for token in tokens:
                phantouser: User = token.token_user
                phantouser.delete()
                token.delete()

    @staticmethod
    def unavailable_token_list(poll: PollModel) -> List[PollTokens]:

        """Return a list of unavailable token links.
        Args:
            poll: the poll the tokens belong to.
        Returns:
            List of unavailable token links.
        """

        return PollTokens.objects.filter(Q(poll_fk=poll) & Q(Q(single_option_use=True) | Q(majority_use=True))).all()

    @staticmethod
    def create_google_record(user: User, poll: PollModel) -> PollTokens:

        """Creates an object PollTokens used to record a vote for a Google auth user.
        Args:
            user: the user whose vote has to be recorded.
            poll: the poll the tokens belong to.
        Returns:
            google_vote: the token representing the user's vote with Google email.
        """

        google_vote: PollTokens = PollTokens(token_user=user, poll_fk=poll)
        google_vote.save()

        return google_vote

available_token_list(poll) staticmethod

Return a list of available tokens.

Parameters:

Name Type Description Default
poll PollModel

the poll the tokens belong to.

required

Returns:

Type Description
List[PollTokens]

List of available token links.

Source code in apps/polls_management/services/poll_token_service.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@staticmethod
def available_token_list(poll: PollModel) -> List[PollTokens]:

    """Return a list of available tokens.
    Args:
        poll: the poll the tokens belong to.
    Returns:
        List of available token links.
    """

    return PollTokens.objects.filter(
            Q(poll_fk=poll) & Q(single_option_use=False) 
            & Q(majority_use=False)
        ).order_by('-created_at').all()

check_majority_option(token) staticmethod

Make token for majority option as already used.

Parameters:

Name Type Description Default
token PollTokens

token model with all necessary information.

required
Source code in apps/polls_management/services/poll_token_service.py
109
110
111
112
113
114
115
116
117
118
@staticmethod
def check_majority_option(token: PollTokens):

    """Make token for majority option as already used.
    Args:
        token: token model with all necessary information.
    """

    token.majority_use = True
    token.save()

check_single_option(token) staticmethod

Make token for single option as already used.

Parameters:

Name Type Description Default
token PollTokens

token model with all necessary information.

required
Source code in apps/polls_management/services/poll_token_service.py
 98
 99
100
101
102
103
104
105
106
107
@staticmethod
def check_single_option(token: PollTokens):

    """Make token for single option as already used.
    Args:
        token: token model with all necessary information.
    """

    token.single_option_use = True
    token.save()

create_google_record(user, poll) staticmethod

Creates an object PollTokens used to record a vote for a Google auth user.

Parameters:

Name Type Description Default
user User

the user whose vote has to be recorded.

required
poll PollModel

the poll the tokens belong to.

required

Returns:

Name Type Description
google_vote PollTokens

the token representing the user's vote with Google email.

Source code in apps/polls_management/services/poll_token_service.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
@staticmethod
def create_google_record(user: User, poll: PollModel) -> PollTokens:

    """Creates an object PollTokens used to record a vote for a Google auth user.
    Args:
        user: the user whose vote has to be recorded.
        poll: the poll the tokens belong to.
    Returns:
        google_vote: the token representing the user's vote with Google email.
    """

    google_vote: PollTokens = PollTokens(token_user=user, poll_fk=poll)
    google_vote.save()

    return google_vote

create_tokens(token_number, poll) staticmethod

Create tokens for the poll and store them in the database.

Parameters:

Name Type Description Default
token_number int

number of tokens to create.

required
poll PollModel

poll to assign tokens created.

required

Returns:

Name Type Description
tokens List[PollTokens]

list of tokens created.

Source code in apps/polls_management/services/poll_token_service.py
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
@staticmethod
def create_tokens(token_number: int, poll: PollModel) -> List[PollTokens]:

    """Create tokens for the poll and store them in the database.
    Args:
        token_number: number of tokens to create.
        poll: poll to assign tokens created.
    Returns:
        tokens: list of tokens created.
    """

    tokens: List[PollTokens] = []

    # creation of a token link for as many times as dictated
    for x in range(token_number):

        # creation of a phantom user to assign a token and database integrity control
        # (if a user with the same name already exists)
        unique_id = get_random_string(length=8)
        while (User.objects.filter(username=unique_id).exists()):
            unique_id = get_random_string(length=8)

        phantomuser: User = User.objects.create_user(username=unique_id)

        # creation of database table for new token
        new_token: PollTokens = PollTokens(token_user=phantomuser, poll_fk=poll)
        new_token.save()

        tokens.append(new_token)

    return tokens

delete_tokens(poll)

Delete tokens and related phantom users for the specified list.

Parameters:

Name Type Description Default
poll PollModel

the poll the tokens belong to.

required
Source code in apps/polls_management/services/poll_token_service.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def delete_tokens(poll: PollModel):

    """Delete tokens and related phantom users for the specified list.
    Args:
        poll: the poll the tokens belong to.
    """

    # get all tokens for the specified poll, be them available or not
    tokens: PollTokens = PollTokens.objects.filter(poll_fk=poll)

    # if there are tokens for the poll, delete the phantom users and then their tokens
    if tokens:
        for token in tokens:
            phantouser: User = token.token_user
            phantouser.delete()
            token.delete()

get_poll_token_by_user(user) staticmethod

Get a poll token by its user.

Parameters:

Name Type Description Default
user User

user whom the token is assigned to.

required

Raises:

Type Description
TokenDoesNotExistException

raised when you retrieve a non-existent token.

Returns:

Name Type Description
poll_token PollTokens

token associated with the user.

Source code in apps/polls_management/services/poll_token_service.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@staticmethod
def get_poll_token_by_user(user: User) -> PollTokens:

    """Get a poll token by its user.
    Args:
        user: user whom the token is assigned to.
    Raises:
        TokenDoesNotExistException: raised when you retrieve a non-existent token.
    Returns:
        poll_token: token associated with the user.
    """

    try:
        poll_token: PollTokens = PollTokens.objects.get(token_user=user)
    except ObjectDoesNotExist:
        raise TokenDoesNotExistException(f"Error: token with user={user} does not exit")    

    return poll_token

is_majority_token_used(token) staticmethod

Check if a token is used for a majority poll.

Parameters:

Name Type Description Default
token PollTokens

token model with all necessary information.

required

Returns:

Type Description
bool

Bool indicating if token is used for a majority judgment poll or not.

Source code in apps/polls_management/services/poll_token_service.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@staticmethod
def is_majority_token_used(token: PollTokens) -> bool:

    """Check if a token is used for a majority poll.
    Args:
        token: token model with all necessary information.
    Returns:
        Bool indicating if token is used for a majority judgment poll or not.
    """

    if token.majority_use:
        return True
    else:
        return False

is_single_option_token_used(token) staticmethod

Check if a token is used for a single option poll.

Parameters:

Name Type Description Default
token PollTokens

token model with all necessary information.

required

Returns:

Type Description
bool

Bool indicating if token is used for a single option poll or not.

Source code in apps/polls_management/services/poll_token_service.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@staticmethod
def is_single_option_token_used(token: PollTokens) -> bool:

    """Check if a token is used for a single option poll.
    Args:
        token: token model with all necessary information.
    Returns:
        Bool indicating if token is used for a single option poll or not.
    """

    if token.single_option_use:
        return True
    else:
        return False

unavailable_token_list(poll) staticmethod

Return a list of unavailable token links.

Parameters:

Name Type Description Default
poll PollModel

the poll the tokens belong to.

required

Returns:

Type Description
List[PollTokens]

List of unavailable token links.

Source code in apps/polls_management/services/poll_token_service.py
152
153
154
155
156
157
158
159
160
161
162
@staticmethod
def unavailable_token_list(poll: PollModel) -> List[PollTokens]:

    """Return a list of unavailable token links.
    Args:
        poll: the poll the tokens belong to.
    Returns:
        List of unavailable token links.
    """

    return PollTokens.objects.filter(Q(poll_fk=poll) & Q(Q(single_option_use=True) | Q(majority_use=True))).all()