Browse Source

Arruma sugestões do PR

Signed-off-by: Luciano Almeida <lucianoalmeida@interlegis.leg.br>
pull/276/head
Luciano Almeida 9 years ago
parent
commit
0ab1f3bc4a
  1. 47
      legacy/migration.py

47
legacy/migration.py

@ -6,6 +6,7 @@ from django.apps import apps
from django.apps.config import AppConfig from django.apps.config import AppConfig
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import connections, models from django.db import connections, models
from django.db.models import CharField, TextField
from django.db.models.base import ModelBase from django.db.models.base import ModelBase
from model_mommy import mommy from model_mommy import mommy
@ -26,7 +27,7 @@ appconfs = [apps.get_app_config(n) for n in [
'protocoloadm', ]] 'protocoloadm', ]]
stubs_list = [] stubs_list = []
stub_created = False
name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs] name_sets = [set(m.__name__ for m in ac.get_models()) for ac in appconfs]
# apps do not overlap # apps do not overlap
@ -87,48 +88,41 @@ def info(msg):
def warn(msg): def warn(msg):
print('WARNING! ' + msg) print('CUIDADO! ' + msg)
def get_fk_related(field, value, label=None): def get_fk_related(field, value, label=None):
has_textfield = False
fields_dict = {} fields_dict = {}
global stub_created
if value is None and field.null is False:
value = 0
if value is not None: if value is not None:
try: try:
value = field.related_model.objects.get(id=value) value = field.related_model.objects.get(id=value)
except ObjectDoesNotExist: except ObjectDoesNotExist:
msg = 'FK [%s] not found for value %s ' \ msg = 'FK [%s] não encontrada para valor %s ' \
'(in %s %s)' % ( '(em %s %s)' % (
field.name, value, field.name, value,
field.model.__name__, label or '---') field.model.__name__, label or '---')
if value == 0: if value == 0:
# se FK == 0, criamos um stub e colocamos o valor DESCONHECIDO # se FK == 0, criamos um stub e colocamos o valor '????????
# para qualquer TextField que possa haver # para qualquer CharField ou TextField que possa haver
if not field.null:
all_fields = field.related_model._meta.get_fields() all_fields = field.related_model._meta.get_fields()
for related_field in all_fields: fields_dict = {f.name: '????????????'[:f.max_length]
if related_field.get_internal_type() == 'TextField': for f in all_fields
fields_dict[related_field.name] = 'DESCONHECIDO' if isinstance(f, (CharField, TextField)) and
has_textfield = True not f.choices and not f.blank}
elif related_field.get_internal_type() == 'CharField': value = mommy.make(field.related_model,
fields_dict[related_field.name] = 'DESC'
has_textfield = True
if has_textfield and field.null is False:
if not stub_created:
stub_created = mommy.make(field.related_model,
**fields_dict) **fields_dict)
warn(msg + ' => STUB CREATED FOR NOT NULL FIELD') warn(msg + ' => STUB criada para campos não nuláveis!')
value = stub_created
else:
value = stub_created
warn(msg + ' => USING STUB ALREADY CREATED')
else: else:
value = None value = None
warn(msg + ' => using NONE for zero value') warn(msg + ' => usando None para valores iguais a zero!')
else: else:
value = make_stub(field.related_model, value) value = make_stub(field.related_model, value)
stubs_list.append((value.id, field)) stubs_list.append((value.id, field))
warn(msg + ' => STUB CREATED') warn(msg + ' => STUB criada!')
else: else:
assert value assert value
return value return value
@ -240,7 +234,6 @@ class DataMigrator:
legacy_model_name = self.model_renames.get(model, model.__name__) legacy_model_name = self.model_renames.get(model, model.__name__)
legacy_model = legacy_app.get_model(legacy_model_name) legacy_model = legacy_app.get_model(legacy_model_name)
legacy_pk_name = legacy_model._meta.pk.name legacy_pk_name = legacy_model._meta.pk.name
global stub_created
# Clear all model entries # Clear all model entries
# They may have been created in a previous migration attempt # They may have been created in a previous migration attempt
@ -251,14 +244,12 @@ class DataMigrator:
# There is no pk in the legacy table # There is no pk in the legacy table
def save(new, old): def save(new, old):
new.save() new.save()
stub_created = False
old_records = iter_sql_records( old_records = iter_sql_records(
'select * from ' + legacy_model._meta.db_table, 'legacy') 'select * from ' + legacy_model._meta.db_table, 'legacy')
else: else:
def save(new, old): def save(new, old):
save_with_id(new, getattr(old, legacy_pk_name)) save_with_id(new, getattr(old, legacy_pk_name))
stub_created = False
old_records = legacy_model.objects.all().order_by(legacy_pk_name) old_records = legacy_model.objects.all().order_by(legacy_pk_name)

Loading…
Cancel
Save