def test_unvalid_route_form(self): data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id] } form = RouteForm(data=data) self.assertFalse(form.is_valid())
def test_valid_route_form(self): data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'total_travel_time': 9, 'cities': [self.city_E.id, self.city_D.id] } form = RouteForm(data=data) self.assertTrue(form.is_valid())
def test_invalid_route_form(self): #Передаем невалидные данные в форму data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id] } form = RouteForm(data=data) self.assertFalse(form.is_valid())
def test_invalid_route_form( self): # проверка формы, нет одного обязательного поля data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id], } form = RouteForm(data=data) self.assertFalse(form.is_valid())
def test_valid_route_form(self): # проверка формы data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id], 'travelling_time': 9 } form = RouteForm(data=data) self.assertTrue(form.is_valid())
def test_valid_route_form(self): # Передаем валидные данные в форму data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id], 'travelling_time': 9 } form = RouteForm(data=data) self.assertTrue(form.is_valid())
def test_invalid_int_float_route_form( self): # проверка формы, вместо int - float data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id], 'travelling_time': 9.45 } form = RouteForm(data=data) self.assertFalse(form.is_valid())
def find_routes(request): if request.method == 'POST': form = RouteForm(request.POST) if form.is_valid(): try: context = get_routes(request, form) except ValueError as error: messages.error(request, error) return render(request, 'routes/find.html', {'form': form}) return render(request, 'routes/find.html', context) form = RouteForm() return render(request, 'routes/find.html', {'form': form})
def find_routes(request): if request.method == 'POST': form = RouteForm(request.POST) if form.is_valid(): try: context = get_routes(request, form) except ValueError as e: messages.error(request, e) return render(request, 'routes/home.html', {'form': form}) return render(request, 'routes/home.html', context) else: form = RouteForm() messages.error(request, 'Data not specified') return render(request, 'routes/home.html', {'form': form})
def find_routes(request): if request.method == "POST": form = RouteForm(request.POST) if form.is_valid(): try: context = get_routes(request, form) except ValueError as e: messages.error(request, e) return render(request, 'routes/home.html', {'form': form}) return render(request, 'routes/home.html', context) return render(request, 'routes/home.html', {'form': form}) else: form = RouteForm() messages.error(request, "Нет данных для поиска") return render(request, 'routes/home.html', {'form': form})
def find_routes(request): if request.method == "POST": form = RouteForm(request.POST or None) context = {'form': form} if form.is_valid(): # print(form.cleaned_data) try: context = get_routes(request, form) except ValueError as e: messages.error(request, e) # return render(request, 'routes/home.html', {'form': form}) return render(request, 'routes/home.html', context) else: messages.error(request, 'Создайте маршрут') form = RouteForm() return render(request, 'routes/home.html', {'form': form})
def add_route(request): if request.method == 'POST': context = {} data = request.POST if data: total_time = int(data['total_time']) from_city_id = int(data['from_city']) to_city_id = int(data['to_city']) trains = data['trains'].split(',') trains_lst = [int(t) for t in trains if t.isdigit()] qs = Train.objects.filter(id__in=trains_lst).select_related( 'from_city', 'to_city') cities = City.objects.filter( id__in=[from_city_id, to_city_id]).in_bulk() form = RouteModelForm( initial={ 'from_city': cities[from_city_id], 'to_city': cities[to_city_id], 'travel_times': total_time, 'trains': qs } ) context['form'] = form return render(request, 'routes/create.html', context) form = RouteForm() messages.error(request, 'Невозможно сохранить не существующий маршрут') # return render(request, 'routes/find.html', {'form': form}) return redirect('routes/find.html')
def save_route(request): if request.method == 'POST': form = RouteModelForm(request.POST) if form.is_valid(): form.save() messages.success(request, 'Маршрут успешно сохранен!') return redirect('routes:find') form = RouteForm() return render(request, 'routes/find.html', {'form': form})
def index(request): schedule_times = [] api_key = os.environ.get('DIR_API_KEY') if request.user.id: current_user = request.user.id else: current_user = 00 # if request.method == "POST": # form = RouteForm(request.POST) # if form.is_valid(): # start_stop = form.cleaned_data['origin'] # end_stop = form.cleaned_data['destination'] # date = form.cleaned_data['date'] # hour = form.cleaned_data['time'].split(":")[0] # date_time = datetime.datetime.strptime(date, '%Y-%m-%d') # route = "16" # start_stop = "12" # r = requests.get( # 'https://data.smartdublin.ie/cgi-bin/rtpi/timetableinformation?type=day&stopid={0}&routeid={1}&datetime={2}'. # format(start_stop, route, date_time)) # json_resp = json.loads(r.text) # for i in json_resp['results']: # if i['arrivaldatetime'].split(" ")[1].startswith(hour): # schedule_times.append(i['arrivaldatetime'].split(" ")[1]) form = RouteForm(initial={'origin': 'Newcastle Manor', 'destination': 'Clutterland', 'date': '2020-07-09', 'time': '17:00', 'line': '68'}) context = {'component': 'app', 'title': 'Dublin Bus | Home', 'current_user': current_user, 'form': form, 'schedule_times': schedule_times, 'key': api_key} return render(request, 'index.html', context)
def test_invalid_route_form(self): data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id], } form = RouteForm(data=data) self.assertFalse(form.is_valid()) data = { 'from_city': self.city_A.id, 'to_city': self.city_B.id, 'cities': [self.city_E.id, self.city_D.id], 'travelling_time': 9.45 } form = RouteForm(data=data) self.assertFalse(form.is_valid())
def home(request): form = RouteForm() return render(request, 'routes/home.html', {'form': form})
def find_routes(request): if request.method == 'POST': form = RouteForm(request.POST or None) if form.is_valid(): data = form.cleaned_data from_city = data['from_city'] to_city = data['to_city'] transit_cities_db = data['transit_cities'] travel_time = data['travel_time'] graph = get_graph() all_ways = list(dfs_paths(graph, from_city.id, to_city.id)) if len(all_ways) == 0: messages.error(request, message='Нет поезда(ов) с таким маршрутом') return render(request, 'routes/home.html', {'form': form}) if transit_cities_db: transit_cities_list = [city.id for city in transit_cities_db] right_ways = [] for way in all_ways: if all(point in way for point in transit_cities_list): right_ways.append(way) if not right_ways: messages.error( request, message='Нет поезда(ов) через данные транзитные города' ) return render(request, 'routes/home.html', {'form': form}) else: right_ways = all_ways trains = [] for way in right_ways: tmp = {} tmp['total_time'] = 0 tmp['trains'] = [] total_time = 0 for index in range(len(way) - 1): # Выбираем из БД поезда с нужными начальным/конечным пунктами и сортируем по времени в пути. qs = Train.objects.filter( from_city=way[index], to_city=way[index + 1]).order_by('travel_time') for train in qs: total_time += train.travel_time tmp['total_time'] = total_time tmp['trains'].append(qs) if travel_time and total_time <= travel_time: trains.append(tmp) elif not travel_time: trains.append(tmp) if not trains: messages.error(request, message='Время в пути больше заданого') return render(request, 'routes/home.html', {'form': form}) routes = [] cities = {'from_city': from_city.name, 'to_city': to_city.name} for train in trains: routes.append({ 'total_time': train['total_time'], 'route': train['trains'], 'from_city': from_city.name, 'to_city': to_city.name }) sorted_routes = [] if len(routes) == 1: sorted_routes = routes else: times = list(set(x['total_time'] for x in routes)) times = sorted(times) for time in times: for route in routes: if time == route['total_time']: sorted_routes.append(route) context = {} form = RouteForm() context['form'] = form context['routes'] = sorted_routes context['cities'] = cities return render(request, 'routes/home.html', context) return render(request, 'routes/home.html', {'form': form}) else: messages.error(request, message='Заполните форму') form = RouteForm() return render(request, 'routes/home.html', {'form': form})
def find(request): form = RouteForm() return render(request, 'routes/find.html', {'form': form})