def test_get_pilots(self): self.assertEqual(len(util.get_pilots()), 0) pilot1 = helper.create_pilot('kim', 'Kim', 'Pilot1') pilot2 = helper.create_pilot('sam', 'Sam', 'Pilot2') pilot3 = helper.create_pilot('ada', 'Ada', 'Pilot0') expected = [pilot3, pilot1, pilot2] query = util.get_pilots() self.assertEqual([o for o in query], expected) user1 = User.objects.create_user('user', 'User', 'Non-Pilot') query = util.get_pilots() self.assertEqual([o for o in query], expected)
class FilterForm(forms.Form): pilot = forms.ModelChoiceField( queryset=util.get_pilots(), empty_label="All", required=False, ) airstrip = forms.ModelChoiceField( queryset=Airstrip.objects.all().order_by('ident'), empty_label="All", required=False, ) base = BaseModelChoiceField( queryset=util.get_bases().order_by('name'), empty_label="All", required=False, ) aircraft_type = forms.ModelChoiceField( queryset=AircraftType.objects.all().order_by('sorted_position'), empty_label="All", required=False, widget=forms.RadioSelect, ) checkout_status = forms.ChoiceField( choices=util.choices_checkout_status(), initial=util.CHECKOUT_SUDAH, widget=forms.RadioSelect, )
class CheckoutEditForm(forms.Form): pilot = forms.ModelChoiceField( # We're defaulting to the full list, but it may be trimmed down to the # request's authenticated user in the view queryset=util.get_pilots(), empty_label=None, ) airstrip = forms.ModelChoiceField( queryset=Airstrip.objects.all().order_by('ident'), empty_label=None, ) aircraft_type = forms.ModelMultipleChoiceField( queryset=AircraftType.objects.all().order_by('sorted_position'), widget=forms.CheckboxSelectMultiple, )
def get_queryset(self): """Prepares the pilot/weight pairs for the template. If the requesting user is a simple pilot, we're only going to show them their own weight value (with a link to update it). If the request is from a flight_scheduler or a superuser, they'll see the full list of pilots and weights (with links to update all of them). Because the weight attribute is not automatically created for a new pilot, the output pilot/weight pairs will use a default weight of 0 if no corresponding PilotWeight instance exists. """ user = self.request.user # Security Check # -------------- # This would be unusual, but just in case: make sure that the request # is from a superuser, pilot, or flight scheduler (normal users may # not view nor edit pilot weights). if not user.is_superuser and not user.is_pilot and not user.is_flight_scheduler: logger.warn( "Forbidden: '%s' is not a pilot, flight scheduler, nor superuser" % user.username) message = 'Only pilots and flight schedulers may view pilot weights.' return self.forbidden(request, message) if user.is_flight_scheduler or user.is_superuser: pilots = util.get_pilots() else: pilots = [ user, ] pilot_weights = [] for pilot in pilots: try: weight = pilot.pilotweight.weight except ObjectDoesNotExist: # A matching PilotWeight probably hasn't been created yet. # Default to zero and continue. weight = 0 pilot_weights.append((pilot, weight)) return pilot_weights
class PilotList(LoginRequiredMixin, ListView): """List of current pilots""" queryset = util.get_pilots() context_object_name = 'pilot_list' template_name = 'checkouts/pilot_list.html'