From d61c64112a746387d0f2c72b7d71a0888936ecba Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sat, 19 Jul 2014 23:54:23 +0200 Subject: [PATCH] Manage categories and improve JSON data loading --- mycoserver/group.py | 125 ++++++++++++++++++++++++++++++++------------ 1 file changed, 93 insertions(+), 32 deletions(-) diff --git a/mycoserver/group.py b/mycoserver/group.py index 7b7c268..91a8683 100644 --- a/mycoserver/group.py +++ b/mycoserver/group.py @@ -61,29 +61,35 @@ class Group(object): self.deletedContributors={} self.contributions={} self.deletedContributions={} + self.categories={} + self.deletedCategories={} def load(self,data): - try: - self.uuid=data['uuid'] - self.name=data['name'] - for email in data['contributors']: - self.contributors[email]=Contributor() - self.contributors[email].load(data['contributors'][email]) - if 'deletedContributors' in data: - for email in data['deletedContributors']: - self.deletedContributors[email]=Contributor() - self.deletedContributors[email].load(data['deletedContributors'][email]) - for uuid in data['contributions']: - self.contributions[uuid]=Contribution() - self.contributions[uuid].load(data['contributions'][uuid]) - if 'deletedContributions' in data: - for uuid in data['deletedContributions']: - self.deletedContributions[uuid]=Contribution() - self.deletedContributions[uuid].load(data['deletedContributions'][uuid]) - return True - except Exception,e: - logging.error('Error loading JSON data : %s',e) - return False + self.uuid=data.get('uuid',None) + self.name=data.get('name',None) + for email in data.get('contributors'): + self.contributors[email]=Contributor() + self.contributors[email].load(data['contributors'][email]) + if 'deletedContributors' in data: + for email in data.get('deletedContributors',{}): + self.deletedContributors[email]=Contributor() + self.deletedContributors[email].load(data['deletedContributors'][email]) + for uuid in data.get('contributions',{}): + self.contributions[uuid]=Contribution() + self.contributions[uuid].load(data['contributions'][uuid]) + if 'deletedContributions' in data: + for uuid in data.get('deletedContributions',{}): + self.deletedContributions[uuid]=Contribution() + self.deletedContributions[uuid].load(data['deletedContributions'][uuid]) + if 'categories' in data: + for uuid in data.get('categories',{}): + self.categories[uuid]=Category() + self.categories[uuid].load(data['categories'][uuid]) + 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): contributors={} @@ -102,13 +108,23 @@ class Group(object): for uuid in self.deletedContributions: 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 { 'uuid': self.uuid, 'name': self.name, 'contributors': contributors, 'deletedContributors': deletedContributors, 'contributions': contributions, - 'deletedContributions': deletedContributions + 'deletedContributions': deletedContributions, + 'categories': categories, + 'deletedCategories': deletedCategories } def restoreContributor(self, email): @@ -219,6 +235,23 @@ class Group(object): ret.contributions[uuid]=group.contributions[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 @@ -230,15 +263,17 @@ class Contribution(object): self.title=None self.cost=None self.date=None + self.category=None self.lastChange=None def load(self,data): - self.uuid=data['uuid'] - self.contributor=data['contributor'] - self.title=data['title'] - self.cost=data['cost'] - self.date=data['date'] - self.lastChange=data['lastChange'] + self.uuid=data.get('uuid',None) + self.contributor=data.get('contributor',None) + self.title=data.get('title',None) + self.cost=data.get('cost',None) + self.date=data.get('date',None) + self.category=data.get('category',None) + self.lastChange=data.get('lastChange',None) def export(self): return { @@ -247,6 +282,7 @@ class Contribution(object): 'cost': self.cost, 'title': self.title, 'date': self.date, + 'category': self.category, 'lastChange': self.lastChange } @@ -264,10 +300,9 @@ class Contributor(object): self.deletionTime=None def load(self,data): - self.name=data['name'] - self.email=data['email'] - if 'deletionTime' in data: - self.deletionTime=data['deletionTime'] + self.name=data.get('name',None) + self.email=data.get('email',None) + self.deletionTime=data.get('deletionTime',None) def export(self): ret={ @@ -278,6 +313,32 @@ class Contributor(object): ret['deletionTime']=self.deletionTime 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__': import testdata import json