def posts(user_id): error=None form = PostsForm() if request.method =='POST': if form.validate_on_submit(): photo = form.photo.data caption = form.caption.data filename = secure_filename(photo.filename) added_on=datetime.datetime.now() newpost=Posts(user_id=user_id,photo=filename,caption=caption,created_on=added_on) photo.save(os.path.join(uploadfolder, filename)) db.session.add(newpost) db.session.commit() return jsonify(response="Your post was added successfully") else: return jsonify({'errors':form_errors(form)}) if request.method=='GET': def following(user_id): isfollower=Follows.query.filter_by(follower_id=session['userid'],user_id=user_id).first() if isfollower: return True else: return False user=Users.query.filter_by(id=user_id).first() if not user: return jsonify(error={'error':'User does not exist'}); else: userinfo={'userid':user.id,'username':user.username,'first_name':user.first_name,'last_name':user.last_name,'location':user.location,'photo':uploadfolder+user.profile_photo,'biography':user.biography,'membersince':user.joined_on.strftime("%B %Y")} posts=[{'photo':uploadfolder+x.photo,'caption':x.caption} for x in Posts.query.filter_by(user_id=user_id).all()] follows=Follows.query.filter_by(user_id=user_id).all() return jsonify(response={'userinfo':userinfo,'posts':posts,'numposts':len(posts),'numfollowers':len(follows),'following':following(user_id)})
def newPost(): error = None form = PostsForm(CombinedMultiDict((request.files, request.form))) if request.method == 'POST' and form.validate_on_submit(): if form.photo.data: photo = form.photo.data caption = form.caption.data if photo.filename == '': error = 'No selected file' if photo and allowed_file(photo.filename): filename = secure_filename(photo.filename) newpost = Posts(user_id=current_user.id, image_URI=photo, caption=caption) photo.save(os.path.join(newpost.image_URI, filename)) db.session.add(newpost) db.session.commit() return jsonify({'messages': 'Photo Post successfully'}) else: error = 'File not allowed' return jsonify({'errors': error}) else: caption = form.caption.data newpost = Posts(user_id=current_user.id, caption=caption) db.session.add(newpost) db.session.commit() return jsonify({'messages': 'Post successfully'}) else: return jsonify({'errors': form_errors(form)})
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)
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
def add(): form = PostsForm() if form.validate_on_submit(): post_data = Posts(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post_data) db.session.commit() return redirect(url_for('home')) else: return render_template('post.html', title='add a post', form=form)
def edit(id): post=Post.query.get_or_404(id) if current_user!=post.author and not current_user.can(Permission.ADMINISTER): abort(403) form=PostsForm() if form.validate_on_submit(): post.body=form.body.data db.session.add(post) flash("you have re-edited the blog text!") return redirect(url_for('.post',id=post.id)) form.body.data=post.body return render_template("edit.html",form=form)
def add(): form = PostsForm() if form.validate_on_submit(): post_data = Posts( f_name=form.f_name.data, l_name=form.l_name.data, title=form.title.data, content=form.content.data, ) db.session.add(post_data) db.session.commit() return redirect(url_for('home')) else: return render_template('post.html', title='Add a post', form=form)
def index(): form=PostsForm() if form.validate_on_submit(): post=Post(body=form.body.data,author=current_user._get_current_object()) db.session.add(post) return redirect(url_for('.index')) page=request.args.get('page',1,type=int) show_followed = False if current_user.is_authenticated: show_followed = bool(request.cookies.get('show_followed', '')) if show_followed: query = current_user.followed_posts else: query = Post.query pagination=Post.query.order_by(Post.date.desc()).paginate(page,per_page=current_app.config['FLASKY_POSTS_PER_PAGE']) posts=pagination.items return render_template("index.html",form=form,posts=posts,pagination=pagination,show_followed=show_followed)
def newPost(user_id): error = None form = PostsForm() if request.method == 'POST' and form.validate_on_submit(): photo = form.photo.data caption = form.caption.data if photo.filename == '': error = 'No selected file' if photo and allowed_file(photo.filename): filename = secure_filename(photo.filename) newpost = Posts(user_id=user_id, photo=photo, caption=caption) file.save(os.path.join(newpost.post_URI, filename)) db.session.add(newpost) db.session.commit() return jsonify(message="Post successfully") else: error = 'File not allowed' return jsonify({'errors': error}) else: return jsonify({'errors': form_errors(form)})
def addPost(): form = PostsForm() if request.method =='POST' and form.validate_on_submit(): photo = form.photo.data caption = form.caption.data