def area_summary(request): """ Area summary view. """ if 'data' in request.session: data = request.session['data'] # Get the vehicle instance from the database. v = data['vehicle'] vehicle = queries.get_vehicle(v['text']['year'], v['obj']['make'], v['obj']['model']) # Get the trim to send to the deals query. form = TrimForm(request.GET, model_pk=v['obj']['model'], trim_year=v['text']['year']) trim = None if 'trim' in request.GET and form.is_valid(): trim = form.cleaned_data['trim'] trim = trim if trim and int(trim) > 0 else None # Look up the dealers from the places. dealer_list, deal_count, area_avg = [], 0, 0 places = () if 'results' in data['places']: places = data['places']['results'] for p in places: dealer = queries.get_dealer(p['id']) if dealer: # Look up the deals from the vehicle and dealer instances. deals = queries.get_deals(vehicle, dealer, trim) deal_count += len(deals) dealer_sum = get_deals_sum(deals) area_avg += dealer_sum # Add dealer and average to data if deals were found. if deals: dealer_avg = dealer_sum / len(deals) dealer_list.append({'obj': dealer, 'avg': dealer_avg}) # Set up pagination. dealers = get_page(request, dealer_list, 5) places_for_js = [{'location': d['obj'].location, 'id': d['obj'].place_id, 'name': d['obj'].name} for d in dealers.object_list] # Set up template variables. data['area_avg'] = area_avg / (deal_count if deal_count > 0 else 1) data['dealers'] = dealers data['form'] = form data['form_action'] = 'area_summary' data['trim'] = trim data['places_json'] = json.dumps(places_for_js) request.session['data'] = data return _rtr('area_summary.html', request.session['data'], context_instance=RequestContext(request)) raise Http404
def dealer_deals(request, place_id): """ Dealer deals view. """ dealer = queries.get_dealer(place_id) if 'data' in request.session and dealer: data = request.session['data'] # Get the vehicle instance from the database. v = data['vehicle'] vehicle = queries.get_vehicle(v['text']['year'], v['obj']['make'], v['obj']['model']) if not vehicle: raise Http404 # Get the trim to send to the deals query. trim = None # Try for the trim specified in the form. form = TrimForm(request.GET, model_pk=v['obj']['model'], trim_year=v['text']['year']) # Fall back on the trim specified in the session. if 'trim' not in request.GET and 'trim' in data: form = TrimForm(data, model_pk=v['obj']['model'], trim_year=v['text']['year']) if form.is_valid(): trim = form.cleaned_data['trim'] trim = trim if trim and int(trim) > 0 else None # Look up the deals from the vehicle and dealer instances. deals = queries.get_deals(vehicle, dealer, trim) data['dealer'] = dealer data['deals'] = deals data['dealer_avg'] = get_deals_sum(deals) / (len(deals) if len(deals) > 0 else 1) data['form'] = form data['form_action'] = 'dealer_deals' place_data = util.get_dict(data['places']['results'], 'id', place_id) data['place_json'] = json.dumps(place_data) request.session['data'] = data return _rtr('dealer_deals.html', request.session['data'], context_instance=RequestContext(request)) raise Http404