~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/user/screen_name_validator.py -rw-r--r-- 1.2 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
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
"""
byceps.services.user.screen_name_validator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Validate screen names regarding their contained characters

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from itertools import chain
from string import ascii_letters, digits

MIN_LENGTH = 3
MAX_LENGTH = 24

GERMAN_CHARS = 'äöüß'
SPECIAL_CHARS = '!$*-./<=>[]_'

# The at sign (`@`) is not allowed in the screen name because it is
# used to distinguish a screen name from an e-mail address on login.

VALID_CHARS = frozenset(
    chain(ascii_letters, digits, GERMAN_CHARS, SPECIAL_CHARS)
)


def is_screen_name_valid(screen_name: str) -> bool:
    """Return `True` if the screen name has a valid length and contains
    only permitted characters.
    """
    return is_length_valid(screen_name) and contains_only_valid_chars(
        screen_name
    )


def is_length_valid(screen_name: str) -> bool:
    """Return `True` if the screen name has a valid length."""
    return MIN_LENGTH <= len(screen_name) <= MAX_LENGTH


def contains_only_valid_chars(screen_name: str) -> bool:
    """Return `True` if the screen name contains only permitted characters."""
    return all(map(VALID_CHARS.__contains__, screen_name))