captnvitman captnvitman -4 years ago 153
Ajax Question

More efficient way of writing this function? (Django, AJAX)

The way I have written the function works now, I'm just wondering if there is a better or more efficient way to write it. It is an ajax call that gets a list for further filtering a geography, for example if county is chosen then it will get a list of all the counties in that state.
I'm still learning coding and want to get better at using good coding practices.

$.ajax({
url : "filters",
type : "POST",
data : { search : selected_state, geog : selected_geog },
success: function(data) {
$.each(data, function(i, v) {
switch(selected_geog) {
case 'County':
case 'Dma':
filters.push(v.fields.name);
break;
case 'Zip':
var exists = $.inArray(v.fields.zcta, data_check);
if (exists < 0) {
data_check.push(v.fields.zcta);
}
data_check.sort();
var exists2 = $.inArray(v.fields.county, filters);
if (exists2 < 0) {
filters.push(v.fields.county);
}
filters.sort();
break;
case 'CD':
filters.push(v.fields.cd114fp);
break;
case 'Zones':
var exists = $.inArray(v.fields.p_dma, filters);
if (exists < 0) {
filters.push(v.fields.p_dma);
}
filters.sort();
var exists2 = $.inArray(v.fields.owner, attrfilters);
if (exists2 < 0) {
attrfilters.push(v.fields.owner);
}
SHPconverter.attrfilters.sort();
var exists3 = $.inArray(v.fields.scode, data_check);
if (exists3 < 0) {
data_check.push(v.fields.scode);
}
data_check.sort();
break;
}
})
}
})


and here is the django view

def filters(request):
searchVar = request.POST.getlist('search[]')
geogVar = request.POST.get('geog')

if geogVar == 'County':
county_as_json = serialize(
'json', County.objects.filter(
state__in=searchVar).order_by('state', 'name'))
return HttpResponse(county_as_json, content_type='json')

elif geogVar == 'DMA':
dma_as_json = serialize(
'json', Dma.objects.filter(
state__in=searchVar).order_by('state', 'name'))
return HttpResponse(dma_as_json, content_type='json')

elif geogVar == 'CD':
cd_as_json = serialize(
'json', Cd.objects.filter(
state__in=searchVar).order_by('state', 'cd'))
return HttpResponse(cd_as_json, content_type='json')

elif geogVar == 'Zip':
zip_as_json = serialize(
'json', Zip.objects.filter(
state__in=searchVar).order_by('state', 'county'))
return HttpResponse(zip_as_json, content_type='json')

elif geogVar == 'Zones':
zones_as_json = serialize(
'json', Zones.objects.filter(
state__in=searchVar).order_by('sname'))
return HttpResponse(softzones_as_json, content_type='json')

Answer Source

You can reduce the entire if statement section by creating a dictionary lookup with a tuple as values

geo_lookup = {'County': (County, ['state', 'name']),
                  'DMA': (Dma, ['state', 'name']),
                  'CD': (Cd, ['state', 'cd']),
                  'Zip': (Zip, ['state', 'county']),  
                  'Zones': (Zones, ['sname']),
                  }

lookup_model = geo_lookup[geogVar][0]   
ordering = geo_lookup[geogVar][1]
as_json = serialize('json', lookup_model.objects.filter(state__in=searchVar).order_by(*ordering))
return HttpResponse(as_json, content_type='json')

You also seem to be trying to serialize models, something which Django Rest Framework specializes in, so just using that instead would stop the requirement for you to manually do all this in a view.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download