Advertisement
Guest User

Untitled

a guest
Jun 10th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Flights - Crews</title>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <link rel="stylesheet" type="text/css" href="/static/flights/style.css" />
  8. </head>
  9. <script>
  10. $(function() {
  11. var $crews = $('#crews');
  12.  
  13. var today = new Date();
  14. var month = today.getUTCMonth() + 1;
  15. var day = today.getUTCDate();
  16. var year = today.getUTCFullYear();
  17.  
  18. filterByDay(day, month, year);
  19. });
  20.  
  21. var filterByDay = function(day, month, year) {
  22. if (day === undefined) {
  23. day = 1;
  24. }
  25. if (month === undefined) {
  26. month = 1;
  27. }
  28. if (year === undefined) {
  29. year = 2018;
  30. }
  31. var $crews = $('#crews');
  32.  
  33. $.ajax({
  34. type: 'GET',
  35. data: {
  36. day: day,
  37. month: month,
  38. year: year
  39. },
  40. url: 'http://127.0.0.1:8000/flights/api/get_flights/',
  41. success: function(flights) {
  42. $.each(flights.flights, function(i, flight) {
  43. $crews.append('<tr><th>' + flight.flight + '</th><th>' + flight.crew + '</th></tr>');
  44. });
  45. },
  46. error: function() {
  47. alert('AJAX request failed');
  48. }
  49. })
  50.  
  51. }
  52.  
  53. var onClickFilterButton = function() {
  54. alert(document.querySelector('#filter_day').value);
  55. alert(document.querySelector('#filter_month').value);
  56. alert(document.querySelector('#filter_year').value);
  57.  
  58. filterByDay(document.querySelector('#filter_day').value, document.querySelector('#filter_month').value, document.querySelector('#filter_year').value);
  59. }
  60.  
  61. </script>
  62. <body>
  63. <header>
  64. <p>
  65. <div id="demo"> </div>
  66. </p>
  67. <p>
  68. <a href=>Logout</a>
  69. </p>
  70.  
  71. </header>
  72.  
  73. Day: <input id="filter_day" type="text" name="filter_day"><br>
  74. Month: <input id="filter_month" type="text" name="filter_month"><br>
  75. Year: <input id="filter_year" type="text" name="filter_year"><br>
  76. <button onclick="onClickFilterButton()">Filter</button>
  77. <a href="/flights/">Go back</a>
  78. <table id="crews">
  79. <tr>
  80. <th>Flight</th>
  81. <th>Crew</th>
  82. </tr>
  83. </table>
  84.  
  85. <button id=add_crew>Add Crew!</button>
  86. <footer>
  87. &copy; Piotr Gierda 2018
  88. </footer>
  89. </body>
  90. </html>
  91.  
  92.  
  93.  
  94.  
  95.  
  96. ////views
  97. from django.shortcuts import get_object_or_404, render, redirect
  98. from django.http import HttpResponse
  99. from django.template import loader
  100. from django.views.decorators.http import require_POST, require_GET
  101. from django.contrib.auth import authenticate, login as login_to_session
  102. from django.contrib.auth.models import User
  103. from django.contrib.auth.decorators import login_required
  104. from django.db import transaction
  105. import django.forms as forms
  106. from django.http import HttpResponse, JsonResponse
  107. from django.views.decorators.csrf import csrf_exempt
  108.  
  109. from .models import Flight, Plane, Ticket, Crew
  110. from .forms import TicketForm, RegistrationForm, TimePeriodForm
  111.  
  112. def main(request):
  113. form = TimePeriodForm()
  114. flights = Flight.objects.order_by('departure', 'plane__id', 'source', 'destination')
  115. if request.method == 'POST':
  116. form = TimePeriodForm(request.POST)
  117. if form.is_valid():
  118. from_year = str(request.POST['time_period_start_year'])
  119. from_month = str(request.POST['time_period_start_month'])
  120. from_day = str(request.POST['time_period_start_day'])
  121. time_period_from = from_year + '-' + from_month + '-' + from_day + ' 00:00'
  122. to_year = str(request.POST['time_period_end_year'])
  123. to_month = str(request.POST['time_period_end_month'])
  124. to_day = str(request.POST['time_period_end_day'])
  125. time_period_to = to_year + '-' + to_month + '-' + to_day + ' 23:59'
  126. flights = Flight.objects.filter(departure__gte=time_period_from, arrival__lte=time_period_to).order_by('departure', 'plane__id', 'source', 'destination')
  127. context = {'flights': flights}
  128. return render(request, 'flights/main.html', locals())
  129.  
  130. @require_POST
  131. def login(request):
  132. user = authenticate(username=request.POST['username'], password=request.POST['password'])
  133. if user is not None:
  134. login_to_session(request, user)
  135. else:
  136. error = True
  137. next = request.POST.get('next', '/')
  138. return redirect(next)
  139.  
  140. def register(request):
  141. if request.method == 'POST':
  142. form = RegistrationForm(request.POST)
  143. if form.is_valid():
  144. user = User.objects.create_user(username=request.POST['username'], password=request.POST['password'])
  145. login_to_session(request, user)
  146. return redirect('main')
  147. else:
  148. form = RegistrationForm()
  149. return render(request, 'flights/register.html', locals())
  150.  
  151. def detail(request, flight_id):
  152. flight = get_object_or_404(Flight, pk=flight_id)
  153. tickets = Ticket.objects.filter(flight=flight).order_by('passenger')
  154.  
  155. t = (Flight.objects.filter(id=flight.id))[0]
  156. # q = str(t) + ' from ' + t.source + ' to ' + t.destination + " " + str(t.departure) + " -> " + str(t.arrival)
  157. q = str(t)
  158. if request.user.is_authenticated:
  159. if request.method == 'POST':
  160. try:
  161. with transaction.atomic():
  162. form = TicketForm(request.POST)
  163. if form.is_valid():
  164. newticket = form.save(commit=False)
  165. newticket.flight = flight
  166. newticket.full_clean()
  167. if len((Ticket.objects.filter(flight=flight).select_related('flight'))) != 0:
  168. if len(tickets) >= ((Flight.objects.filter(plane=((Ticket.objects.filter(flight=flight).select_related('flight')[0]).flight).plane).select_related('plane')[0]).plane).seats:
  169. raise forms.ValidationError(u"No more free seats.")
  170. newticket.save()
  171. return redirect('detail', flight_id)
  172. except forms.ValidationError as e:
  173. message="No more free seats.";
  174. else:
  175. form = TicketForm()
  176.  
  177. return render(request, 'flights/detail.html', locals())
  178.  
  179. def crews(request):
  180. return render(request, 'flights/crews.html', locals())
  181.  
  182. # api requests
  183. @require_GET
  184. @csrf_exempt
  185. def get_crews(request):
  186. response = []
  187. crews = Crew.objects.all()
  188. for crew in crews:
  189. response.append({'crew': str(crew)})
  190. return JsonResponse({'crews': response})
  191.  
  192. @require_GET
  193. @csrf_exempt
  194. def get_flights(request):
  195. response = []
  196. flights = Flight.objects.all()
  197. from_day = str(request.GET['day'])
  198. from_month = str(request.GET['month'])
  199. from_year = str(request.GET['year'])
  200. time_period_from = from_year + '-' + from_month + '-' + from_day + ' 00:00'
  201. to_day = str(request.GET['day'])
  202. to_month = str(request.GET['month'])
  203. to_year = str(request.GET['year'])
  204. time_period_to = to_year + '-' + to_month + '-' + to_day + ' 23:59'
  205. flights = Flight.objects.filter(departure__gte=time_period_from, arrival__lte=time_period_to)
  206. for flight in flights:
  207. response.append({'flight': str(flight), 'crew': str(flight.crew)})
  208. return JsonResponse({'flights': response})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement