def userPosts(userid):
    form = PostsForm()
    if request.method == 'GET':
        user = UserProfile.query.filter_by(id = userid).first()
        if user is not None:
            userposts = UserPosts.query.filter_by(user_id = userid ).items
            if userposts is not None:
                msg = "All post by user successfully fetched"
                er = None
                return jsonify(error=er,message=msg,posts=userposts)
            else:
               er = True
               msg = "User has no posts"
               return jsonify(error=er,message=msg)
        else:
            er=True
            msg = "User does not exist"
            return jsonify(error=er,message=msg)
    elif request.method == 'POST':
        if form.validate_on_submit():
            if current_user.id == userid:
                pic = form.photo.data
                caption =  form.caption.data
                date = format_date_joined(datetime.now())
                newpost  =UserPosts(userid,pic,caption, date)
                db.session.add(newpost)
                db.session.commit()
                
                er = None
                msg = "Post created successfully"
                return jsonify(error=er, message=msg)
            else:
                er=True
                msg = "You can only create posts for yourself. You id is {} and you are trying to create a post for user with the id {}".format(current_user.id, userid)
                return jsonify(error=er , message = msg)
Ejemplo n.º 2
0
def post(user_id):
    """used for adding posts to the users feed"""
    form = PostForm()
    id = int(user_id)
    now = datetime.datetime.now()

    if request.method == 'POST':
        if form.validate_on_submit():
            photo = form.photo.data
            caption = form.caption.data
            filename = secure_filename(photo.filename)
            photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            photo = '/static/uploads/' + filename
            date = now.strftime("%B %d, %Y")
            user = UserPosts(user_id, photo, caption, date)
            db.session.add(user)
            db.session.commit()
            return jsonify(
                response=[{
                    "message": "Successfully created a new post"
                }])
        else:
            return jsonify(errors=[{"errors": form_errors(form)}])
    if request.method == 'GET':
        u = []
        f = [0, 0]
        userdetail = UserProfile.query.filter_by(id=id).first()
        Users = UserPosts.query.filter_by(user_id=id).all()
        length = len(Users)
        followers = UserFollows.query.filter_by(user_id=id).all()
        follow = len(followers)
        for follower in followers:
            f.append(follower.user_id)
        for user in Users:
            u.append({
                'id': user.id,
                'user_id': user.user_id,
                'photo': user.photo,
                'caption': user.caption,
                'created_on': user.created_on,
                'likes': 0
            })
        return jsonify(response=[{
            "id": user_id,
            "username": userdetail.username,
            "firstname": userdetail.first_name,
            "lastname": userdetail.last_name,
            "email": userdetail.email,
            "location": userdetail.location,
            "biography": userdetail.biography,
            "profile_photo": userdetail.profile_photo,
            "joined_on": userdetail.joined_on,
            "posts": u,
            "numpost": length,
            "numfollower": follow,
            "follower": f
        }])
    else:
        return jsonify(error=[{"errors": "unable to create link"}])
Ejemplo n.º 3
0
def userPosts(userid):
    form = PostsForm()
    if request.method == 'GET':
        user = UserProfile.query.filter_by(id=userid).first()
        if user is not None:
            userposts = UserPosts.query.filter_by(user_id=userid)
            if userposts is not None:
                posts_list = []
                for post in userposts:
                    post_creator = UserProfile.query.filter_by(
                        id=post.user_id).first()
                    profile_pic = post_creator.pic
                    likes_count = UserLikes.query.filter_by(
                        post_id=post.id).count()
                    post_dict = {
                        "Post_creator": post_creator.user_name,
                        "profile_pic": profile_pic,
                        "id": post.id,
                        "likes": likes_count,
                        "userid": post.user_id,
                        "pic": post.pic,
                        "caption": post.caption,
                        "created_on": post.created_on
                    }
                    posts_list.append(post_dict)
                msg = "All posts by {} successfully fetched".format(
                    user.user_name)
                er = None
                return jsonify(error=er, message=msg, posts=posts_list)
            else:
                er = True
                msg = "User has no posts"
                return jsonify(error=er, message=msg), 404
        else:
            er = True
            msg = "User does not exist"
            return jsonify(error=er, message=msg), 404
    elif request.method == 'POST':
        if form.validate_on_submit():
            if current_user.id == userid:
                pic = form.photo.data
                caption = form.caption.data
                date = format_date_joined(datetime.now())
                filename = secure_filename(pic.filename)
                pic.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                newpost = UserPosts(userid, filename, caption, date)
                db.session.add(newpost)
                db.session.commit()

                er = None
                msg = "Post created successfully"
                return jsonify(error=er, message=msg), 201
            else:
                er = True
                msg = "You can only create posts for yourself. Your id is {} and you are trying to create a post for user with the id {}".format(
                    current_user.id, userid)
                return jsonify(error=er, message=msg), 401
Ejemplo n.º 4
0
def add_post(user_id):
    user = User.query.get(user_id)

    title = request.form['title']
    post = request.form['post']
    
    if title == "" or post == "":
        flash('Please fill out both title and post')
        return redirect(f'/Home/{user.id}')
    else:
        new_post = UserPosts(title=title, post=post)

        user.posts.append(new_post)

        db.session.commit()

        return redirect(f'/Home/{user.id}')