def ShowPosts(request, channel_id): if request.method == 'POST': form=newPostForm(request.POST, request.FILES) if form.is_valid(): form_save=form.save() posts=Post.objects.filter(channel__in=channel_id) posts=posts.order_by('-pub_date') form_post=newPostForm(initial={'channel':channel_id,'user':request.user.fsuser}) likes=Like.objects.all() return render( request, 'posts.html', {"posts": posts,"form_post":form_post,"likes":likes} )
def make_post(user_id): form = newPostForm() if request.method == "POST": if form.validate_on_submit(): user = Users.query.filter_by(user_id=user.id).first() description = request.form['description'] photo = request.files['photo'] now = datetime.datetime.now() post_picture = secure_filename(photo.filename) post = Posts(userID=user, photo=post_picture, caption=description, created_on=now) db.session.add(post) db.session.commit(post) photo.save(os.path.join(filefolder, post_picture)) post_body = [{"message": "Successfully created a new post"}] return jsonify(result=post_body) elif request.method == "GET": user_posts = Users.query.filter_by(user_id=user_id).all() if not user: return jsonify({'message': "no user found"}) # output list to hold dictionary output = [] for user_post in user_posts: # create dictionary post_data = {} # add data from database to dictionary user_post['id'] = user_post.id user_post['userID'] = user_post.userID user_post['photo'] = user_post.photo user_post['caption'] = user_post.caption user_post['created_on'] = user_post.created_on output.append(post_data) return jsonify({'posts': output}) error_collection = form_errors(form) error = [{'errors': error_collection}] return jsonify(errors=error)
def make_post(user_id): form = newPostForm() if request.method == "POST": if form.validate_on_submit(): userID = user_id caption = request.form['caption'] photo = request.files['photo'] now = datetime.datetime.now() post_picture = secure_filename(photo.filename) post = Posts(userID=userID, photo=post_picture, caption=caption, created_on=now) db.session.add(post) db.session.commit() photo.save(os.path.join(filefolder, post_picture)) return jsonify({'message': "Successfully created a new post"}) elif request.method == "GET": user = Users.query.filter_by(id=user_id).first() if not user: return jsonify({'message': "no user found"}) user_posts = Posts.query.filter_by(userID=user_id).all() # output list to hold dictionary output = [] for user_post in user_posts: # create dictionary post_data = { 'id': user_post.id, 'userID': user_post.userID, 'photo': user_post.photo, 'caption': user_post.caption, 'created_on': user_post.created_on } output.append(post_data) return jsonify(data=output) error_collection = form_errors(form) error = [{'errors': error_collection}] return jsonify(errors=error)
def make_post(user_id): form = newPostForm() db.ses if form.validate_on_submit(): user = Users.query.filter_by(user_id=user.id).first() description = request.form['description'] photo = request.files['photo'] now = datetime.datetime.now() post_picture = secure_filename(photo.filename) post = Posts(userID = user, photo = post_picture, caption = description, created_on = now) db.session.add(post) db.session.commit(post) photo.save(os.path.join(filefolder, post_picture)) post_body = [{"message": "Successfully created a new post"}] return jsonify(result=post_body)