Exemplo n.º 1
0
def callback(request):
    # Get the state saved in session
    expected_state = request.session.pop('auth_state', '')
    # Make the token request
    token = get_token_from_code(request.get_full_path(), expected_state)

    # Get the user's profile
    user = get_user(token)
    print(profstat)
    print(studstat)
    print(user)
    # Save token and user
    store_token(request, token)
    store_user(request, user)

    if User.objects.filter(username=user['surname']).exists():
        print("Exists")
    else:
        new_user = User()
        new_user.is_student = studstat
        new_user.is_professor = profstat
        new_user.username = user['surname']
        new_user.first_name = user['displayName'].split(" ")[0]
        new_user.last_name = user['displayName'].split(" ")[1]
        new_user.email = user['mail'] if (
            user['mail'] != None) else user['userPrincipalName']
        print(new_user)
        new_user.save()
        store(new_user, studstat, profstat)

    if studstat == True:
        return HttpResponseRedirect(reverse('home'))
    else:
        return HttpResponseRedirect(reverse('profhome'))