-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
51 lines (35 loc) · 1.23 KB
/
models.py
File metadata and controls
51 lines (35 loc) · 1.23 KB
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
import re
import uuid
import datetime
from schematics.models import Model
from schematics.types import StringType, DateTimeType
from schematics.exceptions import ModelValidationError
from schematics.types import BaseType
import phonenumbers
from phonenumbers import NumberParseException
# Use phonenumbers library
class InvalidUKMobileNumberException(Exception):
pass
class UKMobileNumberType(BaseType):
MESSAGES = {
'number': 'Value must be a valid UK mobile number.',
}
def to_primitive(self, value):
return value
def validate_number(self, value):
if not isinstance(value, basestring):
raise InvalidUKMobileNumberException('Number must be unicode or str to parse.')
try:
number = phonenumbers.parse(value, None)
except NumberParseException:
raise InvalidUKMobileNumberException(self.messages['number'])
class Recipient(Model):
number = UKMobileNumberType(required=True)
created_at = DateTimeType(default=datetime.datetime.now)
class RecipientStore(object):
def __init__(self):
self._store = {}
def add(self, recipient):
key = str(uuid.uuid4())
self._store[key] = recipient
return key