def register_hub(request): """Hub creation page Returns the hub registration page Args: request: The HTTP request Returns: The hub registration page """ error = None if request.method == 'POST': airport_id = request.POST['airport'] airline = request.user.airline.first() airport = Airport.objects.filter(pk=airport_id) if airport.exists(): airport = airport.first() if not (Hub.objects.filter(owner=airline, airport=airport).exists()): hub = Hub(owner=airline, airport=airport) hub.save() return redirect('home') else: error = "Own this hub" airports = Airport.objects.all() return render(request, 'registration/registration-hub.html', {'airports': airports, 'error': error})
def buy_hub_save(request): """Buy a hub Buy a given hub if possible Args: request: the HTTP request Returns: """ if request.method == 'POST': airport_id = request.POST['airport'] airline = request.user.airline.first() airport = Airport.objects.filter(pk=airport_id) error = None if airport.exists(): airport = airport.first() if not (Hub.objects.filter(owner=airline, airport_id=airport_id).exists()): if not airline.hubs.exists(): hub = Hub() hub.owner_id = airline.pk hub.airport_id = airport_id hub.save() return redirect('home') elif airline.money > airport.price: hub = Hub() hub.owner_id = airline.pk hub.airport_id = airport_id airline.credit(airport.price) hub.save() return redirect('home') else: error = _("You do not have the necessary funds") else: error = _("You already own this hub") hubs = airline.hubs.all().values_list('airport_id', flat=True) airports = Airport.objects.all().exclude(id__in=hubs) return render(request, 'buy-hub.html', {'airports': airports, 'error': error}) else: return redirect('buy-hub')