forked from madisvain/vatcomply
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
68 lines (51 loc) · 1.95 KB
/
models.py
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
from datetime import date
from typing import Optional
from passlib.hash import pbkdf2_sha256
from pydantic import BaseModel, ValidationError, validator, root_validator, EmailStr, SecretStr
from pydantic.dataclasses import dataclass
from db import database, Users
from settings import SYMBOLS
class LoginValidationModel(BaseModel):
email: EmailStr
password: SecretStr
@root_validator
def password_length(cls, values):
email, password = values.get("email"), values.get("password")
user = database.fetch_one(query=Users.select().where(Users.c.email == email))
if not user or pbkdf2_sha256.verify(password.get_secret_value(), user.password):
raise ValueError(
"Please enter a correct username and password. Note that both fields may be case-sensitive."
)
return values
class RatesQueryValidationModel(BaseModel):
base: str = "EUR"
date: Optional[date]
symbols: Optional[list]
@validator("base")
def base_validation(cls, base):
if base not in list(SYMBOLS):
raise ValueError(f"Base currency {base} is not supported.")
return base
@validator("symbols", pre=True, whole=True)
def symbols_validation(cls, symbols):
symbols = symbols.split(",")
diff = list(set(symbols) - set(list(SYMBOLS)))
if diff:
raise ValueError(f"Symbols {', '.join(diff)} are not supported.")
return symbols
class RegistrationValidationModel(BaseModel):
email: EmailStr
password: SecretStr
@validator("password")
def password_length(cls, v):
if len(v.get_secret_value()) < 6:
raise ValueError("Password must be at least 6 characters long.")
return v
class VATValidationModel(BaseModel):
vat_number: str
@validator("vat_number")
def format_validation(cls, v):
# TODO: implement Regex validators
return v
class VatRatesModel(BaseModel):
vat_nu: str