Skip to content

Shor ID Util

Class used to generate and validate a short id url for a poll.

ShortIdUtil

Source code in apps/polls_management/classes/poll_form_utils/short_id_util.py
 7
 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
class ShortIdUtil:

    @staticmethod
    def generate() -> str:
        """Generate a short id for a poll that is unique in the db
            Returns:
                str: Unique short id.
        """
        short_ids: List[str] = list(PollModel.objects.values_list('short_id', flat=True))
        new_id: str = ''.join(random.choice(string.ascii_lowercase) for i in range(6))

        limit: int = 100
        while new_id in short_ids and limit > 0:
            new_id = ''.join(random.choice(string.ascii_lowercase) for i in range(6))
            limit -= 1

        if limit == 0:
            raise Exception("Error generating short id")

        return new_id

    @staticmethod
    def validate(short_id: str) -> bool:
        """Validate the short id.

        Args:
            short_id (str): Short id to validate.

        Returns:
            bool: True if the short id is valid, False otherwise.
        """
        short_ids: List[str] = list(PollModel.objects.values_list('short_id', flat=True))
        if len(short_id) == 6 and short_id not in short_ids:
            return True
        else:
            return False

    def __init__(self, code: str, poll: PollModel=None) -> None:
        self.code: str = code or ""
        self.poll: PollModel = poll

    def validate_length(self) -> bool:
        return len(self.code) >= 6 and len(self.code) <= 60

    def validate_characters(self) -> bool:
        return self.code.isalnum()

    def validate_uniqness(self) -> bool:

        query = PollModel.objects.filter(short_id=self.code)

        # if I am working on an already existent poll is acceptable
        # this code is already used for this poll
        if not self.poll is None:
            query = query.filter(~Q(id__in=[self.poll.id]))

        return query.count() == 0

generate() staticmethod

Generate a short id for a poll that is unique in the db

Returns:

Name Type Description
str str

Unique short id.

Source code in apps/polls_management/classes/poll_form_utils/short_id_util.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@staticmethod
def generate() -> str:
    """Generate a short id for a poll that is unique in the db
        Returns:
            str: Unique short id.
    """
    short_ids: List[str] = list(PollModel.objects.values_list('short_id', flat=True))
    new_id: str = ''.join(random.choice(string.ascii_lowercase) for i in range(6))

    limit: int = 100
    while new_id in short_ids and limit > 0:
        new_id = ''.join(random.choice(string.ascii_lowercase) for i in range(6))
        limit -= 1

    if limit == 0:
        raise Exception("Error generating short id")

    return new_id

validate(short_id) staticmethod

Validate the short id.

Parameters:

Name Type Description Default
short_id str

Short id to validate.

required

Returns:

Name Type Description
bool bool

True if the short id is valid, False otherwise.

Source code in apps/polls_management/classes/poll_form_utils/short_id_util.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@staticmethod
def validate(short_id: str) -> bool:
    """Validate the short id.

    Args:
        short_id (str): Short id to validate.

    Returns:
        bool: True if the short id is valid, False otherwise.
    """
    short_ids: List[str] = list(PollModel.objects.values_list('short_id', flat=True))
    if len(short_id) == 6 and short_id not in short_ids:
        return True
    else:
        return False