Ejemplo n.º 1
0
def add_route(request):
    if request.method == 'POST':
        form = RouteModelForm(request.POST or None)
        if form.is_valid():
            data = form.cleaned_data
            name = data['name']
            from_city = data['from_city']
            to_city = data['to_city']
            travel_times = data['travel_times']
            # Убираем пробелы из ID поездов
            transit_cities = data['transit_cities'].split(' ')
            trains = [int(x) for x in transit_cities if x.isalnum()]
            qs = Train.objects.filter(id__in=trains)
            route = Route(name=name,
                          from_city=from_city,
                          to_city=to_city,
                          travel_times=travel_times)
            route.save()
            for train in qs:
                route.transit_cities.add(train.id)
            messages.success(request, 'Маршрут успешно сохранен!')
            return redirect('/')
    else:
        data = request.GET
        if data:
            from_city = data['from_city']
            to_city = data['to_city']
            travel_times = data['travel_times']
            # Убираем пробелы из ID поездов
            transit_cities = data['transit_cities'].split(' ')
            trains = [int(x) for x in transit_cities if x.isnumeric()]
            qs = Train.objects.filter(id__in=trains)
            form = RouteModelForm(
                initial={
                    'from_city': from_city,
                    'to_city': to_city,
                    'travel_times': travel_times,
                    'transit_cities': data['transit_cities']
                })
            route_descriptions = []
            for train in qs:
                dsc = f'Поезд № {train.name} следующий из г. {train.from_city} в г. {train.to_city}. ' \
                      f'Время в пути {train.travel_time}'
                route_descriptions.append(dsc)
            context = {
                'form': form,
                'description': route_descriptions,
                'from_city': from_city,
                'to_city': to_city,
                'travel_times': travel_times
            }
            return render(request, 'routes/create.html', context=context)
        else:
            messages.error(request,
                           'Невозможно сохранить несуществующий маршрут')
            redirect('/')
Ejemplo n.º 2
0
def save_route(request):
    if request.method == "POST":
        form = RouteModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Маршрут успешно сохранен")
            return redirect('/')
        return render(request, 'routes/create.html', {'form': form})
    else:
        messages.error(request, "Невозможно сохранить несуществующий маршрут")
        return redirect('/')
Ejemplo n.º 3
0
def save_route(request):
    if request.method == 'POST':
        form = RouteModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Route saved successfully')
            return redirect('/')
        return render(request, 'routes/create.html', {'form': form})
    else:
        messages.error(request, 'Unable to save this route')
        return redirect('/')
Ejemplo n.º 4
0
def save_route(request):
    if request.method == 'POST':
        form = RouteModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Route saved')
            return redirect('/routes/')
        return render(request, 'routes/create.html', {'form': form})
    else:
        messages.error(request, 'Cannot save a missing route')
        return redirect('/')
Ejemplo n.º 5
0
def save_route(request):
    if request.method == "POST":
        form = RouteModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Routes successfully created")
            return redirect('/')
        return render(request, 'routes/create.html', {'form': form})
    else:
        messages.error(request, "Unable to save not existing route")
        return redirect('/')
Ejemplo n.º 6
0
def save_route(request):
    if request.method == 'POST':
        form = RouteModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Маршрут сохранён')
            return redirect('routes:home')

        return render(request, 'routes/create.html', {'form': form})
    else:
        messages.error(request, 'Невозможно сохранить несуществующий маршрут')
        return redirect('routes:home')
Ejemplo n.º 7
0
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)
    else:
        messages.error(request, "Невозможно сохранить несуществующий маршрут")
        return redirect('/')
Ejemplo n.º 8
0
def save_route(request):
    if request.method == 'POST':
        form = RouteModelForm(request.POST)
        if form.is_valid():
            route = form.save(commit=False)
            try:
                route.user = request.user
                route.save()
                messages.success(request, "Маршрут успешно сохранен")
                return redirect('/')
            except ValueError:
                messages.error(request,
                               "Необходимо войти или зарегистрироваться")
                return redirect('accounts:login')
        return render(request, 'routes/create.html', {'form': form})
    else:
        messages.error(request, "невозможно сохранить несуществующий маршрут")
        return redirect('/')
Ejemplo n.º 9
0
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})