diff --git a/themis/importexport/sync.py b/themis/importexport/sync.py index ff9efad..d6ec50c 100644 --- a/themis/importexport/sync.py +++ b/themis/importexport/sync.py @@ -72,6 +72,24 @@ class SyncFromThemis(UtilityView): return 'OK' + def create_relation_value_if_needed(self, current_value, intid): + if intid is None: + return None + if current_value is None or current_value.to_id != intid: + return RelationValue(intid) + return current_value + + def create_relation_value_list_if_needed(self, current_value, intids): + if not intids: + return [] + if current_value: + current_intids = list(sorted([x.to_id for x in current_value])) + else: + current_indids = [] + if current_value is None or current_intids != intids: + return [RelationValue(x) for x in intids] + return current_value + def sync_deputies(self, timestamp=None): deputies = json.load(self.urlopen('%s/@@listDeputies' % self.src_url)) # XXX: add timestamp @@ -102,10 +120,8 @@ class SyncFromThemis(UtilityView): data.get('birthdate'), '%Y-%m-%d').toordinal()) else: object.birthdate = None - if data.get('polgroup'): - object.polgroup = RelationValue(self.get_polgroup_intid(data.get('polgroup'))) - else: - object.polgroup = None + self.polgroup = self.create_relation_value_if_needed(self.polgroup, + self.get_polgroup_intid(data.get('polgroup'))) if data.get('picture'): # this will be an url content = self.urlopen(data.get('picture')).read() @@ -156,17 +172,17 @@ class SyncFromThemis(UtilityView): title=data.get('title')) object = getattr(self.commissions_folder, new_id) object.active = data.get('active') - if data.get('president'): - object.president = RelationValue(self.get_author_intid('deputy:%s' % data.get('president'))) - if data.get('vicepresidents'): - object.vicepresidents = [RelationValue(self.get_author_intid('deputy:%s' % x)) - for x in data.get('vicepresidents')] - if data.get('members'): - object.members = [RelationValue(self.get_author_intid('deputy:%s' % x)) - for x in data.get('members')] - if data.get('substitutes'): - object.substitutes = [RelationValue(self.get_author_intid('deputy:%s' % x)) - for x in data.get('substitutes')] + object.president = self.create_relation_value_if_needed(object.president, + self.get_author_intid('deputy:%s' % data.get('president'))) + object.vicepresidents = self.create_relation_value_list_if_needed( + object.vicepresidents, + [self.get_author_intid('deputy:%s' % x) for x in data.get('vicepresidents', [])]) + object.members = self.create_relation_value_list_if_needed( + object.members, + [self.get_author_intid('deputy:%s' % x) for x in data.get('members', [])]) + object.substitutes = self.create_relation_value_list_if_needed( + object.substitutes, + [self.get_author_intid('deputy:%s' % x) for x in data.get('substitutes', [])]) if data.get('competences'): object.competences = data.get('competences') notify(ObjectModifiedEvent(object)) @@ -252,13 +268,14 @@ class SyncFromThemis(UtilityView): 'orateurs_seance_reponse_orale'): speakers = list(set(speakers + (data.get(attr) or []))) - object.speakers = [self.get_author_intid(x) for x in speakers] - object.speakers = [RelationValue(x) for x in object.speakers if x] + speakers_intids = [self.get_author_intid(x) for x in speakers] + object.speakers = self.create_relation_value_list_if_needed( + object.speakers, speakers_intids) reporters = [] - object.reporters = [self.get_author_intid(x) for x in data.get('rapporteurs') or []] - object.reporters = [RelationValue(x) for x in object.reporters if x] - + reporters_intids = [self.get_author_intid(x) for x in data.get('rapporteurs') or []] + object.reporters = self.create_relation_value_list_if_needed( + object.reporters, reporters_intids) if data.get('date_sanction_promulgation'): object.sanction_date = datetime.date.fromordinal( @@ -334,7 +351,8 @@ class SyncFromThemis(UtilityView): else: continue related_docs.append(self.intids.getId(related_doc)) - object.related_docs = [RelationValue(x) for x in related_docs if x] + object.related_docs = self.create_relation_value_list_if_needed( + object.related_docs, related_docs) authors_v = None if data.get('auteur'): @@ -349,7 +367,8 @@ class SyncFromThemis(UtilityView): authors_intids = [self.get_author_intid(x) for x in authors_v] if None in authors_intids: log.warn('unknown error in doc %s' % object.id) - object.authors = [RelationValue(x) for x in authors_intids if x] + object.authors = self.create_relation_value_list_if_needed( + object.authors, authors_intids) object.author_is_government = False else: object.authors = None