def ajax_home_update_selections(request): """ Home screen vehicle selection Ajax view. Returns HttpResponse or raises Http404. """ if request.is_ajax(): # These might be 'Year', 'Make', or 'Model'. make_year = request.GET['make_year'] make = request.GET['make'] model = request.GET['model'] selected = request.GET['selected'] data = {} options = None if selected == 'make_year' and make_year.isdigit(): options = queries.get_make_options(make_year) data['make'] = render_to_string('home/update_select.html', {'options': options, 'name': 'Make'}) options = None if selected == 'make' and make.isdigit(): options = queries.get_model_options(make, make_year) data['model'] = render_to_string('home/update_select.html', {'options': options, 'name': 'Model'}) return HttpResponse(json.dumps(data), content_type='application/json') raise Http404
def build_vehicle_options(get_data, form): """ Home view helper function. Update the vehicle options on the form from the request GET data. Note that the form is being directly modified here. Nothing returned. """ make_year = get_data.get('make_year', 0) make = get_data.get('make', 0) model = get_data.get('model', 0) form.fields['make'].choices = ( [(m.id, m.name) for m in queries.get_make_options(make_year)]) form.fields['make'].choices.insert(0, (0, 'Make')) form.fields['make'].initial = make form.fields['model'].choices = ( [(m.id, m.name) for m in queries.get_model_options(make, make_year)]) form.fields['model'].choices.insert(0, (0, 'Model')) form.fields['model'].initial = model