def community_request(current_user, user_id): """ api to request to join community :param current_user: :param user_id: :return: """ community = Community(community_id=user_id, user_id=current_user.id) community.save() return response('success', 'Successfully joined community', 200)
def get_community(current_user): response = [] community = Community.get_community(current_user.id) for person in community: response.append({ 'user_id': person.user_id, 'community_id': person.community_id }) return jsonify(response), 200
def getuserprofileid(current_user, userid): global user_steps isFollowing = False community = Community.get_community(current_user.id) for person in community: if person.community_id == userid: isFollowing = True count = Community.get_community_count(userid) post_count = Post.get_post_count(userid) steps = Steps.get_steps(userid, 1) user = User.get_by_id(userid) for item in steps: user_steps = item.steps_no data = { 'user_id': userid, 'username': User.getusername(userid), 'community': count, 'posts': post_count, 'isFollowing': isFollowing, 'full_name': user.first_name + " " + user.last_name, 'steps_today': user_steps } return jsonify(data), 200
def getuserprofile(current_user): global user_steps count = Community.get_community_count(current_user.id) post_count = Post.get_post_count(current_user.id) steps = Steps.get_steps(current_user.id, 1) for item in steps: user_steps = item.steps_no data = { 'user_id': current_user.id, 'username': current_user.username, 'community': count, 'posts': post_count, 'steps_today': user_steps } return jsonify(data), 200
def getuser(): global user_steps values = request.get_json() required = ['user_id'] if not all(k in values for k in required): return 'Missing values', 400 user = User.get_by_id(values.get('user_id')) count = Community.get_community_count(values.get('user_id')) post_count = Post.get_post_count(values.get('user_id')) steps = Steps.get_steps(values.get('user_id'), 1) for item in steps: user_steps = item.steps_no if user: data = { 'user_id': user.id, 'username': user.username, 'community': count, 'posts': post_count, 'steps_today': user_steps } else: return response('failed', 'User Does not exist', 401) return jsonify(data), 200
def search(current_user): isFollowing = False values = request.get_json() required = ['username'] if not all(k in values for k in required): return 'Missing values', 400 user = User.get_by_username(values.get('username')) if user: community = Community.get_community(current_user.id) for person in community: if person.community_id == user.id: isFollowing = True data = { 'user_id': user.id, 'username': user.username, 'community': isFollowing, 'profile_pic': user.profile_pic } else: return response('failed', 'User Does not exist', 401) return jsonify(data), 200
def getpost(current_user): """ get posts :return: """ posts = [] comments = [] post = Post.get_posts(current_user.id) community = Community.get_community(current_user.id) is_liked = False for item in post: comments.clear() comment = Comments.getcomments(item.id) user = User.get_by_id(item.user_id) likers = Likes.getlikers(post_id=item.id) for liker in likers: if liker.user_id == current_user.id: is_liked = True for c in comment: comments.append({ 'comment': c.comment, 'username': User.getusername(c.user_id), 'create_at': c.create_at }) posts.append({ 'post_id': item.id, 'description': item.description, 'image_url': item.image_url, 'create_at': item.create_at, 'user_id': item.user_id, 'profile_pic': user.profile_pic, 'username': user.username, 'likes': item.likes, 'comments': comments, 'is_liked': is_liked }) for person in community: data = Post.get_posts(person.community_id) for i in data: comments.clear() comment = Comments.getcomments(i.id) user = User.get_by_id(i.user_id) likers = Likes.getlikers(post_id=i.id) for liker in likers: if liker.user_id == current_user.id: is_liked = True for c in comment: comments.append({ 'comment': c.comment, 'username': User.getusername(c.user_id), 'create_at': c.create_at }) posts.append({ 'post_id': i.id, 'description': i.description, 'image_url': i.image_url, 'create_at': i.create_at, 'user_id': i.user_id, 'profile_pic': user.profile_pic, 'username': user.username, 'likes': i.likes, 'comments': comments, 'is_liked': is_liked }) return jsonify(posts), 200