Manage categories and improve JSON data loading
Benjamin Renard

Benjamin Renard commited on 2014-07-19 21:55:13
Showing 1 changed files, with 82 additions and 21 deletions.

... ...
@@ -61,29 +61,35 @@ class Group(object):
61 61
     self.deletedContributors={}
62 62
     self.contributions={}
63 63
     self.deletedContributions={}
64
+    self.categories={}
65
+    self.deletedCategories={}
64 66
 
65 67
   def load(self,data):
66
-    try:
67
-      self.uuid=data['uuid']
68
-      self.name=data['name']
69
-      for email in data['contributors']:
68
+    self.uuid=data.get('uuid',None)
69
+    self.name=data.get('name',None)
70
+    for email in data.get('contributors'):
70 71
       self.contributors[email]=Contributor()
71 72
       self.contributors[email].load(data['contributors'][email])
72 73
     if 'deletedContributors' in data:
73
-        for email in data['deletedContributors']:
74
+      for email in data.get('deletedContributors',{}):
74 75
         self.deletedContributors[email]=Contributor()
75 76
         self.deletedContributors[email].load(data['deletedContributors'][email])
76
-      for uuid in data['contributions']:
77
+    for uuid in data.get('contributions',{}):
77 78
       self.contributions[uuid]=Contribution()
78 79
       self.contributions[uuid].load(data['contributions'][uuid])
79 80
     if 'deletedContributions' in data:
80
-        for uuid in data['deletedContributions']:
81
+      for uuid in data.get('deletedContributions',{}):
81 82
         self.deletedContributions[uuid]=Contribution()
82 83
         self.deletedContributions[uuid].load(data['deletedContributions'][uuid])
84
+    if 'categories' in data:
85
+      for uuid in data.get('categories',{}):
86
+        self.categories[uuid]=Category()
87
+        self.categories[uuid].load(data['categories'][uuid])
88
+    if 'deletedCategories' in data:
89
+      for uuid in data.get('deletedCategories',{}):
90
+        self.deletedCategories[uuid]=Category()
91
+        self.deletedCategories[uuid].load(data['deletedCategories'][uuid])
83 92
     return True
84
-    except Exception,e:
85
-      logging.error('Error loading JSON data : %s',e)
86
-      return False
87 93
 
88 94
   def export(self):
89 95
     contributors={}
... ...
@@ -102,13 +108,23 @@ class Group(object):
102 108
     for uuid in self.deletedContributions:
103 109
       deletedContributions[uuid]=self.deletedContributions[uuid].export()
104 110
 
111
+    categories={}
112
+    for uuid in self.categories:
113
+      categories[uuid]=self.categories[uuid].export()
114
+
115
+    deletedCategories={}
116
+    for uuid in self.deletedCategories:
117
+      deletedCategories[uuid]=self.deletedCategories[uuid].export()
118
+
105 119
     return {
106 120
       'uuid': self.uuid,
107 121
       'name': self.name,
108 122
       'contributors': contributors,
109 123
       'deletedContributors': deletedContributors,
110 124
       'contributions': contributions,
111
-      'deletedContributions': deletedContributions
125
+      'deletedContributions': deletedContributions,
126
+      'categories': categories,
127
+      'deletedCategories': deletedCategories
112 128
     }
113 129
 
114 130
   def restoreContributor(self, email):
... ...
@@ -219,6 +235,23 @@ class Group(object):
219 235
           ret.contributions[uuid]=group.contributions[uuid]
220 236
           del ret.deletedContributions[uuid]
221 237
 
238
+    ## Categories
239
+    for uuid in self.categories:
240
+      if uuid in group.categories:
241
+        ret.categories[uuid]=self.categories[uuid].sync(group.categories[uuid])
242
+      elif uuid not in ret.deletedCategories:
243
+        ret.categories[uuid]=self.categories[uuid]
244
+      elif self.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange:
245
+        ret.categories[uuid]=self.categories[uuid]
246
+        del ret.deletedCategories[uuid]
247
+    for uuid in group.categories:
248
+      if uuid not in ret.categories:
249
+        if uuid not in ret.deletedCategories:
250
+          ret.categories[uuid]=group.categories[uuid]
251
+        elif group.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange:
252
+          ret.categories[uuid]=group.categories[uuid]
253
+          del ret.deletedCategories[uuid]
254
+
222 255
     return ret
223 256
 
224 257
 
... ...
@@ -230,15 +263,17 @@ class Contribution(object):
230 263
     self.title=None
231 264
     self.cost=None
232 265
     self.date=None
266
+    self.category=None
233 267
     self.lastChange=None
234 268
 
235 269
   def load(self,data):
236
-    self.uuid=data['uuid']
237
-    self.contributor=data['contributor']
238
-    self.title=data['title']
239
-    self.cost=data['cost']
240
-    self.date=data['date']
241
-    self.lastChange=data['lastChange']
270
+    self.uuid=data.get('uuid',None)
271
+    self.contributor=data.get('contributor',None)
272
+    self.title=data.get('title',None)
273
+    self.cost=data.get('cost',None)
274
+    self.date=data.get('date',None)
275
+    self.category=data.get('category',None)
276
+    self.lastChange=data.get('lastChange',None)
242 277
 
243 278
   def export(self):
244 279
     return {
... ...
@@ -247,6 +282,7 @@ class Contribution(object):
247 282
       'cost': self.cost,
248 283
       'title': self.title,
249 284
       'date': self.date,
285
+      'category': self.category,
250 286
       'lastChange': self.lastChange
251 287
     }
252 288
 
... ...
@@ -264,10 +300,9 @@ class Contributor(object):
264 300
     self.deletionTime=None
265 301
 
266 302
   def load(self,data):
267
-    self.name=data['name']
268
-    self.email=data['email']
269
-    if 'deletionTime' in data:
270
-      self.deletionTime=data['deletionTime']
303
+    self.name=data.get('name',None)
304
+    self.email=data.get('email',None)
305
+    self.deletionTime=data.get('deletionTime',None)
271 306
 
272 307
   def export(self):
273 308
     ret={
... ...
@@ -278,6 +313,32 @@ class Contributor(object):
278 313
       ret['deletionTime']=self.deletionTime
279 314
     return ret
280 315
 
316
+class Category(object):
317
+
318
+  def __init__(self):
319
+    self.name=None
320
+    self.color=None
321
+    self.lastChange=None
322
+
323
+  def load(self,data):
324
+    self.name=data.get('name',None)
325
+    self.color=data.get('color',None)
326
+    self.lastChange=data.get('lastChange',None)
327
+
328
+  def export(self):
329
+    ret={
330
+      'name': self.name,
331
+      'color': self.color,
332
+      'lastChange': self.lastChange
333
+    }
334
+    return ret
335
+
336
+  def sync(self, c):
337
+    if c.lastChange>self.lastChange:
338
+      return c
339
+    else:
340
+      return self
341
+
281 342
 if __name__ == '__main__':
282 343
   import testdata
283 344
   import json
284 345