def new_post(): """allows user to create a new post""" form = PostForm() the_post = Post() if form.validate_on_submit(): the_author = Author.get_by_display_name(form.author_display_name.data) the_post.author_id = the_author.id the_post.title = form.title.data the_post.date_written = form.date_written.data the_post.body = form.body.data the_post.language_id = Language.query.filter( Language.name == form.language.data).first().id the_post.country_id = Country.query.filter( Country.name == form.nasod.data).first().id the_post.created_by = current_user.id the_post.modified_by = current_user.id db.session.add(the_post) db.session.commit() return redirect(url_for('post', post_id=the_post.id)) elif request.method == 'GET': the_post.title = form.title.data the_post.date_written = form.date_written.data the_post.body = form.body.data the_post.language = form.language.data return render_template('post.html', form=form, post=the_post, poster=current_user)
def edit_post(post_id): """allows user to edit an existing post""" the_post = Post.query.get(post_id) if request.method == 'GET': form = PostForm( formdata=MultiDict({ 'title': the_post.title, 'date_written': the_post.date_written, 'body': the_post.body, 'author_display_name': the_post.author.display_name, 'language': the_post.language.name, 'nasod': the_post.country.name })) else: form = PostForm() if form.validate_on_submit(): the_author = Author.get_by_display_name(form.author_display_name.data) the_post.author_id = the_author.id the_post.title = form.title.data the_post.date_written = form.date_written.data the_post.body = form.body.data the_post.language_id = Language.query.filter( Language.name == form.language.data).first().id the_post.country_id = Country.query.filter( Country.name == form.nasod.data).first().id the_post.modified_by = current_user.id the_post.modify_timestamp = datetime.utcnow() db.session.commit() return redirect(url_for('post', post_id=post_id)) return render_template('post.html', form=form, post=the_post, author=the_post.author, poster=the_post.poster)
def new_post(): """新建文章视图""" form = PostForm(request.form) if form.validate_on_submit(): form.content.data = request.form['markdownEditor-html-code'] form.categories.data = [Category.query.get(category_id) for category_id in form.categories.data] if not form.description.data: form.description.data = remove_html_tag(form.content.data)[0:150] if form.publish.data: with db.auto_commit(): post = Post() post.set_attr(form.data) db.session.add(post) flash('文章已发布', 'success') if form.save.data: with db.auto_commit(): post = Post() post.set_attr(form.data) post.published = False db.session.add(post) flash('文章已保存为草稿', 'success') return redirect(url_for('web.manage_post')) return render_template('admin/post_editor.html', form=form)
def post_new(): form = PostForm() if request.method == "POST" and form.validate(): 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('post', id=post.id)) return render_template("post_new.html", title="Create New Post", form=form)
def posteditor(): postform = PostForm() if postform.validate_on_submit(): try: npost = PostModel.create_or_update_post(postform) return render_template('post_success.html', post=npost) except Exception as e: logging.error(str(e)) return render_template('error404.html', error=str(e)) else: return render_template('posteditor.html',postform=postform)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category) db.session.add(post) db.session.commit() flash('发表成功.', 'success') return redirect(url_for('web.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def notas(nombre): form = PostForm() if form.validate_on_submit(): post = Post(nota=form.post.data) db.session.add(post) db.session.commit() flash('Se coloc la nota !') return redirect(url_for('notas', nombre=current_user.nombre)) user = Alumno.query.filter_by(nombre=nombre).first_or_404() return render_template('views/profile.html', title=f'Perfil {nombre}', user=user, form=form)
class PostsController(BaseController): @render def show(self): self.post = Post.query.get_or_404(self.params['post_id']) @login_required @render def new(self): self.form = getattr(self, 'form', None) or PostForm() @login_required @render def edit(self): self.post = Post.query.get_or_404(self.params['post_id']) if not self.current_user.can_action_on(self.post): return abort(401) self.form = getattr(self, 'form', None) or PostForm(obj=self.post) @login_required def create(self): self.form = PostForm() if self.form.validate_on_submit(): del self.form post = Post.create(user_id=self.current_user.id, **self._post_params()) return redirect(url_for('topics.show', topic_id=post.topic_id, forum_id=post.topic.forum_id)) else: return self.new() @login_required def update(self): self.form = PostForm() if self.form.validate_on_submit(): del self.form post = Post.query.get_or_404(self.params['post_id']) if not self.current_user.can_action_on(post): return abort(401) post.update(**self._post_params()) return redirect(url_for('topics.show', topic_id=post.topic_id, forum_id=post.topic.forum_id)) else: return self.edit() @admin_required def destroy(self): post = Post.query.get_or_404(self.params['post_id']) topic_id = post.topic_id forum_id = post.topic.forum_id post.delete() return redirect(url_for('topics.show', topic_id=topic_id, forum_id=forum_id)) def _post_params(self): return self.permit(self.params, 'text', 'topic_id')
def edit_post(post_id): form = PostForm() post = Post.query.get_or_404(post_id) if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.category = Category.query.get(form.category.data) db.session.commit() flash('Post updated.', 'success') return redirect(url_for('web.show_post', post_id=post.id)) form.title.data = post.title form.body.data = post.body form.category.data = post.category_id return render_template('admin/edit_post.html', form=form)
def edit_post(id): post = Post.objects.get(pk=id) form = PostForm(obj=post) if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.save() flash('Post updated', 'success') return redirect(url_for('admin.posts')) return render_template('admin/form.html', form=form, endpoint=url_for('.edit_post', id=post.id))
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(author=current_user.id, title=form.title.data, body=form.body.data) post.save() flash('Post created', 'success') return redirect(url_for('admin.posts')) return render_template('admin/form.html', form=form, endpoint=url_for('.new_post'))
def create(self): self.form = PostForm() if self.form.validate_on_submit(): del self.form post = Post.create(user_id=self.current_user.id, **self._post_params()) return redirect(url_for('topics.show', topic_id=post.topic_id, forum_id=post.topic.forum_id)) else: return self.new()
def profile(nombre): form = PostForm() if form.validate_on_submit(): post = Post(alumno=form.alumno.data, asistencia=form.asistencia.data, tipo_examen=form.tipoexamen.data, nota=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Se coloc la nota !') return redirect(url_for('profile', nombre=current_user.nombre)) user = Profesor.query.filter_by(nombre=nombre).first_or_404() return render_template('views/profile.html', title=f'Perfil {nombre}', user=user, form=form)
def edit_post(id: int): post = Post.query.get_or_404(id) if post.author != current_user: abort(403) form = PostForm() if request.method == "POST" and form.validate(): post.title = form.title.data post.content = form.content.data db.session.commit() flash("Your post has been updated!", "info") return redirect(url_for('post', id=post.id)) else: form.title.data = post.title form.content.data = post.content return render_template("edit_post.html", title="Update Post", form=form)
def edit_post(post_id): """ 编辑文章视图 :param post_id: 文章 id """ post = Post.query.get_or_404(post_id) form = PostForm(request.form) if form.validate_on_submit(): form.content.data = request.form['markdownEditor-html-code'] form.categories.data = [ Category.query.get(category_id) for category_id in form.categories.data ] if not form.description.data: form.description.data = remove_html_tag(form.content.data)[0:150] if form.publish.data: with db.auto_commit(): post.set_attr(form.data) post.published = True db.session.add(post) flash('文章已更新', 'success') if form.save.data: with db.auto_commit(): post.set_attr(form.data) post.published = False db.session.add(post) flash('文章已保存为草稿', 'success') return redirect(url_for('web.manage_post')) if not form.errors: form.title.data = post.title form.categories.data = [category.id for category in post.categories] form.content_markdown.data = post.content_markdown form.description.data = post.description form.can_comment.data = post.can_comment return render_template('admin/post_editor.html', form=form, post=post)
def update(self): self.form = PostForm() if self.form.validate_on_submit(): del self.form post = Post.query.get_or_404(self.params['post_id']) if not self.current_user.can_action_on(post): return abort(401) post.update(**self._post_params()) return redirect(url_for('topics.show', topic_id=post.topic_id, forum_id=post.topic.forum_id)) else: return self.edit()
def create(self): self.topic_form = TopicForm() self.post_form = PostForm() is_topic_form_valid = self.topic_form.validate_on_submit() is_post_form_valid = self.post_form.validate_on_submit() if is_topic_form_valid and is_post_form_valid: del self.topic_form del self.post_form topic = Topic.create(user_id=self.current_user.id, **self._topic_params()) Post.create(user_id=self.current_user.id, topic_id=topic.id, **self._post_params()) return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id)) else: return self.new()
def update(self): self.topic_form = TopicForm() self.post_form = PostForm() is_topic_form_valid = self.topic_form.validate_on_submit() is_post_form_valid = self.post_form.validate_on_submit() if is_topic_form_valid and is_post_form_valid: del self.topic_form del self.post_form topic = Topic.query.get_or_404(self.params['topic_id']) if not self.current_user.can_action_on(topic): return abort(401) post = topic.posts.first_or_404() topic.update(**self._topic_params()) post.update(**self._post_params()) return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id)) else: return self.edit()
class TopicsController(BaseController): @render def show(self): self.topic = Topic.query.get_or_404(self.params['topic_id']) self.topic.views_count += 1 self.topic.save() self.in_topic_post_form = PostForm() @login_required @render def new(self): self.topic_form = getattr(self, 'topic_form', None) or TopicForm() self.post_form = getattr(self, 'post_form', None) or PostForm() @login_required @render def edit(self): self.topic = Topic.query.get_or_404(self.params['topic_id']) if not self.current_user.can_action_on(self.topic): return abort(401) self.topic_form = getattr(self, 'topic_form', None) or TopicForm(obj=self.topic) self.post_form = getattr(self, 'post_form', None) or PostForm(obj=self.topic.posts.first_or_404()) @login_required def create(self): self.topic_form = TopicForm() self.post_form = PostForm() is_topic_form_valid = self.topic_form.validate_on_submit() is_post_form_valid = self.post_form.validate_on_submit() if is_topic_form_valid and is_post_form_valid: del self.topic_form del self.post_form topic = Topic.create(user_id=self.current_user.id, **self._topic_params()) Post.create(user_id=self.current_user.id, topic_id=topic.id, **self._post_params()) return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id)) else: return self.new() @login_required def update(self): self.topic_form = TopicForm() self.post_form = PostForm() is_topic_form_valid = self.topic_form.validate_on_submit() is_post_form_valid = self.post_form.validate_on_submit() if is_topic_form_valid and is_post_form_valid: del self.topic_form del self.post_form topic = Topic.query.get_or_404(self.params['topic_id']) if not self.current_user.can_action_on(topic): return abort(401) post = topic.posts.first_or_404() topic.update(**self._topic_params()) post.update(**self._post_params()) return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id)) else: return self.edit() @admin_required def destroy(self): topic = Topic.query.get_or_404(self.params['topic_id']) forum_id = topic.forum_id topic.delete() return redirect(url_for('forums.show', forum_id=forum_id)) def _topic_params(self): return self.permit(self.params, 'title', 'forum_id') def _post_params(self): return self.permit(self.params, 'text')