示例#1
0
文件: views.py 项目: ngokevin/cyder
def login_session(request, username):
    """
    Logs in a user and sets up the session.
    """
    # authenticate / login
    try:
        user = User.objects.get(username=username)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
    except User.DoesNotExist:
        messages.error(request, "User %s does not exist" % (username))
        return request

    # create user profile if needed
    try:
        request.user.get_profile()
    except UserProfile.DoesNotExist:
        profile = UserProfile(user=user)
        profile.save()

    # assign user to default ctnr if needed
    try:
        CtnrUser.objects.get(user=request.user)
    except CtnrUser.DoesNotExist:
        new_default_ctnr = Ctnr.objects.get(id=2)
        CtnrUser(user=request.user, ctnr=new_default_ctnr, level=0).save()

    # set session ctnr
    default_ctnr = request.user.get_profile().default_ctnr
    if default_ctnr:
        request.session['ctnr'] = Ctnr.objects.get(id=default_ctnr.id)
    else:
        request.session['ctnr'] = Ctnr.objects.get(id=2)

    # set session ctnr level
    request.session['level'] = CtnrUser.objects.get(user=request.user, ctnr=default_ctnr).level

    try:
        # set ctnr list (to switch between)
        global_ctnr = CtnrUser.objects.get(user=request.user, ctnr=1)
        if global_ctnr:
            request.session['ctnrs'] = list(Ctnr.objects.all())

        # set ctnr json (for ctnr search bar)
        names = Ctnr.objects.all().values_list('name', flat=True)
        names = sorted([str(name) for name in names], key=str.lower)
        request.session['ctnr_names_json'] = simplejson.dumps(names)

    except CtnrUser.DoesNotExist:
        # set ctnr list (to switch between)
        ctnrs_user = CtnrUser.objects.filter(user=request.user)
        ctnrs = [Ctnr.objects.get(id=ctnr_pk) for ctnr_pk in  ctnrs_user.values_list('ctnr', flat=True)]
        request.session['ctnrs'] = ctnrs

        # set ctnr json (for ctnr search bar)
        names = sorted([str(ctnr.name) for ctnr in ctnrs], key=str.lower)
        request.session['ctnr_names_json'] = simplejson.dumps(names)

    return request
示例#2
0
def login_session(request, username):
    """Logs in a user and sets up the session."""
    try:
        # Authenticate / login.
        user = User.objects.get(username=username)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
    except User.DoesNotExist:
        messages.error(request, "User %s does not exist" % (username))
        return request

    try:
        # Create user profile if needed.
        request.user.get_profile()
    except UserProfile.DoesNotExist:
        profile = UserProfile(user=user)
        profile.save()

    try:
        # Assign user to default ctnr if needed.
        CtnrUser.objects.get(user=request.user,
                             ctnr_id=request.user.get_profile().default_ctnr)
    except CtnrUser.DoesNotExist:
        new_default_ctnr = Ctnr.objects.get(id=2)
        CtnrUser(user=request.user, ctnr=new_default_ctnr, level=0).save()

    # Set session ctnr.
    default_ctnr = request.user.get_profile().default_ctnr
    if default_ctnr:
        request.session['ctnr'] = Ctnr.objects.get(id=default_ctnr.id)
    else:
        request.session['ctnr'] = Ctnr.objects.get(id=2)

    # Set session ctnr level.
    request.session['level'] = CtnrUser.objects.get(user=request.user,
                                                    ctnr=default_ctnr).level

    try:
        # Set ctnr list (to switch between).
        global_ctnr = CtnrUser.objects.get(user=request.user, ctnr=1)
        if global_ctnr:
            request.session['ctnrs'] = (
                list(Ctnr.objects.filter(Q(id=1)
                                         | Q(id=2))) +
                list(Ctnr.objects.exclude(Q(id=1)
                                          | Q(id=2)).order_by("name")))

    except CtnrUser.DoesNotExist:
        # Set ctnr list (to switch between).
        ctnrs_user = CtnrUser.objects.filter(user=request.user)
        ctnrs = [
            Ctnr.objects.get(id=ctnr_pk)
            for ctnr_pk in ctnrs_user.values_list('ctnr', flat=True)
        ]
        request.session['ctnrs'] = ctnrs

    return request
示例#3
0
文件: views.py 项目: alecdhuse/cyder
def login_session(request, username):
    """Logs in a user and sets up the session."""
    try:
        # Authenticate / login.
        user = User.objects.get(username=username)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
    except User.DoesNotExist:
        messages.error(request, "User %s does not exist" % (username))
        return request

    try:
        # Create user profile if needed.
        request.user.get_profile()
    except UserProfile.DoesNotExist:
        profile = UserProfile(user=user)
        profile.save()

    try:
        # Assign user to default ctnr if needed.
        CtnrUser.objects.get(user=request.user,
                             ctnr_id=request.user.get_profile().default_ctnr)
    except CtnrUser.DoesNotExist:
        new_default_ctnr = Ctnr.objects.get(id=2)
        CtnrUser(user=request.user, ctnr=new_default_ctnr, level=0).save()

    # Set session ctnr.
    default_ctnr = request.user.get_profile().default_ctnr
    if default_ctnr:
        request.session['ctnr'] = Ctnr.objects.get(id=default_ctnr.id)
    else:
        request.session['ctnr'] = Ctnr.objects.get(id=2)

    # Set session ctnr level.
    request.session['level'] = CtnrUser.objects.get(user=request.user,
                                                    ctnr=default_ctnr).level

    try:
        # Set ctnr list (to switch between).
        global_ctnr = CtnrUser.objects.get(user=request.user, ctnr=1)
        if global_ctnr:
            request.session['ctnrs'] = (list(Ctnr.objects.filter(Q(id=1)
                | Q(id=2))) + list(Ctnr.objects.exclude(Q(id=1)
                | Q(id=2)).order_by("name")))

    except CtnrUser.DoesNotExist:
        # Set ctnr list (to switch between).
        ctnrs_user = CtnrUser.objects.filter(user=request.user)
        ctnrs = [Ctnr.objects.get(id=ctnr_pk) for ctnr_pk in
                 ctnrs_user.values_list('ctnr', flat=True)]
        request.session['ctnrs'] = ctnrs

    return request
示例#4
0
def login_session(request, username):
    """Logs in a user and sets up the session."""
    try:
        # Authenticate / login.
        user = User.objects.get(username=username)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
    except User.DoesNotExist:
        if not settings.TESTING:
            messages.error(request, "User %s does not exist" % (username))
        return request

    try:
        # Create user profile if needed.
        request.user.get_profile()
    except UserProfile.DoesNotExist:
        profile = UserProfile(user=user)
        profile.save()

    try:
        # Assign user to default ctnr if needed.
        CtnrUser.objects.get(user=request.user,
                             ctnr_id=request.user.get_profile().default_ctnr)
    except CtnrUser.DoesNotExist:
        new_default_ctnr = Ctnr.objects.get(id=2)
        if not CtnrUser.objects.filter(
                user=request.user, ctnr=new_default_ctnr).exists():
            CtnrUser(user=request.user, ctnr=new_default_ctnr, level=0).save()

    # Set session ctnr.
    default_ctnr = request.user.get_profile().default_ctnr
    if default_ctnr:
        request.session['ctnr'] = Ctnr.objects.get(id=default_ctnr.id)
    else:
        request.session['ctnr'] = Ctnr.objects.get(id=2)

    if request.session['ctnr'].name == "default":
        default_ctnr = Ctnr.objects.get(name="global")
        request.session['ctnr'] = default_ctnr

    # Set session ctnr level.
    try:
        level = CtnrUser.objects.get(user=request.user,
                                     ctnr=default_ctnr).level
    except CtnrUser.DoesNotExist:
        level = LEVEL_GUEST

    request.session['level'] = level

    try:
        CtnrUser.objects.get(user=request.user, ctnr=1)
        ctnrs = Ctnr.objects.order_by("name")

    except CtnrUser.DoesNotExist:
        # Set ctnr list (to switch between).
        ctnrs_user = CtnrUser.objects.filter(user=request.user)
        ctnrs = ctnrs_user.values_list('ctnr', flat=True)
        ctnrs = Ctnr.objects.filter(id__in=ctnrs).order_by('name')

    global_ctnr = Ctnr.objects.get(id=1)
    ctnrs = ctnrs.exclude(Q(id=2) | Q(id=1))
    ctnrs = [global_ctnr] + list(ctnrs)

    request.session['ctnrs'] = ctnrs

    return request