Manage categories and improve JSON data loading

This commit is contained in:
Benjamin Renard 2014-07-19 23:54:23 +02:00 committed by root
parent 4e883c3705
commit d61c64112a

View file

@ -61,29 +61,35 @@ class Group(object):
self.deletedContributors={} self.deletedContributors={}
self.contributions={} self.contributions={}
self.deletedContributions={} self.deletedContributions={}
self.categories={}
self.deletedCategories={}
def load(self,data): def load(self,data):
try: self.uuid=data.get('uuid',None)
self.uuid=data['uuid'] self.name=data.get('name',None)
self.name=data['name'] for email in data.get('contributors'):
for email in data['contributors']: self.contributors[email]=Contributor()
self.contributors[email]=Contributor() self.contributors[email].load(data['contributors'][email])
self.contributors[email].load(data['contributors'][email]) if 'deletedContributors' in data:
if 'deletedContributors' in data: for email in data.get('deletedContributors',{}):
for email in data['deletedContributors']: self.deletedContributors[email]=Contributor()
self.deletedContributors[email]=Contributor() self.deletedContributors[email].load(data['deletedContributors'][email])
self.deletedContributors[email].load(data['deletedContributors'][email]) for uuid in data.get('contributions',{}):
for uuid in data['contributions']: self.contributions[uuid]=Contribution()
self.contributions[uuid]=Contribution() self.contributions[uuid].load(data['contributions'][uuid])
self.contributions[uuid].load(data['contributions'][uuid]) if 'deletedContributions' in data:
if 'deletedContributions' in data: for uuid in data.get('deletedContributions',{}):
for uuid in data['deletedContributions']: self.deletedContributions[uuid]=Contribution()
self.deletedContributions[uuid]=Contribution() self.deletedContributions[uuid].load(data['deletedContributions'][uuid])
self.deletedContributions[uuid].load(data['deletedContributions'][uuid]) if 'categories' in data:
return True for uuid in data.get('categories',{}):
except Exception,e: self.categories[uuid]=Category()
logging.error('Error loading JSON data : %s',e) self.categories[uuid].load(data['categories'][uuid])
return False if 'deletedCategories' in data:
for uuid in data.get('deletedCategories',{}):
self.deletedCategories[uuid]=Category()
self.deletedCategories[uuid].load(data['deletedCategories'][uuid])
return True
def export(self): def export(self):
contributors={} contributors={}
@ -102,13 +108,23 @@ class Group(object):
for uuid in self.deletedContributions: for uuid in self.deletedContributions:
deletedContributions[uuid]=self.deletedContributions[uuid].export() deletedContributions[uuid]=self.deletedContributions[uuid].export()
categories={}
for uuid in self.categories:
categories[uuid]=self.categories[uuid].export()
deletedCategories={}
for uuid in self.deletedCategories:
deletedCategories[uuid]=self.deletedCategories[uuid].export()
return { return {
'uuid': self.uuid, 'uuid': self.uuid,
'name': self.name, 'name': self.name,
'contributors': contributors, 'contributors': contributors,
'deletedContributors': deletedContributors, 'deletedContributors': deletedContributors,
'contributions': contributions, 'contributions': contributions,
'deletedContributions': deletedContributions 'deletedContributions': deletedContributions,
'categories': categories,
'deletedCategories': deletedCategories
} }
def restoreContributor(self, email): def restoreContributor(self, email):
@ -219,6 +235,23 @@ class Group(object):
ret.contributions[uuid]=group.contributions[uuid] ret.contributions[uuid]=group.contributions[uuid]
del ret.deletedContributions[uuid] del ret.deletedContributions[uuid]
## Categories
for uuid in self.categories:
if uuid in group.categories:
ret.categories[uuid]=self.categories[uuid].sync(group.categories[uuid])
elif uuid not in ret.deletedCategories:
ret.categories[uuid]=self.categories[uuid]
elif self.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange:
ret.categories[uuid]=self.categories[uuid]
del ret.deletedCategories[uuid]
for uuid in group.categories:
if uuid not in ret.categories:
if uuid not in ret.deletedCategories:
ret.categories[uuid]=group.categories[uuid]
elif group.categories[uuid].lastChange>ret.deletedCategories[uuid].lastChange:
ret.categories[uuid]=group.categories[uuid]
del ret.deletedCategories[uuid]
return ret return ret
@ -230,15 +263,17 @@ class Contribution(object):
self.title=None self.title=None
self.cost=None self.cost=None
self.date=None self.date=None
self.category=None
self.lastChange=None self.lastChange=None
def load(self,data): def load(self,data):
self.uuid=data['uuid'] self.uuid=data.get('uuid',None)
self.contributor=data['contributor'] self.contributor=data.get('contributor',None)
self.title=data['title'] self.title=data.get('title',None)
self.cost=data['cost'] self.cost=data.get('cost',None)
self.date=data['date'] self.date=data.get('date',None)
self.lastChange=data['lastChange'] self.category=data.get('category',None)
self.lastChange=data.get('lastChange',None)
def export(self): def export(self):
return { return {
@ -247,6 +282,7 @@ class Contribution(object):
'cost': self.cost, 'cost': self.cost,
'title': self.title, 'title': self.title,
'date': self.date, 'date': self.date,
'category': self.category,
'lastChange': self.lastChange 'lastChange': self.lastChange
} }
@ -264,10 +300,9 @@ class Contributor(object):
self.deletionTime=None self.deletionTime=None
def load(self,data): def load(self,data):
self.name=data['name'] self.name=data.get('name',None)
self.email=data['email'] self.email=data.get('email',None)
if 'deletionTime' in data: self.deletionTime=data.get('deletionTime',None)
self.deletionTime=data['deletionTime']
def export(self): def export(self):
ret={ ret={
@ -278,6 +313,32 @@ class Contributor(object):
ret['deletionTime']=self.deletionTime ret['deletionTime']=self.deletionTime
return ret return ret
class Category(object):
def __init__(self):
self.name=None
self.color=None
self.lastChange=None
def load(self,data):
self.name=data.get('name',None)
self.color=data.get('color',None)
self.lastChange=data.get('lastChange',None)
def export(self):
ret={
'name': self.name,
'color': self.color,
'lastChange': self.lastChange
}
return ret
def sync(self, c):
if c.lastChange>self.lastChange:
return c
else:
return self
if __name__ == '__main__': if __name__ == '__main__':
import testdata import testdata
import json import json