Example #1
0
def show_trip_results(request, trip_id=None, lib=None):
    """Display information about the given trip and provides way to query the API
    to have information about matching trips.

    """
    userLib = LibUsers(**lib.get_params())
    user = userLib.get_active_user()
    trip = lib.get_trip(trip_id)

    if user.id != trip.user.id:
        raise Http404()

    if request.POST:
        dict = {}
        trip_details = simplejson.loads(request.POST['trip_details'])
        trip = lib.edit_trip(trip_id, **trip_details)

    return render_to_response('show_trip_results.html', {
        'gmapkey': settings.GOOGLE_MAPS_API_KEY,
        'trip': trip,
        'default_zoom': settings.DEFAULT_MAP_CENTER_ZOOM, 
        'default_center': settings.DEFAULT_MAP_CENTER_POINT,
        'is_trip': True,
        'page_url': request.build_absolute_uri(),
    }, context_instance=RequestContext(request))
Example #2
0
def list_mine(request, page=1, lib=None):
    """List all trips for the current user.
    
    """
    userlib = LibUsers(**lib.get_params())
    count = lib.count_user_trips()
    items_per_page = getattr(settings, 'TRIPS_PER_PAGE', DEFAULT_ITEMS_PER_PAGE)
    return render_to_response('my_trips.html', {
        'user': userlib.get_active_user(),
        'trips': lib.list_user_trips(page, items_per_page), 
        'count': count,
        'page': int(page),
        'listpages': compute_nb_pages(count, items_per_page),
        'is_trip': True,
    }, context_instance=RequestContext(request))
Example #3
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))