Exemple #1
0
def user_login_fb(request):
    """
    Handles the login of a user from Facebook.
    If there is no BMG account for the user, one is created.
    """
    #response = HttpResponse()
    response = HttpResponseRedirect("/dashboard/")
    result = authomatic.login(DjangoAdapter(request, response), "fb")
     

    if result:
        if result.error:
            #TODO - right now this is redirecting anyway
            pass
        elif result.user:
            # Get the info from the user
            if not (result.user.name and result.user.id):
                result.user.update()

            username, email= result.user.name, result.user.email

            if (BeatMyGoalUser.objects.filter(username=username).exists()):
                user = BeatMyGoalUser.objects.get(username=username)
                user.backend='django.contrib.auth.backends.ModelBackend'
                login(request, user)
            else:
                password = BeatMyGoalUser.objects.make_random_password(8)
                user = BeatMyGoalUser.create(username, email, password)['user']
                user =  authenticate(username=username, password=password)
                login(request, user)
                response['Location'] = '/users/profile'

    return response
Exemple #2
0
def create_user(request):
    """
    Creates a user and authenticates them, if credentials are valid.
    """

    if request.method == "GET":
            return render(request, 'index.html')
    elif request.method == "POST":
        data = json.loads(request.body)
        username, email, password = data["username"], data["email"], data["password"]
        response = BeatMyGoalUser.create(username, email, password)

        if response['errors']:
            return HttpResponse(json.dumps(response), 
                                content_type = "application/json")            
        else:
            # user = response['user']
            user =  authenticate(username=username, password=password)
            login(request, user)
            redirect = "/users/%s/" % (user.id)
            return HttpResponse(json.dumps({"redirect" : redirect,
                "errors" : response["errors"]
                }), content_type = "application/json")

    else:
        return HttpResponse("Invalid request", status=500)
Exemple #3
0
def confirm(request):           #add Functional test here
    if request.method == "POST":
        data = json.loads(request.body)
        user = request.user;
        password = data["password"]
        response = BeatMyGoalUser.login(user.username, password)
        return HttpResponse(json.dumps({'errors':response['errors']}), content_type = "application/json")
Exemple #4
0
def view_user(request, uid):
    """ 
    Returns the profile of the user with id, uid. 
    """
    if request.method == "GET":
        response = BeatMyGoalUser.getUserById(uid)
        if response['errors']:
            return render(request, 'users/viewUser.html', { "errors" : response["errors"] })
        else:
            return render(request, 'users/viewUser.html', {'viewedUser' : response['user'], 'errors' : response['errors']} )
Exemple #5
0
def edit_user(request, uid):
    """ 
    Allows users to edit their profile if they are logged in.
    """
    uid = int(uid)
    user = request.user
    #user = BeatMyGoalUser.getUserById(uid)
    if (user.is_authenticated() and user.id == uid):
        if request.method == "GET":
            return render(request, 'users/editUser.html', {
                "username": user.username,
                "email":    user.email,
            })
        elif request.method == "POST":
            data = json.loads(request.body)
            username = data['username']
            email = data['email']
            password = data['password']

            loginResponse = BeatMyGoalUser.login(user.username, password)
            if loginResponse['errors']:
                return HttpResponse(json.dumps({"errors" : loginResponse["errors"]}), content_type = "application/json")


            response = BeatMyGoalUser.updateUser(user, username, email)



            if response['errors']:
                return HttpResponse(json.dumps({'errors': response['errors']}), content_type = "application/json")            
            else:
                redirect = "/users/" + str(uid)
                return HttpResponse(json.dumps({"redirect" : redirect,
                    "errors" : response['errors']
                    }), content_type = "application/json")
        #return HttpResponse(json.dumps(res), content_type = 'application/json', status=200)
    else:
        return HttpResponse("Invalid request", status=500)
Exemple #6
0
def delete_user(request, uid):
    """ 
    Allows users to delete their Userid if they are logged in.
    """
    uid = int(uid)
    if request.method == "POST":
        user = request.user;
        if (user.is_authenticated() and user.id == uid):
            response = BeatMyGoalUser.remove(uid)
            if len(response['errors']) == 0:
                return HttpResponse(json.dumps({"redirect": "/"}), content_type = "application/json")
            else:
                ttpResponse("Invalid request", status=500)
    return HttpResponse("Invalid request", status=500)
Exemple #7
0
def goal_leave_goal(request):       #add Functional test here
    """
    Removes a user as a participant of a goal.
    """
    data = json.loads(request.body)
    goal_id = data["goal_id"]
    user = request.user 
    response = BeatMyGoalUser.leaveGoal(user, goal_id)
    redirect = "/goals/" + str(goal_id)
    if response['errors']:
        return HttpResponse(json.dumps(response),content_type = "application/json")

    return HttpResponse(json.dumps({"errors": response["errors"],
        "redirect" : redirect}), content_type = "application/json")
Exemple #8
0
def goal_remove_favorite(request):       
    """
    Removes a user as a participant of a goal.
    """
    data = json.loads(request.body)
    goal_id = data["goal_id"]
    user = request.user 
    response = BeatMyGoalUser.removeFavorite(user, goal_id)
    print response
    redirect = "/goals/" + str(goal_id)
    if response['errors']:
        return HttpResponse(json.dumps(response),content_type = "application/json")

    return HttpResponse(json.dumps({"errors": response["errors"],
        "redirect" : redirect}), content_type = "application/json")
Exemple #9
0
def goal_add_favorite(request):
    """
    Adds a goal as a favorite goal
    """
    data = json.loads(request.body)
    goal_id = data["goal_id"]
    user = request.user
    response = BeatMyGoalUser.addFavorite(user, goal_id)
    
    redirect = "/goals/" + str(goal_id)
    if response['errors']:
            return HttpResponse(json.dumps(response), 
                                content_type = "application/json")
    return HttpResponse(json.dumps({"errors": response["errors"],
        "redirect" : redirect}), content_type = "application/json")
Exemple #10
0
def user_login(request):
    """
    Authenticates the user credential, login if valid
    """
    # if request.method == "GET":
    #     return render(request, 'users/login.html')

    if request.method == "POST":
        data = json.loads(request.body)
        username= data["username"]
        password= data["password"]
        response = BeatMyGoalUser.login(username,password)
            
        if response['errors']:
            return HttpResponse(json.dumps(response), content_type = "application/json")
        else:
            user = response['user']
            login(request, user)
            return HttpResponse(json.dumps({"errors": response['errors'], 
                                            "redirect" : "/dashboard/"}),
                                content_type = "application/json")