def change_password(request): """ change user account password :param request: :return: """ new_password=request.POST['new_password'] old_password=request.POST['old_password'] username=request.POST['username'] response=dict() user=authenticate(username=username, password=old_password) token_user=request.user if not user: print "auth error" return error_response("Invalid username or old password") else: user.set_password(new_password) user.save() response['success']=True response['message']="success" return package_handle(response)
def get_userprofile_detail(request): """ :param :return: """ response=dict() response['success'] = False user=request.user my_profile=user.profile try: target_username=request.POST['target_username'] except KeyError: account.api_account.error_response("provide target_username") try: target_userprofile=UserProfile.objects.get(user__username=target_username) except UserProfile.DoesNotExist: account.api_account.error_response("target_username does not exist") package=target_userprofile.get_userprofile_info(my_profile) response['success']=True response['message']="success" response['package']=dict() for key, value in package.iteritems(): response['package'][key]=value return package_handle(response)
def change_userprofile_info(request): profile=request.user.profile package=request.POST['package'] package=ast.literal_eval(package) for k, v in package.items(): profile.change_userprofile_info(k,v) response=dict() response['success']=True response['message']="success" return package_handle(response)
def account_login(request): """ :param request: GET method :return: """ response=dict() username = request.POST['username'] password = request.POST['password'] user=authenticate(username=username, password=password) response['message']="" response['userid']="" response['success']=False if user is not None: if user.is_active: response['success']=True response['message']="sucess" try: exist_token = Token.objects.get(user=user) except Token.DoesNotExist: return error_response("server error") exist_token.delete() token=Token.objects.create(user=user) token.created = datetime.datetime.utcnow() response['token']=token.key else: # Return a 'disabled account' error message response['message']="disabled account" else: # Return an 'invalid login' error message. response['message']="invalid login" return package_handle(response)
def get_friend_list(request): """ :param request: :return: list of friend usernames """ user=request.user friend_list = user.profile.get_follower() package=",".join(friend_list) response=dict() response['success']=True response['message']="success" response['package']=package return package_handle(response)
def delete_account(request): """ delete user account and its related profile :param request: :return: """ user=request.user profile=user.profile user.delete() try: profile.delete() except: pass response=dict() response['success']=True response['message']="success" return package_handle(response)
def account_logout(request): """ log user out, reset access token :param request: POST method :return: """ response=dict() response['success']=False user=request.user try: exist_token = Token.objects.get(user=user) except Token.DoesNotExist: return error_response("server error") exist_token.delete() Token.objects.create(user=user) response['success']=True response['message']="success" return package_handle(response)
def create_account(request): """ :param request: POST method json format '{"username": "******", "email":"email_value" , "password": "******""" response=dict() response['success']=False response['message']="" try: username = request.POST['username'] password = request.POST['password'] email= request.POST['email'] if User.objects.filter(username=username).exists(): return error_response("username exists") if User.objects.filter(email=email).exists(): return error_response("email exists") new_user = User.objects.create_user(username=username, email=email, password=password) token=Token.objects.create(user=new_user) new_user_profile = UserProfile(user = new_user) new_user_profile.save() response['success']=True response['message']="success" response['token']=token.key except ValueError: return error_response("Please provide username, password and email") return package_handle(response)
def friend_action(request): """ POST method :param request: :return: """ user=request.user try: action=request.POST['action'] target_username=request.POST['target_username'] except KeyError: return account.api_account.error_response("user POST method to include 'action' and 'target_username'") try: target_userprofile=UserProfile.objects.get(user__username=target_username) except User.DoesNotExist: return account.api_account.error_response("target username does not exist") response=dict() if action=="follow": user.profile.rs_follow(target_userprofile) # todo send friend request notification elif action=="block": user.profile.rs_block(target_userprofile) elif action=="unfollow" or action=="unblock": user.profile.rs_reset(target_userprofile) response['success']=True response['message']="success" return package_handle(response)