Browse Source

Merge pull request #263 from interlegis/136-eliminar-stubs-desnecessarios

fix #136 eliminar stubs desnecessarios
pull/285/head
Marcio Mazza 9 years ago
parent
commit
f94f34e1f7
  1. 13
      legacy/migration.py

13
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,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))
warn(msg + ' => STUB CREATED')
else:
assert value
@ -193,6 +196,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 +250,14 @@ class DataMigrator:
if getattr(old, 'ind_excluido', False):
self.to_delete.append(new)
def delete_stubs(self):
for line in stubs_list:
stub, field = line
# Filter all objects in model and delete from related model
# if quantity is equal to zero
if field.model.objects.filter(**{field.name: stub}).exists():
field.related_model.objects.get(**{'id': stub}).delete()
def migrate(obj=appconfs):
dm = DataMigrator()

Loading…
Cancel
Save