Revision 633436626234 () - Diff

Link to this snippet: https://friendpaste.com/2ppJfGsIzG25YmPJTigrnC
Embed:
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
def update_fields(self, record_id, fields):
"""Safely update a number of fields. 'fields' being a
dictionary with path_tuple: value for only the fields we want
to change the value of, where path_tuple is a tuple of
fieldnames indicating the path to the possibly nested field
we're interested in.
"""
while True:
record = self.db[record_id]
modified = False
for path, value in fields.items():
if not isinstance(path, tuple):
path = (path,)
parent = record
for field in path[:-1]:
parent = parent.setdefault(field, {})
if parent.get(path[-1]) == value:
continue
modified = True
parent[path[-1]] = value
if modified:
try:
self.db[record.id] = record
except ResourceConflict:
logger.debug(
"Update of record with id %s in database %s failed." % (
record_id, self.db.name))
continue
break