コード例 #1
0
def add_key(request):
    username = request.POST.get("username", "")
    code = request.POST.get("code", "")

    # Try and find one and only one user for this username
    user = None
    if username:
        user_search = User.objects.filter(username=username)
        if not user_search:
            messages.add_message(
                request, messages.ERROR,
                "Could not find user for username '%s'" % username)
        elif user_search.count() > 1:
            messages.add_message(
                request, messages.ERROR,
                "More then one user found for username '%s'" % username)
        else:
            user = user_search.first()

    # Make sure this door code isn't used by anyone else
    if code:
        if DoorCode.objects.filter(code=code).count() > 0:
            messages.add_message(request, messages.ERROR,
                                 "Door code '%s' already in use!" % code)
            code = ""

    # If we have enough information construct a door_code
    # but don't save it until we get user confirmation
    door_code = None
    if user and code:
        door_code = DoorCode(created_by=request.user, user=user, code=code)

    # Save the code if we've received user confirmation
    if door_code and 'add_door_code' in request.POST:
        door_code.save()
        email.announce_new_key(user)
        return HttpResponseRedirect(
            reverse('doors:keys', kwargs={'username': user.username}))

    # Pull a list of active members for our autocomplete
    active_members = User.helper.active_members()

    context = {
        'username': username,
        'code': code,
        'door_code': door_code,
        'active_members': active_members
    }
    return render(request, 'keymaster/add_key.html', context)
コード例 #2
0
ファイル: views.py プロジェクト: nadineproject/nadine
def add_key(request):
    username = request.POST.get("username", "")
    code = request.POST.get("code", "")

    # Try and find one and only one user for this username
    user = None
    if username:
        user_search = User.objects.filter(username=username)
        if not user_search:
            messages.add_message(request, messages.ERROR, "Could not find user for username '%s'" % username)
        elif user_search.count() > 1:
            messages.add_message(request, messages.ERROR, "More then one user found for username '%s'" % username)
        else:
            user = user_search.first()

    # Make sure this door code isn't used by anyone else
    if code:
        if DoorCode.objects.filter(code=code).count() > 0:
            messages.add_message(request, messages.ERROR, "Door code '%s' already in use!" % code)
            code = ""

    # If we have enough information construct a door_code
    # but don't save it until we get user confirmation
    door_code = None
    if user and code:
        door_code = DoorCode(created_by=request.user, user=user, code=code)

    # Save the code if we've received user confirmation
    if door_code and 'add_door_code' in request.POST:
        door_code.save()
        email.announce_new_key(user)
        return HttpResponseRedirect(reverse('doors:keys', kwargs={'username': user.username}))

    # Pull a list of active members for our autocomplete
    active_members = User.helper.active_members()

    context = {'username':username, 'code':code, 'door_code':door_code, 'active_members':active_members}
    return render(request, 'keymaster/add_key.html', context)