2ppJfGsIzG25YmPJTigrnC changeset

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