-
Notifications
You must be signed in to change notification settings - Fork 19
/
base.py
50 lines (39 loc) · 1.2 KB
/
base.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
#!/usr/bin/env python
#coding:utf8
import mongoengine
class BaseMongoStorage(object):
'''adapter for MongoDB'''
def save(self):
'''save obj to db'''
mongoengine.Document.save(self)
def update(self, **kwargs):
'''update obj by kwargs
update(var1=1)
update(var1=1, var2=2)'''
new_kwargs = {}
for key in kwargs:
new_kwargs['set__' + key] = kwargs[key]
mongoengine.Document.update(self, **new_kwargs)
def delete(self):
'''delete obj from db'''
mongoengine.Document.delete(self)
@classmethod
def get(cls, **kwargs):
'''get obj list from db'''
start = kwargs.pop('start', None)
end = kwargs.pop('end', None)
key = kwargs.pop('key', None)
if key:
kwargs['pk'] = key
return [obj for obj in cls.objects(**kwargs)[start:end]]
@classmethod
def status(cls):
'''get status of this storage'''
return {'count': cls.objects.count()}
@property
def key(self):
return str(self.pk)
def __setitem__(self, key, value):
setattr(self, key, value)
def __getitem__(self, key):
return getattr(self, key)