def process_signup(user_payload): try: # set default image current_directory = os.path.dirname(__file__) filename = os.path.join( current_directory, "../../static/images/default_profile_picture.png") image = open(filename, "r+b") user_payload['password'] = encrypt_password( user_payload.get('password')) user_payload['display_picture'] = image UserMethods.create_record(**user_payload) except IntegrityError: db.session.rollback() raise Conflict("User already registered")
def get_user_post(username, post_id): current_user = generate_user_from_auth_token( request.headers.get('access-token')) post_user = UserMethods.get_user_by_username(username) if current_user.id == post_user.id or \ post_user.status.value == "public" or \ FollowerMethods.is_following(current_user.id, post_user.id): # send all data like comments, likes and post details if not PostMethods.get_record_with_id(post_id): raise NotFound("Post not found") post = PostMethods.get_record_with_id(post_id) comments = post.post_comments like_count = post.total_number_of_likes likes = post.post_likes post_data = dict(caption=post.caption, image=post.image._public_url, post_id=post.id, user_id=post.user_id) response = dict(post=post_data, comments=comments, like_count=like_count, likes=likes) return jsonify(response) else: return jsonify( dict(message="Profile is private! Follow to see the post !")), 400
def generate_user_from_auth_token(auth_token): payload = decode_auth_token( auth_token, Entity.USER.value ) current_username = payload.get('entity_id') current_user = UserMethods.get_user_by_username(current_username) return current_user
def custom_feed(): auth_token = request.headers.get('access_token') current_user = generate_user_from_auth_token(auth_token) # HIT REDIS FOR ALL POSTS RELATED T followees = FollowerMethods.get_followees(current_user.id) posts = [] for followee in followees: user = UserMethods.get_record_with_id(followee.followee_id) start_time = datetime.utcnow() user_posts = redis_client.lrange(user.username, 0, -1) print(datetime.utcnow() - start_time) for post in user_posts: posts.append(json.loads(post)) return jsonify({"posts": posts})
def unlike_action_on_post(username, post_id): current_user = generate_user_from_auth_token( request.headers.get('access-token')) post_user = UserMethods.get_user_by_username(username) if current_user.id == post_user.id or \ post_user.status.value == "public" or \ FollowerMethods.is_following(current_user.id, post_user.id): # send all data like comments, likes and post details if not PostMethods.get_record_with_id(post_id): raise NotFound("Post not found") LikeMethods.delete_record(post_id, post_user.id) db.commit() return jsonify(dict(message="Successfully unliked the post")) else: return jsonify( dict(message="Profile is private! Follow to unlike the post !") ), 400
def feed(): auth_token = request.headers.get('access_token') current_user = generate_user_from_auth_token(auth_token) # HIT REDIS FOR ALL POSTS RELATED TO followees = FollowerMethods.get_followees(current_user.id) posts = [] for followee in followees: user = UserMethods.get_record_with_id(followee.followee_id) start_time = datetime.utcnow() user_posts = db.query(Post).filter(Post.user_id == user.id).all() print(datetime.utcnow() - start_time) for post in user_posts: posts.append( dict(caption=post.caption, image=post.image._public_url, user_id=post.user_id)) return jsonify({"posts": posts})
def unfollow(followee_username): follower_user = generate_user_from_auth_token( request.headers.get('access-token')) followee_user = UserMethods.get_user_by_username(followee_username) if followee_user is None: raise NotFound('No user with username {username} found'.format( username=followee_username)) # same user id unfollow request if follower_user.id == followee_user.id: raise BadRequest('Request cannot be proceesed') # check if it follows if FollowerMethods.is_following(follower_user.id, followee_user.id): unfollow_user(follower_user.id, followee_user.id) db.commit() return jsonify({"message": "Successfully unfollowed"}), 200
def user_profile(username): current_user = generate_user_from_auth_token( request.headers.get('access-token')) profile_user = UserMethods.get_user_by_username(username) response = dict( name=profile_user.name, username=profile_user.username, display_picture=dict(profile_user.display_picture) ) # HATEOS CONCEPT USED FOR POSTS # no circular checking if current_user.username != username: # check if following if FollowerMethods.is_following(current_user.id, profile_user.id): response['posts'] = profile_user.all_posts else: response['posts'] = profile_user.all_posts return response
def follow(followee_username): follower_user = generate_user_from_auth_token( request.headers.get('access-token')) followee_user = UserMethods.get_user_by_username(followee_username) if followee_user is None: raise NotFound('No user with username {username} found'.format( username=followee_username)) # same user id follow request if follower_user.id == followee_user.id: raise BadRequest('Request cannot be proceesed') # check if not follows if not FollowerMethods.is_following(follower_user.id, followee_user.id): if followee_user.status.value == "public": follow_public_user(follower_user.id, followee_user.id) db.commit() return jsonify({"message": "Successfully followed"}), 200 else: send_follow_request(follower_user.id, followee_user.id) db.commit() return jsonify({"message": "Follow request sent"}), 200