From dcbd49b4422eb2ed264c323c70d7030e23970648 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 10 Mar 2016 12:33:59 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Elimina=20stubs=20desnecess=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- legacy/migration.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/legacy/migration.py b/legacy/migration.py index d054ef7eb..7f004869b 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -25,6 +25,8 @@ appconfs = [apps.get_app_config(n) for n in [ 'lexml', 'protocoloadm', ]] +stubs_list = [] + name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] # apps do not overlap @@ -103,6 +105,8 @@ def get_fk_related(field, value, label=None): warn(msg + ' => using NONE for zero value') else: value = make_stub(field.related_model, value) + stubs_list.append( + (value.id, field.model, field.related_model, field.name)) warn(msg + ' => STUB CREATED') else: assert value @@ -193,6 +197,8 @@ class DataMigrator: info('Deleting models with ind_excluido...') for obj in self.to_delete: obj.delete() + info('Deleting unnecessary stubs...') + self.delete_stubs() def _do_migrate(self, obj): if isinstance(obj, AppConfig): @@ -245,6 +251,19 @@ class DataMigrator: if getattr(old, 'ind_excluido', False): self.to_delete.append(new) + def delete_stubs(self): + for line in stubs_list: + stub_id = line[0] + model = line[1] + related_model = line[2] + field_name = line[3] + + # Filter all objects in model and delete from related model + # if quantity is equal to zero + obj_quantity = model.objects.filter(**{field_name: stub_id}) + if obj_quantity.count() == 0: + related_model.objects.get(**{'id': stub_id}).delete() + def migrate(obj=appconfs): dm = DataMigrator() From 98c05d21b78e9d3f9ea84487dc448c9774e0b7b8 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Thu, 17 Mar 2016 10:32:45 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Arruma=20conven=C3=A7=C3=A3o=20de=20lista?= =?UTF-8?q?=20e=20demais=20sugest=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- legacy/migration.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index 7f004869b..5e8cf3ed6 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -105,8 +105,7 @@ def get_fk_related(field, value, label=None): warn(msg + ' => using NONE for zero value') else: value = make_stub(field.related_model, value) - stubs_list.append( - (value.id, field.model, field.related_model, field.name)) + stubs_list.append((value.id, field)) warn(msg + ' => STUB CREATED') else: assert value @@ -253,16 +252,11 @@ class DataMigrator: def delete_stubs(self): for line in stubs_list: - stub_id = line[0] - model = line[1] - related_model = line[2] - field_name = line[3] - + stub, field = line # Filter all objects in model and delete from related model # if quantity is equal to zero - obj_quantity = model.objects.filter(**{field_name: stub_id}) - if obj_quantity.count() == 0: - related_model.objects.get(**{'id': stub_id}).delete() + if field.model.objects.filter(**{field.name: stub}).exists(): + field.related_model.objects.get(**{'id': stub}).delete() def migrate(obj=appconfs):