2

Buenas tardes tengo el siguiente fragmento de código:

content_view = cleaned_data.get('content_view')
show_content = cleaned_data.get('show_content')
show_children = cleaned_data.get('show_children')

footer_title_1 = cleaned_data.get('footer_title_1')
footer_link_1 = cleaned_data.get('footer_link_1')
footer_image_1 = cleaned_data.get('footer_image_1')
footer_title_2 = cleaned_data.get('footer_title_2')
footer_link_2 = cleaned_data.get('footer_link_2')
footer_image_2 = cleaned_data.get('footer_image_2')
footer_title_3 = cleaned_data.get('footer_title_3')
footer_link_3 = cleaned_data.get('footer_link_3')
footer_image_3 = cleaned_data.get('footer_image_3')

if not self.object and not parent:
    msg = u'No se puede crear páginas del primer nivel.'
    self.add_error('parent', msg)

if not image and not parent:
    msg = u'La página del primer nivel debe tener una imagen.'
    self.add_error('image', msg)

if show_children and not content_view:
    msg = u'Tiene que elegir la forma de mostrar a los hijos.'
    self.add_error('content_view', msg)

if not show_children and not show_content:
    msg = u'Tiene que elegir qué mostrar en la página: ' \
          u'el contenigo, los hijos o ambos.'
    self.add_error('show_content', msg)

if (
    footer_title_1 or footer_link_1 or footer_image_1 or
    footer_title_2 or footer_link_2 or footer_image_2 or
    footer_title_3 or footer_link_3 or footer_image_3
) and not (
    footer_title_1 and footer_link_1 and footer_image_1 and
    footer_title_2 and footer_link_2 and footer_image_2 and
    footer_title_3 and footer_link_3 and footer_image_3
):
    msg = u'Tiene que completar todos los campos \
          de los bloques inferiores de la página.'
    self.add_error(None, msg)

quisiera saber de que forma podria refactorizarlo? puesto que pylint me arroja alerta por tener mas de 5 condiciones.

1
  • 1
    A menos que hayas definido esos campos como blank=true, son obligatorios y no hay necesidad de hacer esas validaciones. Si es un campo personalizado, puedes inicializarlo marcándolos como footer_title_2.required = true
    – toledano
    el 3 may. 2016 a las 12:36

1 respuesta 1

2

Como te dice @toledano, lo mejor es aprovechar las facilidadesd de django para controlar el formulario.

Pero para responder a tu pregunta, es posible reducir la cadena de comprobaciones utilizando las funciones all y any quedando mucho más legible:

footers = [footer_title_1, footer_link_1, footer_image_1,
           footer_title_2, footer_link_2, footer_image_2,
           footer_title_3, footer_link_3, footer_image_3 ]

if any(footers) and not all(footers):
   ...

Aún se puede hacer mejor para evitar tener que comprobar cada elemento dos veces:

if len(footers) > sum(1 for f in footers if f) >= 1:
   ...

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.