예제 #1
0
def display_matching_trips(request, trip_id=None, lib=None):
    """Make a request to the BV server to find matching trips. Format the 
    output to be read by javascript clientside code.
    
    """
    def to_json(trip):
        return [get_trip_dict(t) for t in trips]

    trip_search_type = int(request.POST['trip_type'])
    results = lib.search_trip(trip_id=trip_id, **unicode_to_dict(request.POST))
    trip_demands = results['trip_demands']
    trip_offers = results['trip_offers']
    trip = results['trip']
    
    if trip_search_type == TRIP_OFFER:
        trips = trip_demands
    else:
        trips = trip_offers
    
    response_dict = {
        'authenticated': is_bvoauth_authenticated(request),
    }
    if not trip_id:
        response_dict['trips'] = to_json(trips)
    else:
        response_dict['trip_demands'] = to_json(trip_demands)
        response_dict['trip_offers'] = to_json(trip_offers)
    resp = HttpResponse()
    simplejson.dump(response_dict , resp, ensure_ascii=False, separators=(',',':'))
    return resp
예제 #2
0
def calculate_buffer(request, lib):
    response_dict = lib.calculate_buffer(
        unicode_to_dict(dict(request.REQUEST.items())))

    resp = HttpResponse()
    simplejson.dump(response_dict, 
        resp, ensure_ascii=False, separators=(',',':'))

    return resp
예제 #3
0
def ogcserver(request, lib):
    return HttpResponse(lib.ogcserver(
        unicode_to_dict(dict(request.REQUEST.items()))), content_type="image/png")
예제 #4
0
def create_trip(request, trip_id=None, trip_from_search=False, lib=None):
    """Creates/Edit a trip.
    
    If informations provided in request.POST, try to send them throught the API, 
    and display eventual errors or redirect to the search view.
    
    If no informations provided in request.POST, just display the create trip
    form.
    
    """
    userlib = LibUsers(**lib.get_params())
    userprofiledata = userlib.get_active_user().to_dict()
    cartypes = [(c.id, c.name) for c in lib.get_cartypes()]
    
    if request.method == 'POST':
        if trip_id:
            #edition of an existing trip.
            trip = lib.get_trip(trip_id)
            form = EditTripForm(data=request.POST)
            form_offer = EditTripOfferOptionsForm(data=request.POST, 
                prefix="offer", cartype_choices=cartypes)
            form_demand = EditTripDemandOptionsForm(data=request.POST, 
                prefix="demand", cartype_choices=cartypes)
        else:
            # creation of a new trip.
            form = EditTripForm(data=request.POST)
            form_offer = EditTripOfferOptionsForm(prefix="offer",
                cartype_choices=cartypes,
                initial=userprofiledata, 
            )

            form_demand = EditTripDemandOptionsForm(prefix="demand", 
                cartype_choices =cartypes,
                initial=userprofiledata,
            )

            trip_type = int(request.POST['trip_type']) 
            if trip_type == TRIP_OFFER or trip_type == TRIP_BOTH:
                form_offer = EditTripOfferOptionsForm(
                    prefix="offer", 
                    data=request.POST,
                    cartype_choices=cartypes
                )
            if trip_type == TRIP_DEMAND or trip_type == TRIP_BOTH:
                form_demand = EditTripDemandOptionsForm(
                    prefix="demand", 
                    data=request.POST,
                    cartype_choices=cartypes
                )
        error = False
        if form.is_valid() and not trip_from_search:
            trip_type = int(form['trip_type'].data)
            
            if trip_type != TRIP_DEMAND : 
                if not form_offer.is_valid():
                    error = True
    
            if trip_type != TRIP_OFFER:
                if not form_demand.is_valid():
                    error = True
    
            if error == False:
                finaldict = {}
                for f in (form, form_offer, form_demand):
                    if hasattr(f, 'cleaned_data'):
                        for key,value in f.cleaned_data.items():
                            finaldict.setdefault(key, value)
                if trip_id:
                    lib.edit_trip(trip_id=trip_id, **unicode_to_dict(dict(request.POST.items())))
                else:
                    trip = lib.add_trip(**unicode_to_dict(dict(request.POST.items())))
                if request.POST['return_trip'] == 'true':
                    return redirect(reverse('trips:create_return_trip', args=[trip.id]))
                else:
                    return redirect(reverse('trips:show_results', args=[trip.id]))
    else:
        if trip_id:
            #edition of an existing trip.
            trip = lib.get_trip(trip_id)
            form = EditTripForm(initial=trip)
            offer_initial = getattr(trip, 'offer',None)
            demand_initial = getattr(trip, 'demand', None)
            form_offer = EditTripOfferOptionsForm(initial=offer_initial, 
                prefix="offer", cartype_choices=cartypes)
            form_demand = EditTripDemandOptionsForm(initial=demand_initial, 
                prefix="demand", cartype_choices=cartypes)
        else:
            form = EditTripForm()
            form_offer = EditTripOfferOptionsForm(
                initial=userprofiledata, 
                prefix="offer", 
                cartype_choices=cartypes
            )
            form_demand = EditTripDemandOptionsForm(
                initial=userprofiledata, 
                prefix="demand",
                cartype_choices=cartypes
            )
    view_dict = {
        'form':form,
        'trip_from_search': trip_from_search,
        'form_offer_options': form_offer,
        'form_demand_options': form_demand,
        'default_zoom': settings.DEFAULT_MAP_CENTER_ZOOM, 
        'default_center': settings.DEFAULT_MAP_CENTER_POINT,
        'is_trip': True,
        'gmapkey': settings.GOOGLE_MAPS_API_KEY,
    }
    if trip_id:
        view_dict['trip'] = trip

    return render_to_response('add_trip.html', view_dict, context_instance=RequestContext(request))