def search(request): if request.method == 'POST': search_form = SearchForm(request.POST) if search_form.is_valid(): departure_city = search_form.cleaned_data['departure_city'] arrival_city = search_form.cleaned_data['arrival_city'] departure_date = search_form.cleaned_data['departure_date'] unique_flights = [] for flight in Flight.objects.all(): diff = int((departure_date - flight.departure_date_begin).total_seconds() / 86400) if diff < 0 or diff % flight.repeat_interval != 0 \ or flight.departure_airport.city != departure_city \ or flight.arrival_airport.city != arrival_city: continue departure_datetime = parse_datetime('{} {}'.format(departure_date, flight.departure_time)) uniq_diff = flight.arrival_date_begin - flight.departure_date_begin arrival_date = departure_datetime.date() + timedelta(days=uniq_diff.days) arrival_time = flight.arrival_time db_unique_flights = UniqueFlight.objects.filter(flight__exact=flight, departure_datetime=departure_datetime) if db_unique_flights.count() == 0: unique_flight = UniqueFlight(flight_id=flight.id, departure_datetime=departure_datetime, arrival_date=arrival_date, arrival_time=arrival_time, left_seats_F=flight.aircraft.seat_count_F, left_seats_B=flight.aircraft.seat_count_B, left_seats_E=flight.aircraft.seat_count_E) unique_flight.save() else: unique_flight = db_unique_flights[0] unique_flights.append(unique_flight) return render_to_response('flights.html', {'unique_flights': unique_flights, 'search_form': search_form}, context_instance=RequestContext(request)) else: search_form = SearchForm() return render_to_response('search.html', {'search_form': search_form}, context_instance=RequestContext(request))
def show_all(request): unique_flights = [] today = datetime.date.today() for i in range(1, 183): current_day = today + datetime.timedelta(days=i) for flight in Flight.objects.all(): diff = int((current_day - flight.departure_date_begin).total_seconds() / 86400) if diff < 0 or diff % flight.repeat_interval != 0: continue current_datetime = parse_datetime('{} {}'.format(current_day, flight.departure_time)) uniq_diff = flight.arrival_date_begin - flight.departure_date_begin arrival_date = current_datetime.date() + timedelta(days=uniq_diff.days) arrival_time = flight.arrival_time db_unique_flights = UniqueFlight.objects.filter(flight__exact=flight, departure_datetime=current_datetime) if db_unique_flights.count() == 0: unique_flight = UniqueFlight(flight_id=flight.id, departure_datetime=current_datetime, arrival_date=arrival_date, arrival_time=arrival_time, left_seats_F=flight.aircraft.seat_count_F, left_seats_B=flight.aircraft.seat_count_B, left_seats_E=flight.aircraft.seat_count_E ) unique_flight.save() else: unique_flight = db_unique_flights[0] unique_flights.append(unique_flight) p = Paginator(unique_flights, 10) if 'page' in request.POST: page = request.POST.get("page", "") page_number = int(page) else: page_number = 1 is_next = p.page(page_number).has_next() is_previous = p.page(page_number).has_previous() if 'next' in request.POST: if is_next: page_number += 1 if 'previous' in request.POST: if is_previous: page_number -= 1 is_next = p.page(page_number).has_next() is_previous = p.page(page_number).has_previous() current_objects = p.page(page_number).object_list return render_to_response('flights_all.html', {'object_list': current_objects, 'page_number': page_number, 'next': is_next, 'previous': is_previous}, context_instance=RequestContext(request))