Пример #1
0
def get_trains(request):
	context_dict = {}
	if request.method == 'POST':
		from_station = request.POST.get('from_station')
		to_station = request.POST.get('to_station')
		date = request.POST.get('departure_date')
		departure_date = datetime.strptime(date, "%Y-%m-%d")
		client = Client(api_usernumber = conf.USER_NUMBER, api_passmd5 = conf.USER_PASSMD5, id_terminal = conf.ID_TERMINAL, id_service = conf.ID_SERVICE, request_url = conf.API_URL)
		df = DateFormat(departure_date)
		places = client.get_places(from_station, to_station, df.format('d.m.Y'), '0123')
		data = json.dumps(places, ensure_ascii=False)
		response_dict = json.loads(data)
		if 'status' in response_dict:
			context_dict['error'] = response_dict['message']
		else:
			context_dict = request_handler(response_dict)
		context_dict['data'] = data
		context_dict['from_station'] = Station.objects.get(code=from_station)
		context_dict['to_station'] = Station.objects.get(code=to_station)
		context_dict['departure_date'] = date
		context_dict['stations'] = Station.objects.all()
	return render(request, 'search/trains.html', context_dict)