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 iframe_form(request, theme, format, date=None, departure_slug=None, departure_zip=None, arrival_slug=None, arrival_zip=None): """Display the content of the Ifram of type "form"/ Checks that the theme and the format matches. Simple form: departuire, arrival, date (if there is place to display it), 2 buttons (seeking for conductor or for passenger) """ if theme not in IFRAME_THEMES: raise Http404 if format not in IFRAME_FORMATS: raise Http404 # get date and cities departure, arrival = None, None if date != '00-00-0000': date = get_date(date, FRENCH_DATE_INPUT_FORMATS) else: date = None if departure_slug: try: departure = City.objects.get_from_slug(departure_slug, int(departure_zip)) except City.DoesNotExist: departure = None if arrival_slug: try: arrival = City.objects.get_from_slug(arrival_slug, int(arrival_zip)) except City.DoesNotExist: arrival = None return home(request, layout='iframe', theme_id=theme, theme_dict=IFRAME_THEMES[theme], format_id=format, format_dict=IFRAME_FORMATS[format], date=date, departure=departure, arrival=arrival)
def iframe_form( request, theme, format, date=None, departure_slug=None, departure_zip=None, arrival_slug=None, arrival_zip=None ): """Display the content of the Ifram of type "form"/ Checks that the theme and the format matches. Simple form: departuire, arrival, date (if there is place to display it), 2 buttons (seeking for conductor or for passenger) """ if theme not in IFRAME_THEMES: raise Http404 if format not in IFRAME_FORMATS: raise Http404 # get date and cities departure, arrival = None, None if date != "00-00-0000": date = get_date(date, FRENCH_DATE_INPUT_FORMATS) else: date = None if departure_slug: try: departure = City.objects.get_from_slug(departure_slug, int(departure_zip)) except City.DoesNotExist: departure = None if arrival_slug: try: arrival = City.objects.get_from_slug(arrival_slug, int(arrival_zip)) except City.DoesNotExist: arrival = None return home( request, layout="iframe", theme_id=theme, theme_dict=IFRAME_THEMES[theme], format_id=format, format_dict=IFRAME_FORMATS[format], date=date, departure=departure, arrival=arrival, )
def home( request, layout=None, theme_id=None, theme_dict=None, format_id=None, format_dict=None, media_specific=None, date=None, departure=None, arrival=None, ): """Home. This view is acessible via GET and POST. On GET, it displays a form for selecting a trip On POST, check if the form is valid, and redirect to other views """ def get_dates_available(dates): """Filter dates from a set of given dates filter dates in past """ today = datetime.date.today() return [date for date in dates if date[0] >= today] if media_specific is None: media_specific = "" response_dict = { "current_item": 1, "gmapkey": settings.GOOGLE_MAPS_API_KEY, "theme_id": theme_id, "theme_dict": theme_dict, "format_id": format_id, "format_dict": format_dict, "departure": departure, "arrival": arrival, } today = datetime.date.today() dates_available = () search_trip_details = request.session.get("search_trip_details", None) if search_trip_details and not theme_id: date = get_date(search_trip_details["date"], FRENCH_DATE_INPUT_FORMATS) if date is not None and dates_available: # check if date in session is in dates_available, else it would print an error if date not in dates_available: date = None initial = { "departure": search_trip_details["departure"]["name"], "departure_point": search_trip_details["departure"]["point"], "departure_favoriteplace": search_trip_details["departure"]["favoriteplace"] if "favoriteplace" in search_trip_details["departure"] else None, "arrival": search_trip_details["arrival"]["name"], "arrival_point": search_trip_details["arrival"]["point"], "arrival_favoriteplace": search_trip_details["arrival"]["favoriteplace"] if "favoriteplace" in search_trip_details["arrival"] else None, "date": date, "type": Trip.OFFER, } elif theme_id: initial = {"date": date, "departure": departure, "arrival": arrival} else: initial = {"date": today} if request.method == "POST": if dates_available: form = SearchTripWithDatesForm(dates_available, data=request.POST) else: form = SearchTripForm(data=request.POST) if form.is_valid(): try: trip_type = int(form.cleaned_data["type"]) except: trip_type = Trip.OFFER search_trip_details = { "departure": { "name": form.cleaned_data["departure"], "point": form.cleaned_data["departure_point"], "favoriteplace": form.cleaned_data["departure_favoriteplace"], }, "arrival": { "name": form.cleaned_data["arrival"], "point": form.cleaned_data["arrival_point"], "favoriteplace": form.cleaned_data["arrival_favoriteplace"], }, "date": form.cleaned_data["date"].strftime("%d/%m/%Y") if form.cleaned_data["date"] else today.strftime("%d/%m/%Y"), } request.session["search_trip_details"] = search_trip_details match_departure = R_CITY_ZIP.match(str_slugify(form.cleaned_data["departure"])) match_arrival = R_CITY_ZIP.match(str_slugify(form.cleaned_data["arrival"])) args = None if match_departure and match_arrival: args = [ match_departure.group(1), match_departure.group(2), match_arrival.group(1), match_arrival.group(2), ] if trip_type == Trip.OFFER: if args: return HttpResponseRedirect(reverse("carpool:robots_search_offer_trip", args=args)) else: return HttpResponseRedirect(reverse("carpool:search_offer_trip")) else: if args: return HttpResponseRedirect(reverse("carpool:robots_search_demand_trip", args=args)) else: return HttpResponseRedirect(reverse("carpool:search_demand_trip")) elif request.method == "GET": if dates_available: form = SearchTripWithDatesForm(dates_available, initial=data) else: initial_data = {"date": today} form = SearchTripForm(initial=initial_data) response_dict.update({"form": form, "date_uptodate": dates_available}) # theming if layout in _LAYOUTS: template = loader.get_template("carpool/tools/%s_form.html" % layout) else: template = loader.get_template("carpool/home.html") context = RequestContext(request, response_dict) return HttpResponse(template.render(context))
def home(request): response_dict = { "current_item": 1, "gmapkey": settings.GOOGLE_MAPS_API_KEY, "places": FavoritePlace.objects.all().order_by("name"), } today = datetime.date.today() search_trip_details = request.session.get("search_trip_details", None) if search_trip_details: date = get_date(search_trip_details["date"], FRENCH_DATE_INPUT_FORMATS) if date < today: date = today data = { "departure": search_trip_details["departure"]["name"], "departure_point": search_trip_details["departure"]["point"], "departure_favoriteplace": search_trip_details["departure"]["favoriteplace"] if "favoriteplace" in search_trip_details["departure"] else None, "arrival": search_trip_details["arrival"]["name"], "arrival_point": search_trip_details["arrival"]["point"], "arrival_favoriteplace": search_trip_details["arrival"]["favoriteplace"] if "favoriteplace" in search_trip_details["arrival"] else None, "date": date, "type": Trip.OFFER, } else: data = None dates_concert = get_dates_concert() if request.method == "POST": if dates_concert: form = SearchTripRmll2009Form(request.POST) else: form = SearchTripForm(request.POST) if form.is_valid(): try: trip_type = int(form.cleaned_data["type"]) except: trip_type = Trip.OFFER search_trip_details = { "departure": { "name": form.cleaned_data["departure"], "point": form.cleaned_data["departure_point"], "favoriteplace": form.cleaned_data["departure_favoriteplace"], }, "arrival": { "name": form.cleaned_data["arrival"], "point": form.cleaned_data["arrival_point"], "favoriteplace": form.cleaned_data["arrival_favoriteplace"], }, "date": form.cleaned_data["date"].strftime("%d/%m/%Y") if form.cleaned_data["date"] else date_default.strftime("%d/%m/%Y"), } request.session["search_trip_details"] = search_trip_details match_departure = R_CITY_ZIP.match(str_slugify(form.cleaned_data["departure"])) match_arrival = R_CITY_ZIP.match(str_slugify(form.cleaned_data["arrival"])) args = None if match_departure and match_arrival: args = [ match_departure.group(1), match_departure.group(2), match_arrival.group(1), match_arrival.group(2), ] if trip_type == Trip.OFFER: if args: return HttpResponseRedirect(reverse("carpool:robots_search_offer_trip", args=args)) else: return HttpResponseRedirect(reverse("carpool:search_offer_trip")) else: if args: return HttpResponseRedirect(reverse("carpool:robots_search_demand_trip", args=args)) else: return HttpResponseRedirect(reverse("carpool:search_demand_trip")) else: if dates_concert: form = SearchTripRmll2009Form(data) else: initial_data = {"date": today} form = SearchTripForm(data, initial=initial_data) response_dict.update({"form": form, "date_uptodate": dates_concert, "cities_concert": _CITIES_CONCERT}) template = loader.get_template("carpool/home_rmll2009.html") context = RequestContext(request, response_dict) return HttpResponse(template.render(context))
def home(request, layout=None, theme_id=None, theme_dict=None, format_id=None, \ format_dict=None, media_specific=None, date=None, departure=None, \ arrival=None): """Home. This view is acessible via GET and POST. On GET, it displays a form for selecting a trip On POST, check if the form is valid, and redirect to other views """ def get_dates_available(dates): """Filter dates from a set of given dates filter dates in past """ today = datetime.date.today() return [date for date in dates if date[0] >= today] if media_specific is None: media_specific = '' response_dict = { 'current_item': 1, 'gmapkey': settings.GOOGLE_MAPS_API_KEY, 'theme_id': theme_id, 'theme_dict': theme_dict, 'format_id': format_id, 'format_dict': format_dict, 'departure': departure, 'arrival': arrival, } today = datetime.date.today() dates_available = () search_trip_details = request.session.get('search_trip_details', None) if search_trip_details and not theme_id: date = get_date(search_trip_details['date'], FRENCH_DATE_INPUT_FORMATS) if date is not None and dates_available: # check if date in session is in dates_available, else it would print an error if date not in dates_available: date = None initial = { 'departure': search_trip_details['departure']['name'], 'departure_point': search_trip_details['departure']['point'], 'departure_favoriteplace': search_trip_details['departure']['favoriteplace'] if 'favoriteplace' in search_trip_details['departure'] else None, 'arrival': search_trip_details['arrival']['name'], 'arrival_point': search_trip_details['arrival']['point'], 'arrival_favoriteplace': search_trip_details['arrival']['favoriteplace'] if 'favoriteplace' in search_trip_details['arrival'] else None, 'date': date, 'type': Trip.OFFER, } elif theme_id: initial = { 'date': date, 'departure': departure, 'arrival': arrival, } else: initial = {'date': today} if request.method == 'POST': if dates_available: form = SearchTripWithDatesForm(dates_available, data=request.POST) else: form = SearchTripForm(data=request.POST) if form.is_valid(): try: trip_type = int(form.cleaned_data['type']) except: trip_type = Trip.OFFER search_trip_details = { 'departure': {'name': form.cleaned_data['departure'], 'point': form.cleaned_data['departure_point'], 'favoriteplace': form.cleaned_data['departure_favoriteplace']}, 'arrival': {'name': form.cleaned_data['arrival'], 'point': form.cleaned_data['arrival_point'], 'favoriteplace': form.cleaned_data['arrival_favoriteplace']}, 'date': form.cleaned_data['date'].strftime("%d/%m/%Y") if form.cleaned_data['date'] else today.strftime("%d/%m/%Y") } request.session['search_trip_details'] = search_trip_details match_departure = R_CITY_ZIP.match(str_slugify(form.cleaned_data['departure'])) match_arrival = R_CITY_ZIP.match(str_slugify(form.cleaned_data['arrival'])) args = None if match_departure and match_arrival: args = [match_departure.group(1), match_departure.group(2), match_arrival.group(1), match_arrival.group(2)] if trip_type == Trip.OFFER: if args: return HttpResponseRedirect(reverse('carpool:robots_search_offer_trip', args=args)) else: return HttpResponseRedirect(reverse('carpool:search_offer_trip')) else: if args: return HttpResponseRedirect(reverse('carpool:robots_search_demand_trip', args=args)) else: return HttpResponseRedirect(reverse('carpool:search_demand_trip')) elif request.method == "GET": if dates_available: form = SearchTripWithDatesForm(dates_available, initial=data) else: initial_data = {'date': today} form = SearchTripForm(initial=initial_data) response_dict.update({ 'form': form, 'date_uptodate': dates_available, }) # theming if layout in _LAYOUTS: template = loader.get_template('carpool/tools/%s_form.html' % layout) else: template = loader.get_template('carpool/home.html') context = RequestContext(request, response_dict) return HttpResponse(template.render(context))
def home(request): response_dict = { 'current_item': 1, 'gmapkey': settings.GOOGLE_MAPS_API_KEY, 'places': FavoritePlace.objects.all().order_by('name'), } today = datetime.date.today() search_trip_details = request.session.get('search_trip_details', None) if search_trip_details: date = get_date(search_trip_details['date'], FRENCH_DATE_INPUT_FORMATS) if date < today: date = today data = { 'departure': search_trip_details['departure']['name'], 'departure_point': search_trip_details['departure']['point'], 'departure_favoriteplace': search_trip_details['departure']['favoriteplace'] if 'favoriteplace' in search_trip_details['departure'] else None, 'arrival': search_trip_details['arrival']['name'], 'arrival_point': search_trip_details['arrival']['point'], 'arrival_favoriteplace': search_trip_details['arrival']['favoriteplace'] if 'favoriteplace' in search_trip_details['arrival'] else None, 'date': date, 'type': Trip.OFFER } else: data = None dates_concert = get_dates_concert() if request.method == 'POST': if dates_concert: form = SearchTripRmll2009Form(request.POST) else: form = SearchTripForm(request.POST) if form.is_valid(): try: trip_type = int(form.cleaned_data['type']) except: trip_type = Trip.OFFER search_trip_details = { 'departure': { 'name': form.cleaned_data['departure'], 'point': form.cleaned_data['departure_point'], 'favoriteplace': form.cleaned_data['departure_favoriteplace'] }, 'arrival': { 'name': form.cleaned_data['arrival'], 'point': form.cleaned_data['arrival_point'], 'favoriteplace': form.cleaned_data['arrival_favoriteplace'] }, 'date': form.cleaned_data['date'].strftime("%d/%m/%Y") if form.cleaned_data['date'] else date_default.strftime("%d/%m/%Y") } request.session['search_trip_details'] = search_trip_details match_departure = R_CITY_ZIP.match( str_slugify(form.cleaned_data['departure'])) match_arrival = R_CITY_ZIP.match( str_slugify(form.cleaned_data['arrival'])) args = None if match_departure and match_arrival: args = [ match_departure.group(1), match_departure.group(2), match_arrival.group(1), match_arrival.group(2) ] if trip_type == Trip.OFFER: if args: return HttpResponseRedirect( reverse('carpool:robots_search_offer_trip', args=args)) else: return HttpResponseRedirect( reverse('carpool:search_offer_trip')) else: if args: return HttpResponseRedirect( reverse('carpool:robots_search_demand_trip', args=args)) else: return HttpResponseRedirect( reverse('carpool:search_demand_trip')) else: if dates_concert: form = SearchTripRmll2009Form(data) else: initial_data = {'date': today} form = SearchTripForm(data, initial=initial_data) response_dict.update({ 'form': form, 'date_uptodate': dates_concert, 'cities_concert': _CITIES_CONCERT, }) template = loader.get_template('carpool/home_rmll2009.html') context = RequestContext(request, response_dict) return HttpResponse(template.render(context))