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
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)
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")
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']} )
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)
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)
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")
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")
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")
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")