Skip to content

Commit 0210599

Browse files
Tariq DaoudaTariq Daouda
authored andcommitted
fill_default ands tests
1 parent 3d91b98 commit 0210599

File tree

4 files changed

+126
-62
lines changed

4 files changed

+126
-62
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.1.0
2+
=====
3+
* Added getitem for documents at the database level
4+
* Added fill_default() on documents to replace None values by schema defaults
5+
* fill_default() is automatically called on save
6+
17
2.0.2
28
=====
39
* Fixed contains functions

pyArango/document.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,7 @@ def validate(self):
108108
return True
109109

110110
def set(self, dct):
111-
"""Set the store using a dictionary"""
112-
# if not self.mustValidate:
113-
# self.store = dct
114-
# self.patchStore = dct
115-
# return
116-
111+
"""Set the values to a dict. Any missing value will be filled by it's default"""
117112
for field, value in dct.items():
118113
if field not in self.collection.arangoPrivates:
119114
if isinstance(value, dict):
@@ -126,6 +121,14 @@ def set(self, dct):
126121
else:
127122
self[field] = value
128123

124+
def fill_default(self):
125+
"""replace all None values with defaults"""
126+
for field, value in self.validators.items():
127+
if isinstance(value, dict):
128+
self[field].fill_default()
129+
elif self[field] is None:
130+
self[field] = value.default
131+
129132
def __dir__(self):
130133
return dir(self.getStore())
131134

@@ -234,6 +237,10 @@ def to_default(self):
234237
"""reset the document to the default values"""
235238
self.reset(self.collection, self.collection.getDefaultDocument())
236239

240+
def fill_default(self):
241+
"""reset the document to the default values"""
242+
self._store.fill_default()
243+
237244
def validate(self):
238245
"""validate the document"""
239246
self._store.validate()
@@ -264,7 +271,9 @@ def save(self, waitForSync = False, **docArgs):
264271
If you want to only update the modified fields use the .patch() function.
265272
Use docArgs to put things such as 'waitForSync = True' (for a full list cf ArangoDB's doc).
266273
It will only trigger a saving of the document if it has been modified since the last save. If you want to force the saving you can use forceSave()"""
274+
self._store.fill_default()
267275
payload = self._store.getStore()
276+
# print(payload)
268277
self._save(payload, waitForSync = False, **docArgs)
269278

270279
def _save(self, payload, waitForSync = False, **docArgs):

0 commit comments

Comments
 (0)