Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Flights - Crews</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <link rel="stylesheet" type="text/css" href="/static/flights/style.css" />
- </head>
- <script>
- $(function() {
- var $crews = $('#crews');
- var today = new Date();
- var month = today.getUTCMonth() + 1;
- var day = today.getUTCDate();
- var year = today.getUTCFullYear();
- filterByDay(day, month, year);
- });
- var filterByDay = function(day, month, year) {
- if (day === undefined) {
- day = 1;
- }
- if (month === undefined) {
- month = 1;
- }
- if (year === undefined) {
- year = 2018;
- }
- var $crews = $('#crews');
- $.ajax({
- type: 'GET',
- data: {
- day: day,
- month: month,
- year: year
- },
- url: 'http://127.0.0.1:8000/flights/api/get_flights/',
- success: function(flights) {
- $.each(flights.flights, function(i, flight) {
- $crews.append('<tr><th>' + flight.flight + '</th><th>' + flight.crew + '</th></tr>');
- });
- },
- error: function() {
- alert('AJAX request failed');
- }
- })
- }
- var onClickFilterButton = function() {
- alert(document.querySelector('#filter_day').value);
- alert(document.querySelector('#filter_month').value);
- alert(document.querySelector('#filter_year').value);
- filterByDay(document.querySelector('#filter_day').value, document.querySelector('#filter_month').value, document.querySelector('#filter_year').value);
- }
- </script>
- <body>
- <header>
- <p>
- <div id="demo"> </div>
- </p>
- <p>
- <a href=>Logout</a>
- </p>
- </header>
- Day: <input id="filter_day" type="text" name="filter_day"><br>
- Month: <input id="filter_month" type="text" name="filter_month"><br>
- Year: <input id="filter_year" type="text" name="filter_year"><br>
- <button onclick="onClickFilterButton()">Filter</button>
- <a href="/flights/">Go back</a>
- <table id="crews">
- <tr>
- <th>Flight</th>
- <th>Crew</th>
- </tr>
- </table>
- <button id=add_crew>Add Crew!</button>
- <footer>
- © Piotr Gierda 2018
- </footer>
- </body>
- </html>
- ////views
- from django.shortcuts import get_object_or_404, render, redirect
- from django.http import HttpResponse
- from django.template import loader
- from django.views.decorators.http import require_POST, require_GET
- from django.contrib.auth import authenticate, login as login_to_session
- from django.contrib.auth.models import User
- from django.contrib.auth.decorators import login_required
- from django.db import transaction
- import django.forms as forms
- from django.http import HttpResponse, JsonResponse
- from django.views.decorators.csrf import csrf_exempt
- from .models import Flight, Plane, Ticket, Crew
- from .forms import TicketForm, RegistrationForm, TimePeriodForm
- def main(request):
- form = TimePeriodForm()
- flights = Flight.objects.order_by('departure', 'plane__id', 'source', 'destination')
- if request.method == 'POST':
- form = TimePeriodForm(request.POST)
- if form.is_valid():
- from_year = str(request.POST['time_period_start_year'])
- from_month = str(request.POST['time_period_start_month'])
- from_day = str(request.POST['time_period_start_day'])
- time_period_from = from_year + '-' + from_month + '-' + from_day + ' 00:00'
- to_year = str(request.POST['time_period_end_year'])
- to_month = str(request.POST['time_period_end_month'])
- to_day = str(request.POST['time_period_end_day'])
- time_period_to = to_year + '-' + to_month + '-' + to_day + ' 23:59'
- flights = Flight.objects.filter(departure__gte=time_period_from, arrival__lte=time_period_to).order_by('departure', 'plane__id', 'source', 'destination')
- context = {'flights': flights}
- return render(request, 'flights/main.html', locals())
- @require_POST
- def login(request):
- user = authenticate(username=request.POST['username'], password=request.POST['password'])
- if user is not None:
- login_to_session(request, user)
- else:
- error = True
- next = request.POST.get('next', '/')
- return redirect(next)
- def register(request):
- if request.method == 'POST':
- form = RegistrationForm(request.POST)
- if form.is_valid():
- user = User.objects.create_user(username=request.POST['username'], password=request.POST['password'])
- login_to_session(request, user)
- return redirect('main')
- else:
- form = RegistrationForm()
- return render(request, 'flights/register.html', locals())
- def detail(request, flight_id):
- flight = get_object_or_404(Flight, pk=flight_id)
- tickets = Ticket.objects.filter(flight=flight).order_by('passenger')
- t = (Flight.objects.filter(id=flight.id))[0]
- # q = str(t) + ' from ' + t.source + ' to ' + t.destination + " " + str(t.departure) + " -> " + str(t.arrival)
- q = str(t)
- if request.user.is_authenticated:
- if request.method == 'POST':
- try:
- with transaction.atomic():
- form = TicketForm(request.POST)
- if form.is_valid():
- newticket = form.save(commit=False)
- newticket.flight = flight
- newticket.full_clean()
- if len((Ticket.objects.filter(flight=flight).select_related('flight'))) != 0:
- if len(tickets) >= ((Flight.objects.filter(plane=((Ticket.objects.filter(flight=flight).select_related('flight')[0]).flight).plane).select_related('plane')[0]).plane).seats:
- raise forms.ValidationError(u"No more free seats.")
- newticket.save()
- return redirect('detail', flight_id)
- except forms.ValidationError as e:
- message="No more free seats.";
- else:
- form = TicketForm()
- return render(request, 'flights/detail.html', locals())
- def crews(request):
- return render(request, 'flights/crews.html', locals())
- # api requests
- @require_GET
- @csrf_exempt
- def get_crews(request):
- response = []
- crews = Crew.objects.all()
- for crew in crews:
- response.append({'crew': str(crew)})
- return JsonResponse({'crews': response})
- @require_GET
- @csrf_exempt
- def get_flights(request):
- response = []
- flights = Flight.objects.all()
- from_day = str(request.GET['day'])
- from_month = str(request.GET['month'])
- from_year = str(request.GET['year'])
- time_period_from = from_year + '-' + from_month + '-' + from_day + ' 00:00'
- to_day = str(request.GET['day'])
- to_month = str(request.GET['month'])
- to_year = str(request.GET['year'])
- time_period_to = to_year + '-' + to_month + '-' + to_day + ' 23:59'
- flights = Flight.objects.filter(departure__gte=time_period_from, arrival__lte=time_period_to)
- for flight in flights:
- response.append({'flight': str(flight), 'crew': str(flight.crew)})
- return JsonResponse({'flights': response})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement