@login_required
def instructors(request):
    '''Search for instructors.'''
    instructor_badges = Badge.objects.instructor_badges()
    instructors = Person.objects.filter(badges__in=instructor_badges) \
                                .filter(airport__isnull=False) \
                                .select_related('airport') \
                                .prefetch_related('lessons')
    instructors = instructors.annotate(
        num_taught=Count(
            Case(
                When(
                    task__role__name='instructor',
                    then=Value(1)
                ),
                output_field=IntegerField()
            )
        )
    )
    form = InstructorsForm()

    lessons = list()

    if 'submit' in request.GET:
        form = InstructorsForm(request.GET)
        if form.is_valid():
            data = form.cleaned_data

            if data['lessons']:
                lessons = data['lessons']
                # this has to be in a loop to match a *subset* of lessons,
                # not any lesson within the list (as it would be with
                # `.filter(lessons_in=lessons)`)
                for lesson in lessons:
                    instructors = instructors.filter(
                        qualification__lesson=lesson
                    )

            if data['airport']:
                x = data['airport'].latitude
                y = data['airport'].longitude
                # using Euclidean distance just because it's faster and easier
                complex_F = ((F('airport__latitude') - x) ** 2
                             + (F('airport__longitude') - y) ** 2)
                instructors = instructors.annotate(distance=complex_F) \
                                         .order_by('distance', 'family')

            if data['latitude'] and data['longitude']:
                x = data['latitude']
                y = data['longitude']
                # using Euclidean distance just because it's faster and easier
                complex_F = ((F('airport__latitude') - x) ** 2
                             + (F('airport__longitude') - y) ** 2)
                instructors = instructors.annotate(distance=complex_F) \
                                         .order_by('distance', 'family')

            if data['country']:
                instructors = instructors.filter(
                    airport__country__in=data['country']
                ).order_by('family')

            if data['gender']:
                instructors = instructors.filter(gender=data['gender'])

            if data['instructor_badges']:
                for badge in data['instructor_badges']:
                    instructors = instructors.filter(badges__name=badge)

    instructors = get_pagination_items(request, instructors)
    context = {
        'title': 'Find Instructors',
        'form': form,
        'persons': instructors,
        'lessons': lessons,
    }
    return render(request, 'workshops/instructors.html', context)

#------------------------------------------------------------
dataset/ETHPy150Open swcarpentry/amy/workshops/views.py/instructors/original