File: validators.py

package info (click to toggle)
python-django-extensions 4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,812 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (124 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download | duplicates (2)
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
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
# -*- coding: utf-8 -*-
import unicodedata
import binascii

from django.core.exceptions import ValidationError
from django.utils.deconstruct import deconstructible
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _


@deconstructible
class NoControlCharactersValidator:
    message = _("Control Characters like new lines or tabs are not allowed.")
    code = "no_control_characters"
    whitelist = None

    def __init__(self, message=None, code=None, whitelist=None):
        if message:
            self.message = message
        if code:
            self.code = code
        if whitelist:
            self.whitelist = whitelist

    def __call__(self, value):
        value = force_str(value)
        whitelist = self.whitelist
        category = unicodedata.category
        for character in value:
            if whitelist and character in whitelist:
                continue
            if category(character)[0] == "C":
                params = {"value": value, "whitelist": whitelist}
                raise ValidationError(self.message, code=self.code, params=params)

    def __eq__(self, other):
        return (
            isinstance(other, NoControlCharactersValidator)
            and (self.whitelist == other.whitelist)
            and (self.message == other.message)
            and (self.code == other.code)
        )


@deconstructible
class NoWhitespaceValidator:
    message = _("Leading and Trailing whitespaces are not allowed.")
    code = "no_whitespace"

    def __init__(self, message=None, code=None, whitelist=None):
        if message:
            self.message = message
        if code:
            self.code = code

    def __call__(self, value):
        value = force_str(value)
        if value != value.strip():
            params = {"value": value}
            raise ValidationError(self.message, code=self.code, params=params)

    def __eq__(self, other):
        return (
            isinstance(other, NoWhitespaceValidator)
            and (self.message == other.message)
            and (self.code == other.code)
        )


@deconstructible
class HexValidator:
    messages = {
        "invalid": _("Only a hex string is allowed."),
        "length": _("Invalid length. Must be %(length)d characters."),
        "min_length": _("Ensure that there are more than %(min)s characters."),
        "max_length": _("Ensure that there are no more than %(max)s characters."),
    }
    code = "hex_only"

    def __init__(
        self, length=None, min_length=None, max_length=None, message=None, code=None
    ):
        self.length = length
        self.min_length = min_length
        self.max_length = max_length
        if message:
            self.message = message
        else:
            self.message = self.messages["invalid"]
        if code:
            self.code = code

    def __call__(self, value):
        value = force_str(value)
        if self.length and len(value) != self.length:
            raise ValidationError(
                self.messages["length"],
                code="hex_only_length",
                params={"length": self.length},
            )
        if self.min_length and len(value) < self.min_length:
            raise ValidationError(
                self.messages["min_length"],
                code="hex_only_min_length",
                params={"min": self.min_length},
            )
        if self.max_length and len(value) > self.max_length:
            raise ValidationError(
                self.messages["max_length"],
                code="hex_only_max_length",
                params={"max": self.max_length},
            )

        try:
            binascii.unhexlify(value)
        except (TypeError, binascii.Error):
            raise ValidationError(self.messages["invalid"], code="hex_only")

    def __eq__(self, other):
        return (
            isinstance(other, HexValidator)
            and (self.message == other.message)
            and (self.code == other.code)
        )