class ValidationError Exception pass class ModelManager object def __i

 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
class ValidationError(Exception):
pass
class ModelManager(object):
def __init__(self, model, client):
self._model = model
self._client = client
class ModelMeta(type):
def __init__(cls, name, bases, attrs):
new_cls = super(ModelMeta, cls).__init__(cls, name, bases, attrs)
manager = getattr(cls, '__manager__', None) or ModelManager
cls.objects = manager(cls)
return new_cls
class ModelBase(object):
def __init__(self, **kwargs):
for field_name, value in kwargs.items():
setattr(self, field_name, value)
def validate(self, field_name, value):
return isinstance(value, type(getattr(self, field_name, None)))
def __setattr__(self, field_name, value):
if not self.validate(field_name, value):
raise ValidationError, 'Field "%s" is not instance of %s\'s' % \
(field_name, type(value))
super(ModelBase, self).__setattr__(field_name, value)
def push(self):
return
class Model(ModelBase):
__metaclass__ = ModelMeta
class User(Model):
id = int()
class Friend(Model):
__act__ = ('friends', 'friends_mutal', 'friends_online', 'friends_new')
id = int()
name = unicode()
photo = unicode()
online = bool()
class Photo(Model):
owner_photo = unicode()
thumb = unicode()
image = unicode()
class Message(Model):
message_id = int()
creation_time = unicode()
content = unicode()
sender = User()
recipient = User()
read = bool()