|
a/Ming/ming/session.py |
|
b/Ming/ming/session.py |
|
... |
|
... |
31 |
def find(self, cls, *args, **kwargs):
|
31 |
def find(self, cls, *args, **kwargs):
|
32 |
cursor = self._impl(cls).find(*args, **kwargs)
|
32 |
cursor = self._impl(cls).find(*args, **kwargs)
|
33 |
return Cursor(cls, cursor)
|
33 |
return Cursor(cls, cursor)
|
34 |
|
34 |
|
35 |
def remove(self, cls, *args, **kwargs):
|
35 |
def remove(self, cls, *args, **kwargs):
|
|
|
36 |
if 'safe' not in kwargs:
|
|
|
37 |
kwargs['safe'] = True
|
36 |
self._impl(cls).remove(*args, **kwargs)
|
38 |
self._impl(cls).remove(*args, **kwargs)
|
37 |
|
39 |
|
38 |
def find_by(self, cls, **kwargs):
|
40 |
def find_by(self, cls, **kwargs):
|
39 |
return self.find(cls, kwargs)
|
41 |
return self.find(cls, kwargs)
|
40 |
|
42 |
|
|
... |
|
... |
66 |
else:
|
68 |
else:
|
67 |
data = dict(doc)
|
69 |
data = dict(doc)
|
68 |
doc.update(data)
|
70 |
doc.update(data)
|
69 |
if args:
|
71 |
if args:
|
70 |
values = dict((arg, data[arg]) for arg in args)
|
72 |
values = dict((arg, data[arg]) for arg in args)
|
71 |
result = self._impl(doc).update(dict(_id=doc._id), {'$set':values})
|
73 |
result = self._impl(doc).update(dict(_id=doc._id), {'$set':values}, safe=True)
|
72 |
else:
|
74 |
else:
|
73 |
result = self._impl(doc).save(data, safe=True)
|
75 |
result = self._impl(doc).save(data, safe=True)
|
74 |
if result:
|
76 |
if result:
|
75 |
doc._id = result
|
77 |
doc._id = result
|
76 |
|
78 |
|
|
... |
|
... |
93 |
data = dict(doc)
|
95 |
data = dict(doc)
|
94 |
doc.update(data)
|
96 |
doc.update(data)
|
95 |
self._impl(doc).update(doc, spec, upsert, safe=True)
|
97 |
self._impl(doc).update(doc, spec, upsert, safe=True)
|
96 |
|
98 |
|
97 |
def delete(self, doc):
|
99 |
def delete(self, doc):
|
98 |
self._impl(doc).remove({'_id':doc._id})
|
100 |
self._impl(doc).remove({'_id':doc._id}, safe=True)
|
99 |
|
101 |
|
100 |
def set(self, doc, fields_values):
|
102 |
def set(self, doc, fields_values):
|
101 |
"""
|
103 |
"""
|
102 |
sets a key/value pairs, and persists those changes to the datastore immediately
|
104 |
sets a key/value pairs, and persists those changes to the datastore immediately
|
103 |
"""
|
105 |
"""
|
104 |
fields_values = Object.from_bson(fields_values)
|
106 |
fields_values = Object.from_bson(fields_values)
|
105 |
fields_values.make_safe()
|
107 |
fields_values.make_safe()
|
106 |
doc.update(fields_values)
|
108 |
doc.update(fields_values)
|
107 |
impl = self._impl(doc)
|
109 |
impl = self._impl(doc)
|
108 |
impl.update({'_id':doc._id}, {'$set':fields_values})
|
110 |
impl.update({'_id':doc._id}, {'$set':fields_values}, safe=True)
|
109 |
|
111 |
|
110 |
def increase_field(self, doc, **kwargs):
|
112 |
def increase_field(self, doc, **kwargs):
|
111 |
"""
|
113 |
"""
|
112 |
usage: increase_field(key=value)
|
114 |
usage: increase_field(key=value)
|
113 |
Sets a field to value, only if value is greater than the current value
|
115 |
Sets a field to value, only if value is greater than the current value
|
|
... |
|
... |
119 |
raise ValueError, "%s=%s" % (key, value)
|
121 |
raise ValueError, "%s=%s" % (key, value)
|
120 |
|
122 |
|
121 |
if key not in doc:
|
123 |
if key not in doc:
|
122 |
self._impl(doc).update(
|
124 |
self._impl(doc).update(
|
123 |
{'_id': doc._id, key: None},
|
125 |
{'_id': doc._id, key: None},
|
124 |
{'$set': {key: value}}
|
126 |
{'$set': {key: value}},
|
|
|
127 |
safe = True,
|
125 |
)
|
128 |
)
|
126 |
self._impl(doc).update(
|
129 |
self._impl(doc).update(
|
127 |
{'_id': doc._id, key: {'$lt': value}},
|
130 |
{'_id': doc._id, key: {'$lt': value}},
|
128 |
# failed attempt at doing it all in one operation
|
131 |
# failed attempt at doing it all in one operation
|
129 |
#{'$where': "this._id == '%s' && (!(%s in this) || this.%s < '%s')"
|
132 |
#{'$where': "this._id == '%s' && (!(%s in this) || this.%s < '%s')"
|
130 |
# % (doc._id.url_encode(), key, key, value)},
|
133 |
# % (doc._id.url_encode(), key, key, value)},
|
131 |
{'$set': {key: value}}
|
134 |
{'$set': {key: value}},
|
|
|
135 |
safe = True,
|
132 |
)
|
136 |
)
|