From 3c47701d8895ff6d38127ac49449bac6ba06c7ff Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Tue, 19 Apr 2016 16:04:45 -0300 Subject: [PATCH] =?UTF-8?q?Cria=20fun=C3=A7=C3=A3o=20para=20pegar=20fields?= =?UTF-8?q?=5Fdict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- legacy/migration.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index f4a01b93c..40c73b3f8 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -97,7 +97,7 @@ def warn(msg): def get_fk_related(field, value, label=None): - fields_dict = {} + # fields_dict = {} if value is None and field.null is False: value = 0 if value is not None: @@ -112,11 +112,12 @@ def get_fk_related(field, value, label=None): # se FK == 0, criamos um stub e colocamos o valor '????????' # para qualquer CharField ou TextField que possa haver if not field.null: - all_fields = field.related_model._meta.get_fields() - fields_dict = {f.name: '????????????'[:f.max_length] - for f in all_fields - if isinstance(f, (CharField, TextField)) and - not f.choices and not f.blank} + # all_fields = field.related_model._meta.get_fields() + # fields_dict = {f.name: '????????????'[:f.max_length] + # for f in all_fields + # if isinstance(f, (CharField, TextField)) and + # not f.choices and not f.blank} + fields_dict = get_fields_dict(field.related_model) value = mommy.make(field.related_model, **fields_dict) descricao = 'stub criado para campos não nuláveis!' @@ -229,12 +230,23 @@ def save_relation(obj, problema='', descricao=''): def make_stub(model, id): - new = mommy.prepare(model) + fields_dict = get_fields_dict(model) + new = mommy.prepare(model, **fields_dict) save_with_id(new, id) return new +def get_fields_dict(model): + all_fields = model._meta.get_fields() + fields_dict = {} + fields_dict = {f.name: '????????????'[:f.max_length] + for f in all_fields + if isinstance(f, (CharField, TextField)) and + not f.choices and not f.blank} + return fields_dict + + class DataMigrator: def __init__(self): self.field_renames, self.model_renames = get_renames() @@ -352,7 +364,7 @@ class DataMigrator: def delete_stubs(self): excluidos = 0 - for obj in ProblemaMigracao.objects.all().reverse(): + for obj in ProblemaMigracao.objects.all(): if obj.content_object: original = obj.content_type.get_all_objects_for_this_type( id=obj.object_id) @@ -433,12 +445,9 @@ def check_app_no_ind_excluido(app): def make_with_log(model, _quantity=None, make_m2m=False, **attrs): + import ipdb; ipdb.set_trace() all_fields = model._meta.get_fields() - fields_dict = {} - fields_dict = {f.name: '????????????'[:f.max_length] - for f in all_fields - if isinstance(f, (CharField, TextField)) and - not f.choices and not f.blank} + fields_dict = get_fields_dict(model) stub = make(model, _quantity, make_m2m, **fields_dict) problema = 'Um stub foi necessário durante a criação de um outro stub' descricao = 'Essa entrada é necessária para um dos stubs criados'