Parent: [0f153c] (diff)

Child: [d7b818] (diff)

Download this file

session.py    131 lines (109 with data), 4.2 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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pymongo
from base import Cursor, Object
class Session(object):
_registry = {}
_datastores = {}
def __init__(self, bind=None):
self.bind = bind
@classmethod
def by_name(cls, name):
if name in cls._registry:
result = cls._registry[name]
else:
result = cls._registry[name] = cls(cls._datastores.get(name))
return result
def _impl(self, cls):
try:
return self.bind.db[cls.__mongometa__.name]
except TypeError:
return None
def get(self, cls, **kwargs):
bson = self._impl(cls).find_one(kwargs)
if bson is None: return None
return cls.make(bson)
def find(self, cls, *args, **kwargs):
cursor = self._impl(cls).find(*args, **kwargs)
return Cursor(cls, cursor)
def remove(self, cls, *args, **kwargs):
self._impl(cls).remove(*args, **kwargs)
def find_by(self, cls, **kwargs):
return self.find(cls, kwargs)
def count(self, cls):
return self._impl(cls).count()
def ensure_index(self, cls, fields):
if not isinstance(fields, (list, tuple)):
fields = [ fields ]
index_fields = [(f, pymongo.ASCENDING) for f in fields]
return self._impl(cls).ensure_index(index_fields)
def ensure_indexes(self, cls):
for idx in getattr(cls.__mongometa__, 'indexes', []):
self.ensure_index(cls, idx)
def group(self, cls, *args, **kwargs):
return self._impl(cls).group(*args, **kwargs)
def update_partial(self, cls, spec, fields, upsert):
return self._impl(cls).update(spec, fields, upsert, safe=True)
def save(self, doc, *args):
doc.make_safe()
if doc.__mongometa__.schema is not None:
data = doc.__mongometa__.schema.validate(doc)
else:
data = dict(doc)
doc.update(data)
if args:
values = dict((arg, data[arg]) for arg in args)
result = self._impl(doc).update(dict(_id=doc._id), {'$set':values})
else:
result = self._impl(doc).save(data, safe=True)
if result:
doc._id = result
def insert(self, doc):
doc.make_safe()
if doc.__mongometa__.schema is not None:
data = doc.__mongometa__.schema.validate(doc)
else:
data = dict(doc)
doc.update(data)
bson = self._impl(doc).insert(data, safe=True)
if isinstance(bson, pymongo.objectid.ObjectId):
doc.update(_id=bson)
def update(self, doc, spec, upsert=False):
doc.make_safe()
if doc.__mongometa__.schema is not None:
data = doc.__mongometa__.schema.validate(doc)
else:
data = dict(doc)
doc.update(data)
self._impl(doc).update(doc, spec, upsert, safe=True)
def delete(self, doc):
self._impl(doc).remove({'_id':doc._id})
def set(self, doc, fields_values):
"""
sets a key/value pairs, and persists those changes to the datastore immediately
"""
fields_values = Object.from_bson(fields_values)
fields_values.make_safe()
doc.update(fields_values)
impl = self._impl(doc)
impl.update({'_id':doc._id}, {'$set':fields_values})
def increase_field(self, doc, **kwargs):
"""
usage: increase_field(key=value)
Sets a field to value, only if value is greater than the current value
Does not change it locally
"""
key = kwargs.keys()[0]
value = kwargs[key]
if value is None:
raise ValueError, "%s=%s" % (key, value)
if key not in doc:
self._impl(doc).update(
{'_id': doc._id, key: None},
{'$set': {key: value}}
)
self._impl(doc).update(
{'_id': doc._id, key: {'$lt': value}},
# failed attempt at doing it all in one operation
#{'$where': "this._id == '%s' && (!(%s in this) || this.%s < '%s')"
# % (doc._id.url_encode(), key, key, value)},
{'$set': {key: value}}
)