@login_required
@permission_required("waitinglist.manage_cohorts")
def cohort_member_add(request, pk):

    cohort = Cohort.objects.get(pk=pk)

    if "invite_next" in request.POST:
        try:
            N = int(request.POST["invite_next"])
        except __HOLE__:
            return redirect("waitinglist_cohort_detail", cohort.id)
        # people who are NOT invited or on the site already
        waiting_list = WaitingListEntry.objects.exclude(
            email__in=SignupCode.objects.values("email")
        ).exclude(
            email__in=User.objects.values("email")
        )
        emails = waiting_list.values_list("email", flat=True)[:N]
    else:
        email = request.POST["email"].strip()
        if email:
            emails = [email]
        else:
            emails = []

    for email in emails:
        if not SignupCode.objects.filter(email=email).exists():
            signup_code = SignupCode.create(email=email, max_uses=1, expiry=730)
            signup_code.save()
            SignupCodeCohort.objects.create(signup_code=signup_code, cohort=cohort)

    return redirect("waitinglist_cohort_detail", cohort.id)
dataset/ETHPy150Open pinax/django-waitinglist/waitinglist/views.py/cohort_member_add