Home > Software design >  Checking conditions with a function django views
Checking conditions with a function django views

Time:04-10

I am trying to make my code more readable and less verbose. I have this long view, where I have some if and elif statement to check some conditions. What I am trying to do is to write this function into another file (utils.py) and use this function in the views. The view is something like that: views.py

        if float(ata_tacho) <= float(etd_tacho):
            messages.error(request, "ATA Tachometer cannot be greater or equal to ETD Tachometer")
            return HttpResponseRedirect(reverse('flight:add_new_flight'))
        elif ata <= etd:
            messages.error(request, "ATA cannot be greater or equal to ETD!")
            return HttpResponseRedirect(reverse('flight:add_new_flight'))

        elif function_type_obj.name == 'Dual Command' and not instructor:
            messages.error(request, "Instructor must be on Board!")
            return HttpResponseRedirect(reverse('flight:add_new_flight'))
More code to run only if conditions above are satisfied

These are some conditions I need to check to continue running the code. So far, so good. If I try to write this function in a more general one in utils.py file like below:

utils.py

def flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type, instructor):
    if float(ata_tacho) <= float(etd_tacho):
         messages.error(request, "ATA Tachometer cannot be greater or equal to ETD Tachometer")
         return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    elif ata <= etd:
        messages.error(request, "ATA cannot be greater or equal to ETD!")
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

    elif function_type == 'Dual Command' and not instructor:
        messages.error(request, "Instructor must be on Board!")
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

And I call the function in the view like this:

        flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type_obj.name, instructor)

What happens is that basically this piece of code works but DOES NOT stop the rest of the view to keep its job done. I mean, it returns the error messages but doesn't stop the rest of the view. I think I am probably missing some return or I don't know what I am doing wrong.

CodePudding user response:

Indeed, if you just call the flight_validator that way, its return value is just ignored.

A quick fix could be:

views.py:

redirection = flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type_obj.name, instructor)

if redirection is not None:
    return redirection

Alternative 1:

But this is probably a best practice to keep the HTTP response initialized in the views.

utils.py:

def flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type, instructor):
    if float(ata_tacho) <= float(etd_tacho):
        messages.error(request, "ATA Tachometer cannot be greater or equal to ETD Tachometer")
        return False
    elif ata <= etd:
        messages.error(request, "ATA cannot be greater or equal to ETD!")
        return False
    elif function_type == 'Dual Command' and not instructor:
        messages.error(request, "Instructor must be on Board!")
        return False

    return True

views.py:

is_valid_flight = flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type_obj.name, instructor)

if not is_valid_flight:
    return HttpResponseRedirect(reverse('flight:add_new_flight'))

Alternative 2:

That been said, you could even factor a bit more:

utils.py:

def flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type, instructor):
    if float(ata_tacho) <= float(etd_tacho):
        return False, "ATA Tachometer cannot be greater or equal to ETD Tachometer"
    elif ata <= etd:
        return False, "ATA cannot be greater or equal to ETD!"
    elif function_type == 'Dual Command' and not instructor:
        return False, "Instructor must be on Board!"

    return True, ""

views.py:

is_valid_flight, error_message = flight_validator(request, ata_tacho, etd_tacho, ata, etd, function_type_obj.name, instructor)

if not is_valid_flight:
    messages.error(request, error_message)
    return HttpResponseRedirect(reverse('flight:add_new_flight'))
  • Related