|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
# ############################################################################# |
|
3
|
|
|
# |
|
4
|
|
|
# The MIT License (MIT) |
|
5
|
|
|
# |
|
6
|
|
|
# Copyright (c) 2016 Trocafone |
|
7
|
|
|
# |
|
8
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
# in the Software without restriction, including without limitation the rights |
|
11
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
# furnished to do so, subject to the following conditions: |
|
14
|
|
|
# |
|
15
|
|
|
# The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
# all copies or substantial portions of the Software. |
|
17
|
|
|
# |
|
18
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
24
|
|
|
# SOFTWARE. |
|
25
|
|
|
# |
|
26
|
|
|
############################################################################### |
|
27
|
|
|
|
|
28
|
1 |
|
from voluptuous import Invalid |
|
29
|
1 |
|
from brazilnum.util import clean_id |
|
|
|
|
|
|
30
|
1 |
|
from brazilnum.cnpj import validate_cnpj |
|
|
|
|
|
|
31
|
1 |
|
from brazilnum.cpf import validate_cpf |
|
|
|
|
|
|
32
|
1 |
|
from brazilnum.cep import format_cep |
|
|
|
|
|
|
33
|
1 |
|
import datetime |
|
34
|
1 |
|
import re |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
1 |
|
def CNPJ(value): |
|
|
|
|
|
|
38
|
1 |
|
if not validate_cnpj(value): |
|
39
|
1 |
|
raise Invalid("Invalid CNPJ") |
|
40
|
1 |
|
return clean_id(value) |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
1 |
|
def CPF(value): |
|
|
|
|
|
|
44
|
1 |
|
if not validate_cpf(value): |
|
45
|
1 |
|
raise Invalid("Invalid CPF") |
|
46
|
1 |
|
return clean_id(value) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
1 |
|
def CEP(value): |
|
|
|
|
|
|
50
|
1 |
|
try: |
|
51
|
1 |
|
format_cep(value) |
|
52
|
1 |
|
except ValueError as e: |
|
53
|
1 |
|
raise Invalid(e) |
|
54
|
|
|
|
|
55
|
1 |
|
return clean_id(value) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
1 |
|
def Email(value): |
|
|
|
|
|
|
59
|
1 |
|
if not re.match("[^@]+@[^@]+\.[^@]+", value): |
|
|
|
|
|
|
60
|
1 |
|
raise Invalid("This email is invalid.") |
|
61
|
1 |
|
return value |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
1 |
|
def Date(value): |
|
|
|
|
|
|
65
|
1 |
|
fmt = '%d/%m/%Y' |
|
66
|
1 |
|
try: |
|
67
|
1 |
|
if isinstance(value, int): |
|
68
|
1 |
|
value = datetime.date.today() + datetime.timedelta(value) |
|
69
|
|
|
|
|
70
|
1 |
|
return value.strftime(fmt) |
|
71
|
1 |
|
except Exception: |
|
72
|
1 |
|
raise Invalid("Should be an instance of datetime.datetime") |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
1 |
|
def Price(value): |
|
|
|
|
|
|
76
|
1 |
|
if not (type(value) == int or type(value) == float): |
|
77
|
1 |
|
raise Invalid("This price is not and integer or a float.") |
|
78
|
1 |
|
value = float(value) |
|
79
|
|
|
return "%.2f" % value |
|
80
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.