def add_trip_from_search(request): """Add a trip from a previous search (stored in session) """ def _get_favorite_place(json, key): try: favorite_place_id = int(json.get(key).get("favoriteplace")) return FavoritePlace.objects.get(pk=favorite_place_id) except (ValueError, FavoritePlace.DoesNotExist): return None if request.method != "POST" or "trip_details" not in request.POST: return HttpResponseRedirect("carpool:add_trip") trip = Trip(user=request.user) try: json = simplejson.loads(request.POST["trip_details"]) trip.trip_type = int(json.get("type")) trip.trip_radius = int(json.get("radius")) departure_favoriteplace = _get_favorite_place(json, "departure") if departure_favoriteplace: trip.departure_city = departure_favoriteplace.city trip.departure_address = departure_favoriteplace.address trip.departure_point = departure_favoriteplace.point else: trip.departure_city = json.get("departure").get("city") trip.departure_point = GEOSGeometry(json.get("departure").get("point")) arrival_favoriteplace = _get_favorite_place(json, "arrival") if arrival_favoriteplace: trip.arrival_city = arrival_favoriteplace.city trip.arrival_address = arrival_favoriteplace.address trip.arrival_point = arrival_favoriteplace.point else: trip.arrival_city = json.get("arrival").get("city") trip.arrival_point = GEOSGeometry(json.get("arrival").get("point")) trip.interval_min = min(abs(int(json.get("interval_min"))), MAX_INTERVAL) trip.interval_max = min(abs(int(json.get("interval_max"))), MAX_INTERVAL) trip.date = get_date(json.get("date"), FRENCH_DATE_INPUT_FORMATS) form = AddModifyTripOptionsForm(initial=model_to_dict(request.user.get_profile()), instance=trip) except: return HttpResponseRedirect(reverse("carpool:add_trip")) mpoints = MultiPoint([trip.departure_point, trip.arrival_point]) response_dict = { "current_item": 10, "gmapkey": settings.GOOGLE_MAPS_API_KEY, "trip": trip, "form": form, "geometry": mpoints.envelope.wkt, "trip_from_search": True, "hours": range(24), } template = loader.get_template("carpool/add_modify_trip.html") context = RequestContext(request, response_dict) return HttpResponse(template.render(context))
def add_trip_from_search(request): """Add a trip from a previous search (stored in session) """ def _get_favorite_place(json, key): try: favorite_place_id = int(json.get(key).get('favoriteplace')) return FavoritePlace.objects.get(pk=favorite_place_id) except (ValueError, FavoritePlace.DoesNotExist): return None if request.method != 'POST' or 'trip_details' not in request.POST: return HttpResponseRedirect('carpool:add_trip') trip = Trip(user=request.user) try: json = simplejson.loads(request.POST['trip_details']) trip.trip_type = int(json.get('type')) trip.trip_radius = int(json.get('radius')) departure_favoriteplace = _get_favorite_place(json, 'departure') if departure_favoriteplace: trip.departure_city = departure_favoriteplace.city trip.departure_address = departure_favoriteplace.address trip.departure_point = departure_favoriteplace.point else: trip.departure_city = json.get('departure').get('city') trip.departure_point = GEOSGeometry(json.get('departure').get('point')) arrival_favoriteplace = _get_favorite_place(json, 'arrival') if arrival_favoriteplace: trip.arrival_city = arrival_favoriteplace.city trip.arrival_address = arrival_favoriteplace.address trip.arrival_point = arrival_favoriteplace.point else: trip.arrival_city = json.get('arrival').get('city') trip.arrival_point = GEOSGeometry(json.get('arrival').get('point')) trip.interval_min = min(abs(int(json.get('interval_min'))), MAX_INTERVAL) trip.interval_max = min(abs(int(json.get('interval_max'))), MAX_INTERVAL) trip.date = get_date(json.get('date'), FRENCH_DATE_INPUT_FORMATS) form = AddModifyTripOptionsForm(initial=model_to_dict(request.user.get_profile()), instance=trip) except: return HttpResponseRedirect(reverse('carpool:add_trip')) mpoints = MultiPoint([trip.departure_point, trip.arrival_point]) response_dict = { 'current_item': 10, 'gmapkey': settings.GOOGLE_MAPS_API_KEY, 'trip': trip, 'form': form, 'geometry': mpoints.envelope.wkt, 'trip_from_search': True, 'hours': range(24), } template = loader.get_template('carpool/add_modify_trip.html') context = RequestContext(request, response_dict) return HttpResponse(template.render(context))
def add_return_trip(request, trip_id): """Create a new back trip based on information of an existing trip """ trip = get_object_or_404(Trip, pk=trip_id, user=request.user) new_trip = Trip(user=request.user) new_trip.name = _('%(trip_name)s - Return') % {'trip_name': trip.name} new_trip.trip_type = trip.trip_type new_trip.departure_city = trip.arrival_city new_trip.departure_address = trip.arrival_address new_trip.departure_point = trip.arrival_point new_trip.arrival_city = trip.departure_city new_trip.arrival_address = trip.departure_address new_trip.arrival_point = trip.departure_point new_trip.regular = trip.regular if new_trip.regular: new_trip.dows = trip.dows new_offer = None if trip.offer: new_offer = TripOffer() new_offer.checkpointlist = trip.offer.checkpointlist new_offer.checkpointlist.reverse() new_offer.radius = trip.offer.radius new_trip.offer = new_offer new_demand = None if trip.demand: new_demand = TripDemand() new_demand.radius = trip.demand.radius new_trip.demand = new_demand userprofiledata = model_to_dict(request.user.get_profile()) form_trip = EditTripForm(initial=userprofiledata, instance=new_trip) form_offer = EditTripOfferOptionsForm(initial=userprofiledata, instance=new_offer, prefix='offer') form_demand = EditTripDemandOptionsForm(initial=userprofiledata, instance=new_demand, prefix='demand') points = [cp['point'] for cp in trip.offer.checkpointlist] if trip.offer else [] points.append(trip.departure_point) points.append(trip.arrival_point) mpoints = MultiPoint(points) response_dict = { 'form_trip': form_trip, 'form_offer_options': form_offer, 'form_demand_options': form_demand, 'default_center': settings.DEFAULT_MAP_CENTER_POINT, 'default_zoom': settings.DEFAULT_MAP_CENTER_ZOOM, 'return_trip': True, } template = loader.get_template('carpool/add_modify_trip.html') context = RequestContext(request, response_dict) return HttpResponse(template.render(context))
def add_return_trip(request, trip_id): """Create a new back trip based on information of an existing trip """ trip = get_object_or_404(Trip, pk=trip_id, user=request.user) new_trip = Trip(user=request.user) new_trip.name = _("%(trip_name)s - Return") % {"trip_name": trip.name} new_trip.trip_type = trip.trip_type new_trip.departure_city = trip.arrival_city new_trip.departure_address = trip.arrival_address new_trip.departure_point = trip.arrival_point new_trip.arrival_city = trip.departure_city new_trip.arrival_address = trip.departure_address new_trip.arrival_point = trip.departure_point new_trip.regular = trip.regular if new_trip.regular: new_trip.dows = trip.dows new_offer = None if trip.offer: new_offer = TripOffer() new_offer.checkpointlist = trip.offer.checkpointlist new_offer.checkpointlist.reverse() new_offer.radius = trip.offer.radius new_trip.offer = new_offer new_demand = None if trip.demand: new_demand = TripDemand() new_demand.radius = trip.demand.radius new_trip.demand = new_demand userprofiledata = model_to_dict(request.user.get_profile()) form_trip = EditTripForm(initial=userprofiledata, instance=new_trip) form_offer = EditTripOfferOptionsForm(initial=userprofiledata, instance=new_offer, prefix="offer") form_demand = EditTripDemandOptionsForm(initial=userprofiledata, instance=new_demand, prefix="demand") points = [cp["point"] for cp in trip.offer.checkpointlist] if trip.offer else [] points.append(trip.departure_point) points.append(trip.arrival_point) mpoints = MultiPoint(points) response_dict = { "form_trip": form_trip, "form_offer_options": form_offer, "form_demand_options": form_demand, "default_center": settings.DEFAULT_MAP_CENTER_POINT, "default_zoom": settings.DEFAULT_MAP_CENTER_ZOOM, "return_trip": True, } template = loader.get_template("carpool/add_modify_trip.html") context = RequestContext(request, response_dict) return HttpResponse(template.render(context))