def get_city(cls, value): """Return a city from a value""" if not value: return None match_address = R_CITY_ZIP.match(str_slugify(value)) try: if match_address: return cls.objects.get_from_slug( match_address.group(1), int(match_address.group(2)), ) else: return cls.objects.filter( slug__startswith=str_slugify(value)).order_by( '-population', 'slug', 'zipcode')[0] except (ValueError, cls.DoesNotExist, IndexError): return None
def get_city(cls, value): """Return a city from a value""" if not value: return None match_address = R_CITY_ZIP.match(str_slugify(value)) try: if match_address: return cls.objects.get_from_slug( match_address.group(1), int(match_address.group(2)), ) else: return cls.objects.filter( slug__startswith=str_slugify(value) ).order_by('-population', 'slug', 'zipcode')[0] except (ValueError, cls.DoesNotExist, IndexError): return None
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))