def add_post(): form = PostForm(request.form) if form.validate(): user = User.get(User.id == current_user.get_id()) md = form.post.data cleaned_markdown = clean_markdown(md) html = markdown.markdown(cleaned_markdown) result = user.create_post(html) return result else: return form.errors
def post_new(): form = PostForm() if form.validate_on_submit(): image = add_image(form.file.data, "labs") post = Post(name=form.name.data, caption=form.caption.data, image=image) db.session.add(post) db.session.commit() return redirect(url_for("posts.post_home")) return render_template("viewsPosts/PostForm.html", form=form, title="Create A Post")
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data.strip(), content=form.content.data.strip(), author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post', editing=False)
def new_post(): form = PostForm() if form.validate_on_submit(): pitch = Pitch(title=form.title.data, content=form.content.data, author=current_user, category=form.category.data) db.session.add(pitch) db.session.commit() flash('Your post has been created', 'successfully ') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title='Update Post', form=form, legend='Update Post')
def add_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, subtitle=form.subtitle.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash("The Post has been submitted", 'success') return redirect(url_for('posts.home')) return render_template('add_post.html', form=form, form_title="Add a new post")
def post_edit(post_id): form = PostForm() post = Post.query.get(post_id) if form.validate_on_submit(): new_image = add_image(form.file.data, "labs") post.name = form.name.data post.caption = form.caption.data post.image = new_image db.session.commit() return redirect(url_for("posts.post_home")) elif request.method == "GET": form.name.data = post.name form.caption.data = post.caption form.file.data = post.image return render_template("viewsPosts/PostForm.html", form=form)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) else: form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash(f"Post Updated!", category="success") return redirect(url_for("posts.post", post_id=post.id)) elif request.method == "GET": form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title="Update Post", form=form, legend="Update Post")
def list_posts(): """ shows all posts and lets signed-in users post a picture, text or both """ form_post = PostForm() all_posts = PostsModel.query.all() authors = UserModel if request.method == 'POST' and check_auth(): if form_post.text.data or form_post.media.data: text = form_post.text.data media = form_post.media.data time = datetime.now() if media: media = save_file( current_user.username, media, 'post_uploads' ) # saves file to directory, returns filename PostsModel.add(time, text, media, current_user.id) return redirect('/posts') else: return render_template('posts.html', pages=generate_pages(), form=form_post, all_posts=all_posts, authors=authors)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post( title=form.title.data, content=form.content.data, author=current_user ) db.session.add(post) db.session.commit() logger.debug("Created post %r by %r", post.title, post.author.username) flash("Your post haas been created!", "success") return redirect(url_for("main.home")) return render_template( "create_post.html", title="New Post", form=form, legend="New Post" )
def new_post(): form = PostForm() if form.validate_on_submit(): pic =None if form.image.data: picture_file = save_picture(form.image.data) final_pic = picture_file pic= final_pic post = Post(title=form.title.data, content=form.content.data, author=current_user, category=form.category.data, image=pic) db.session.add(post) db.session.commit() flash('Your post has been published!', 'success') return redirect(url_for('main.home')) myposts = Post.query.order_by(Post.posted_date.desc()) return render_template('new-post.html', title='New Post', form=form, legend='New Post', myposts=myposts)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash("Your post has been updated!", "success") return redirect(url_for("post", post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template("create_post.html", title="Update Post", form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post( title=form.title.data, content=form.content.data, author=current_user # using the backref of author set on the post # model, you can decide to use the user_id column if you want ) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('common.home')) return render_template('create_post.html', title='New Post', form=form, legend='Update Post')
def delete_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() db.session.delete(post) db.session.commit() flash('Your post has been deleted!', 'success') return redirect(url_for('main.home'))
def editpost(id): post = Post.query.get_or_404(id) if current_user != post.author: abort(403) category_list = [(g.id, g.title) for g in Category.query.all()] form = PostForm() form.category.choices = category_list if form.validate_on_submit(): post.title = form.title.data post.category_id = form.category.data post.body_html = form.post.data db.session.add(post) db.session.commit() flash(_('The post has been updated.')) return redirect(url_for('posts.post', id=post.id)) form.title.data = post.title form.post.data = post.body_html return render_template('edit_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): flash('Your post has been created!','success') hashtag='' hashtags= form.content.data print(hashtags) newstr= hashtags.split() for char in newstr: if char.startswith("#"): hashtag=char.strip("#") print(hashtag) post = Pitch(title = form.title.data, content= form.content.data, author = current_user,hashtags=hashtag) db.session.add(post) db.session.commit() return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post',form=form,legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): picture_file = '/static/background_pics/default.jpg' if form.picture.data: picture_file = save_picture(form.picture.data) post = Post(title=form.title.data, content=form.content.data.replace('`', ''), author=current_user, image_file=picture_file) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.root')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() picture_file = 'blog.jpg' if form.validate_on_submit(): print(form.picture.data) if form.picture.data: picture_file = save_picture(form.picture.data) post = Post(title=form.title.data, content=form.content.data, category=form.category.data, summary=form.summary.data, image_file=picture_file, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', form=form, legend="Create Post")
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data post.date_posted = datetime.utcnow() db.session.commit() flash("Post Updated successfully!", 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title='Edit Post', legend="Edit Post", form=form)
def update_post(id): post = Post.query.get_or_404(id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data post.description = form.description.data post.labels = form.labels.data post.updated_date = datetime.utcnow() db.session.commit() flash('Post updated!', 'success') return redirect(url_for('posts.show_post', id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content form.description.data = post.description form.labels.data = post.labels return render_template('create_post.html', form=form, legend='Update Post')
def editpost(): if g.auth is not None and g.auth.role is not USER.ADMIN: return redirect(url_for('not_found')) if request.method == 'POST': try: form = request.args title = form.get('title') pid = form.get('drafts') dopost = form.get('dopost') post = Post.query.filter_by(title=title).first() if post is None: post = Post.query.filter_by(id=pid).first() if post is None: post = Post() post.user_id = g.user.id post.title = title post.textbody = form.get('textbody').encode('u8') post.htmlbody = form.get('htmlbody').encode('u8') if dopost == True or dopost == "true": post.draft = False post.posted = datetime.datetime.now() db.session.add(post) else: post.title = title post.textbody = form.get('textbody').encode('u8') post.htmlbody = form.get('htmlbody').encode('u8') if dopost == True or dopost == "true": post.draft = False post.posted = datetime.datetime.now() db.session.commit() return make_response("Post Saved") except: print >> sys.stderr, sys.exc_info() return make_response("Exceptional Error") pid = request.args.get('pid') form = PostForm() form.drafts.choices = [ (p.id, p.title) for p in Post.query.filter_by(draft=True).order_by(Post.title).all() ] form.drafts.choices.insert(0, (-1, "New Post")) return render_template("posts/editpost.html", form=form, user=g.user, admin=g.admin, pid=pid)
def new_post(id): form = PostForm() user = User.query.get_or_404(id) if form.validate_on_submit(): picture_file = save_picture(form.picture.data) post = Post(city=form.city.data, image_file=picture_file, category=form.category.data, summary=form.summary.data, title=form.title.data, story=form.story.data, Protagonist=user) db.session.add(post) db.session.commit() flash('Your Solution has been created!', 'success') return redirect(url_for('main.solutions')) return render_template('posts/create_post.html', title='New Post', form=form, legend='Create Solution')
def updating_post(post_id): post = Post.query.get_or_404(post_id) # after updating the post if post.author != current_user: abort(403) # forbidden route form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been update!', 'success') # redirect to detail page return redirect(url_for('posts.post', post_id=post_id)) # before updating the post elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('post_template.html', title='Updating Post', form=form, legend="Update Post")
def new_post(): form = PostForm() tagForm = TagForm() tags = Tag.query.all() form.tags.choices = [(tag.id, tag.value) for tag in tags] if form.validate_on_submit(): post = Post(title=form.title.data, content=repr(form.content.data), author=current_user, tags=Tag.query.filter(Tag.id.in_(form.tags.data)).all()) db.session.add(post) db.session.commit() flash(f'Your Post has been created!!', category='success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', legend='New Post', form=form, tags=tags, tagForm=tagForm)
def edit_post(id): post = Post.query.get(id) form = PostForm(obj=post) if form.validate_on_submit(): tags_names = form.tags.data tags = [] if "," in tags_names: tags_names = tags_names.split(",") elif tags_names == "": tags_names = [] else: tags_names = [tags_names] # if len(tags_names) == 1: # if Tag.query.filter_by(name=tags_names[0]).first(): # tags.append(Tag.query.filter_by(name=tags_names[0]).first()) # else: # new_tag = Tag(tags_names[0]) # tags.append(new_tag) # db.session.add(new_tag) # db.session.commit() # else: for tag_name in tags_names: if Tag.query.filter_by(name=tag_name).first(): tags.append(Tag.query.filter_by(name=tag_name).first()) else: new_tag = Tag(tag_name) tags.append(new_tag) db.session.add(new_tag) db.session.commit() post.title = form.title.data post.body = form.body.data post.tags = tags db.session.add(post) db.session.commit() flash("Post updated successfully") return redirect(url_for("posts.list_posts")) else: return render_template( "posts/form.html", form=form, url=url_for("posts.edit_post", id=post.id), post_id=post.id )
def edit_post(post_id): posts = post.query.get_or_404(post_id) #Check if the current user is the author of the post if post.author != current_user: abort(403) #http response for forbidden route form = PostForm() if request.method == "GET": form.title.data == post.title form.subtitle.data == post.subtitle form.content.data == post.text elif form.validation_on_submit(): post.title = form.title.data post.subtitle = form.subtitle.data post.content = form.text.data db.session.commit() flash("this post has been updated!", 'success') return redirect(url_for('post.single_post', post_id=post.id)) return render_template('add_post.html', form=form, form_title="Edit Post")
def creating_post(): # form을 form wtf을 이용한 forms.py에서 가져와 쓴다. (form은 하나의 클래스에서 가져옴) form = PostForm() # wtf의 class기반의 form을 가져와서 submit한 녀석을 validation print("::::creating post::::::::::::title:::::::::::", form.title.data) print("::::creating post::::::::::::content:::::::::", form.content.data) # print(":::::::", form.validate_on_submit()) if form.validate_on_submit(): # DB에 저장, Post from model print("============= inside===========") print(current_user) post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Post created!!!', 'success') return redirect(url_for('main.home')) # rendering할때 form에다가 wtf에서 만들어낸 클래스를 기반으로 그녀석을 rendering한다. return render_template('post_template.html', title='New Post', form=form, legend="New Post")
def new_post(): form = PostForm() if form.validate_on_submit(): tags_names = form.tags.data tags = [] if "," in tags_names: tags_names = tags_names.split(",") elif tags_names == "": tags_names = [] else: tags_names = [tags_names] if len(tags_names) == 1: if Tag.query.filter_by(name=tags_names[0]).first(): tags.append(Tag.query.filter_by(name=tags_names[0]).first()) else: new_tag = Tag(tags_names[0]) tags.append(new_tag) db.session.add(new_tag) db.session.commit() else: for tag_name in tags_names: if Tag.query.filter_by(name=tag_name).first(): tags.append(Tag.query.filter_by(name=tag_name).first()) else: new_tag = Tag(tag_name) tags.append(new_tag) db.session.add(new_tag) db.session.commit() post = Post(title=form.title.data, body=form.body.data, tags=tags) db.session.add(post) db.session.commit() session["number_of_posts"] = len(Post.query.all()) flash("Post created successfully") return redirect(url_for("posts.list_posts")) else: return render_template("posts/form.html", form=form, url=url_for("posts.new_post"))
def new_post(): form = PostForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) post = Post(title=form.title.data, content=form.content.data, author=current_user, image_file=picture_file) db.session.add(post) db.session.commit() else: post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash("Your post has been created!", 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title="New Post", form=form, legend='New Post')
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): if form.image.data: picture_file = save_picture(form.image.data) final_pic = picture_file post.title = form.title.data post.content = form.content.data post.category = form.category.data post.image = final_pic db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content form.category.data = post.category myposts = Post.query.order_by(Post.posted_date.desc()) return render_template('new-post.html', title='Update Post', form=form, legend='Update Post', myposts=myposts)
def edit_post(slug): post = Post.query.filter(Post.slug == slug).first() if post.author.username == current_user.username: if request.method == 'POST': form = PostForm(formdata=request.form, obj=post) form.populate_obj(post) post.updated = datetime.utcnow() db.session.commit() return redirect(url_for('posts.news_detail.html, post=post')) form = PostForm(obj=post) return render_template('posts/edit_post.html', edit_post_form=form, post=post) return redirect(url_for('posts.news'))
def list_posts(): user = None form_post = PostForm() all_posts = PostsModel.query.all() authors = UserModel try: if session['username']: user = UserModel.query.filter_by( username=session['username']).first() if request.method == 'POST': if form_post.text.data is None and form_post.media.data is None: pass else: text = form_post.text.data media = form_post.media.data time = datetime.now() print(33) if media: print(35) media_title = secure_filename( f'{user.username}_{media.filename}') print(37) media.save( f'app/static/uploads/post_uploads/{media_title}') print(39) else: print(41) media_title = None print(43) received_data = (time, text, media_title, user.id) print(45) create(received_data, PostsModel) print(47) return redirect('/posts') except: pass return render_template('posts.html', pages=pages_nav_list, user=user, form=form_post, all_posts=all_posts, authors=authors)