def populate_renamed_fields(self, new, old): renames = self.field_renames[type(new)] for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() msg = ("O valor do campo %s (%s) da model %s era inválido" % (field.name, field_type, field.model.__name__)) if old_field_name: old_value = getattr(old, old_field_name) if isinstance(field, models.ForeignKey): old_type = type(old) # not necessarily a model if hasattr(old_type, '_meta') and \ old_type._meta.pk.name != 'id': label = old.pk else: label = '-- SEM PK --' value = get_fk_related(field, old_value, label) else: value = getattr(old, old_field_name) if field_type == 'DateField' and \ not field.null and value is None: descricao = 'A data 1111-11-11 foi colocada no lugar' problema = 'O valor da data era nulo ou inválido' warn(msg + ' => ' + descricao) value = date(1111, 11, 11) self.data_mudada['obj'] = new self.data_mudada['descricao'] = descricao self.data_mudada['problema'] = problema self.data_mudada.setdefault('nome_campo', []).\ append(field.name) if field_type == 'CharField' or field_type == 'TextField': if value is None or value == 'None': value = '' setattr(new, field.name, value) elif field.model.__name__ == 'TipoAutor' and \ field.name == 'content_type': model = normalize(new.descricao.lower()).replace(' ', '') content_types = field.related_model.objects.filter( model=model).exclude(app_label='legacy') assert len(content_types) <= 1 value = content_types[0] if content_types else None setattr(new, field.name, value)
def adjust_tipo_autor(new, old): model_apontado = normalize(new.descricao.lower()).replace(' ', '') content_types = ContentType.objects.filter( model=model_apontado).exclude(app_label='legacy') assert len(content_types) <= 1 new.content_type = content_types[0] if content_types else None
def populate_renamed_fields(self, new, old): renames = self.field_renames[type(new)] for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() msg = ("O valor do campo %s (%s) da model %s era inválido" % (field.name, field_type, field.model.__name__)) if old_field_name: old_value = getattr(old, old_field_name) if isinstance(field, models.ForeignKey): old_type = type(old) # not necessarily a model if hasattr(old_type, '_meta') and \ old_type._meta.pk.name != 'id': label = old.pk else: label = '-- SEM PK --' value = get_fk_related(field, old_value, label) else: value = getattr(old, old_field_name) if field_type == 'DateField' and \ not field.null and value is None: descricao = 'A data 0001-01-01 foi colocada no lugar' problema = 'O valor da data era nulo ou inválido' warn(msg + ' => ' + descricao) value = '0001-01-01' self.data_mudada['obj'] = new self.data_mudada['descricao'] = descricao self.data_mudada['problema'] = problema self.data_mudada.setdefault('nome_campo', []).\ append(field.name) if field_type == 'CharField' or field_type == 'TextField': if value is None or value == 'None': value = '' if field.model._meta.label == 'sessao.RegistroVotacao' and \ field.name == 'ordem' and \ not isinstance(value, OrdemDia): try: new_value = ExpedienteMateria.objects.get(pk=value) setattr(new, 'expediente', new_value) setattr(new, field.name, None) continue except ObjectDoesNotExist: msg = 'FK [%s] não encontrada para valor %s ' \ '(em %s %s)' % ( field.name, value, field.model.__name__, label or '---') value = make_stub(field.related_model, value) descricao = 'stub criado para entrada orfã!' warn(msg + ' => ' + descricao) save_relation(value, [field.name], msg, descricao, eh_stub=True) setattr(new, field.name, value) elif field.model.__name__ == 'TipoAutor' and \ field.name == 'content_type': try: value = field.related_model.objects.get( model=normalize(new.descricao.lower()).replace(' ', '')) except ObjectDoesNotExist: value = None setattr(new, field.name, value)
def populate_renamed_fields(self, new, old): renames = self.field_renames[type(new)] for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() msg = ("O valor do campo %s (%s) da model %s era inválido" % (field.name, field_type, field.model.__name__)) if old_field_name: old_value = getattr(old, old_field_name) if isinstance(field, models.ForeignKey): old_type = type(old) # not necessarily a model if hasattr(old_type, '_meta') and \ old_type._meta.pk.name != 'id': label = old.pk else: label = '-- SEM PK --' value = get_fk_related(field, old_value, label) else: value = getattr(old, old_field_name) if field_type == 'DateField' and \ not field.null and value is None: descricao = 'A data 0001-01-01 foi colocada no lugar' problema = 'O valor da data era nulo ou inválido' warn(msg + ' => ' + descricao) value = '0001-01-01' self.data_mudada['obj'] = new self.data_mudada['descricao'] = descricao self.data_mudada['problema'] = problema self.data_mudada.setdefault('nome_campo', []).\ append(field.name) if field_type == 'CharField' or field_type == 'TextField': if value is None or value == 'None': value = '' if field.model._meta.label == 'sessao.RegistroVotacao' and \ field.name == 'ordem' and \ not isinstance(value, OrdemDia): try: new_value = ExpedienteMateria.objects.get(pk=value) setattr(new, 'expediente', new_value) setattr(new, field.name, None) continue except ObjectDoesNotExist: msg = 'FK [%s] não encontrada para valor %s ' \ '(em %s %s)' % ( field.name, value, field.model.__name__, label or '---') value = make_stub(field.related_model, value) descricao = 'stub criado para entrada orfã!' warn(msg + ' => ' + descricao) save_relation(value, [field.name], msg, descricao, eh_stub=True) setattr(new, field.name, value) elif field.model.__name__ == 'TipoAutor' and \ field.name == 'content_type': try: value = field.related_model.objects.get(model=normalize( new.descricao.lower()).replace(' ', '')) except ObjectDoesNotExist: value = None setattr(new, field.name, value)
def adjust_tipo_autor(new, old): model_apontado = normalize(new.descricao.lower()).replace(' ', '') content_types = ContentType.objects.filter(model=model_apontado) assert len(content_types) <= 1 new.content_type = content_types[0] if content_types else None