def deal_entry(request, place_id): """ Deal entry view. """ if 'data' in request.session: # Verify that the passed-in place is in the session. place_data = util.get_dict( request.session['data']['places']['results'], 'id', place_id) if not place_data: raise Http404 data = {'vehicle': request.session['data']['vehicle'], 'dealer': place_data} model_pk = data['vehicle']['obj']['model'] trim_year = data['vehicle']['text']['year'] data['place_json'] = json.dumps(place_data) if request.method == 'POST': form = EntryForm(request.POST, model_pk=model_pk, trim_year=trim_year, label_suffix='') if form.is_valid(): user_ip = queries.store_user_ip(request.META, form.cleaned_data) vehicle = queries.store_vehicle(data['vehicle']) dealer = queries.store_dealer(place_data) deal = queries.store_deal(user_ip, vehicle, dealer, form.cleaned_data) data['deal'] = deal request.session['data'] = data return redirect(deal_entered) else: form = EntryForm(initial={'date': date.today()}, model_pk=model_pk, trim_year=trim_year, label_suffix='') data['form'] = form return _rtr('deal_entry.html', 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