def new_post(): form = PostForm() if request.method == 'POST': if form.validate_on_submit(): post = Post( content=form.content.data, author=User.objects(username=current_user.username).first(), author_name=current_user.username, create_time=datetime.utcnow()) if 'file' in request.files: pic = request.files['pic'] fname = pic.filename ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif'] UPLOAD_FOLDER = current_app.config['UPLOAD_FOLDER'] if not (os.path.isdir(os.path.join(UPLOAD_FOLDER))): os.mkdir(os.path.join(UPLOAD_FOLDER)) if not (os.path.isdir( os.path.join(UPLOAD_FOLDER, current_user.username))): os.mkdir(os.path.join(UPLOAD_FOLDER, current_user.username)) flag = '.' in fname and \ fname.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS if not flag: flash('错误的文件类型') return render_template('post/new_post.html', form=form) pic.save('{}{}/{}'.format(UPLOAD_FOLDER, current_user.username, fname)) #linux目录 post.pic = '/static/post_pic/{}/{}'.format( current_user.username, fname) post.save() flash('发布成功') return redirect(url_for('post_main.post_view')) return render_template('post/new_post.html', form=form, title='发微博')
def post_new(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.body.data, category=form.category.data) db.session.add(post) db.session.commit() flash('Added new Post entry!') return redirect(url_for('post.post_list')) return render_template('post_ed.html', form=form)
def new(): method = 'POST' action = '.' form = PostForm(request.form) if form.validate_on_submit(): post = Post() post = set_post(post,form) print(post) post.put() flash('Post created!') return redirect(url_for('.index')) return render_template("posts/new.html", form=form, method=method, action=action)
def new(): if not current_user.is_authenticated and not current_user.is_confirmed: return redirect(url_for('main.home')) form = PostForm() if form.validate_on_submit(): p = Post(images=form.images.data, caption=form.caption.data, user=current_user) db.session.add(p) db.session.commit() return redirect(url_for('main.home')) return render_template('new.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.body.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index')) return render_template('post/create_post.html', title='New 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) db.session.add(post) db.session.commit() flash('成功发布文章', 'success') return redirect(url_for('main.home')) return render_template('post/create_post.html', title='新文章', form=form, legend='新文章')
def new(): if not (current_user.is_authenticated and current_user.is_confirmed): flash('Go and confirm your email, Baka', 'link') return redirect(url_for('main.home')) form = PostForm() if form.validate_on_submit(): p = Post(images=form.images.data, caption=form.caption.data, user=current_user) db.session.add(p) db.session.commit() flash('New post added', 'success') return redirect(url_for('main.home')) return render_template('new.html', form=form)
def edit(id): method = 'POST' action = '.' post = Post.get_by_id(id) form = PostForm(request.form, post) form.category.data = post.category.name tags = [] for tag in post.tags: tags.append(tag.name) form.tags.data = ','.join(tags) if form.validate_on_submit(): post = set_post(post,form) post.put() flash('Post updated!') return render_template("posts/edit.html", form=form, method=method, action=action)
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.body = form.body.data db.session.commit() flash('Your post has been updated!') return redirect(url_for('post.post_detail', post_id=post.id)) form.title.data = post.title form.body.data = post.body return render_template('post/create_post.html', title='Update Post', form=form)
def post_ed(id): form = PostForm() post = Post.query.get(id) if form.validate_on_submit(): post.body = form.body.data post.category = form.category.data post.title = form.title.data db.session.add(post) db.session.commit() flash('Sucessfuly edited Post id: ' + str(post.id)) return redirect(url_for('post.post_list')) form.title.data = post.title form.category.data = post.category form.body.data = post.body return render_template('post_ed.html', form=form)
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('更新文章成功', '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('post/create_post.html', title='修改文章', form=form, legend='修改文章')
def add_post(): form = PostForm() if form.validate_on_submit(): # write post to db post = Post(heading=form.heading.data, post=form.post.data, author=current_user) db.session.add(post) db.session.commit() # tags should be separated by commas (,) and start with hash (#) tags = form.tags.data tag_list = [t.strip() for t in tags.split(',')] processTags(tag_list, post) flash('Your post has been published!', 'success') return redirect(url_for('main.index')) return render_template('post/add_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data post = Post(title=title, body=body, author=current_user) for category_name in form.categories.data.split(): category = Category.query.filter_by(name=category_name).first() if category is None: category = Category(name=category_name) db.session.add(category) db.session.commit() if category not in post.categories: post.categories.append(category) db.session.add(post) db.session.commit() flash('博文发布成功。', 'success') return redirect(url_for('post.show_post', post_id=post.id)) return render_template('post/new_post.html', form=form)
def add_post(): form = PostForm() if form.validate_on_submit(): heading = form.heading.data slug = slugify(heading) if Post.getPostBySlug(slug) is None: # write post to db post = Post(heading=heading,slug=slug,post=form.post.data,author=current_user) db.session.add(post) db.session.commit() # tags should be separated by commas (,) and start with hash (#) tags = form.tags.data tag_list = [t.strip() for t in tags.split(',')] processTags(tag_list,post) flash('Your post has been published!','success') return redirect(url_for('main.index')) flash('Error post not created as title already exists in database.','danger') return redirect(url_for('main.index')) return render_template('post/add_post.html',form=form)
def edit_post(slug): old_slug = slug post = Post.getPostBySlug(slug) # slug is wrong if post is None: flash('No such post exists.','danger') return redirect(url_for('index')) # users's cannot edit other user's post, not needed in this blog but there anyways if post.author.id != current_user.id: flash("You are not authorised to edit someone else's post",'danger') return redirect(url_for('main.index')) form = PostForm() if form.validate_on_submit(): heading = form.heading.data slug = slugify(heading) # check if heading already exists check_post = Post.getPostBySlug(slug) if (check_post is not None): if (check_post.id != post.id): flash('Error post not created as title already exists in database.','danger') return redirect('/post_detail/' + old_slug) post.heading = heading post.slug = slug post.post = form.post.data post.update_date = datetime.utcnow() db.session.add(post) db.session.commit() # tags should be separated by commas (,) and start with hash (#) tags = form.tags.data tag_list = [t.strip() for t in tags.split(',')] processTags(tag_list,post) flash('Your post has been updated!','success') return redirect('/post_detail/' + slug) tags = post.getTagNamesStr() return render_template('post/edit_post.html',form=form,post=post,tags=tags)
def edit_post(id): # keeps track of whether user came from home page or detail page if request.method == 'GET': session['edit_post'] = request.referrer post = Post.getPost(id) # id is wrong if post is None: flash('No such post exists.', 'error') return redirect(url_for('index')) # users's cannot edit other user's post, not needed in this blog but there anyways if post.author.id != current_user.id: flash("You are not authorised to edit someone else's post", 'error') return redirect(url_for('main.index')) form = PostForm() if form.validate_on_submit(): post.heading = form.heading.data post.post = form.post.data post.update_date = datetime.utcnow() db.session.add(post) db.session.commit() # tags should be separated by commas (,) and start with hash (#) tags = form.tags.data tag_list = [t.strip() for t in tags.split(',')] processTags(tag_list, post) flash('Your post has been updated!', 'success') if session['edit_post'] is not None: return redirect(session['edit_post']) else: return redirect(url_for('main.index')) tags = post.getTagNamesStr() return render_template('post/edit_post.html', form=form, post=post, tags=tags)