1
\$\begingroup\$

I am quite new in django forms. I am woking on problem where in function name generate_outage_form passed two parameters name settings and segment Now I have to make function less verbose and more robust.

def generate_outage_form(settings, segment=None):
    form = None
    datacenters = settings.get('datacenters')
    datacenters = get_and_sort(datacenters, 'abbreviation')

    departments = settings.get('departments')
    departments = get_and_sort(departments, 'name')

    outage_types = settings.get('outage_types', [])
    outage_causes = settings.get('outage_causes', [])
    outage_impacts = settings.get('outage_impacts', [])

    # Put a check to determine what form should be shown
    if not segment:
        if session.get('division'):
            dc_search = re.search(r'global dc', session.get('division'), re.I)
            ent_search = re.search(r'support', session.get('division'), re.I)
            if dc_search:
                form = forms.RecordDCOutage()
                segment = 'datacenter'
            if ent_search:
                form = forms.RecordSupportOutage()
                segment = 'support'
            if not form:
                form = forms.RecordDCOutage()
                segment = 'datacenter'
        else:
            form = forms.RecordSupportOutage()
            segment = 'support'
    else:
        if segment == 'datacenter':
            form = forms.RecordDCOutage()
        elif segment == 'support':
            form = forms.RecordSupportOutage()
        else:
            form = forms.RecordSupportOutage()
            segment = 'support'

    form.outage_type.choices = [
        (
            i_type.get('name'),
            i_type.get('name')
        ) for i_type in outage_types if (
            i_type.get('active') and i_type.get(segment)
        )
    ]
    form.outage_type.choices.insert(0, ('', ''))
    form.outage_cause.choices = [
        (
            cause.get('name'),
            cause.get('name')
        ) for cause in outage_causes if (
            cause.get('active') and cause.get(segment)
        )
    ]
    form.outage_cause.choices.insert(0, ('', ''))
    form.outage_impact.choices = [
        (
            impact.get('name'),
            impact.get('name')
        ) for impact in outage_impacts if (
            impact.get('active') and impact.get(segment)
        )
    ]
    form.outage_impact.choices.insert(0, ('', ''))
    form.data_center.choices = [
        (
            dc.get('abbreviation'),
            '%s - %s' % (
                dc.get('abbreviation'),
                dc.get('name')
            )
        ) for dc in datacenters if dc.get('active')
    ]
    form.data_center.choices.insert(0, ('', ''))
    form.departments.choices = [
        (
            dept.get('name'),
            dept.get('name')
        ) for dept in departments if (
            dept.get('active') and dept.get(segment)
        )
    ]

    return form, segment
\$\endgroup\$
  • 3
    \$\begingroup\$ Telling what the code does helps us write better reviews. Right now there isn't enough information about what the code does. That information should be in the title and the first paragraph. Please see How to ask a good question. \$\endgroup\$ – pacmaninbw Sep 25 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.