예제 #1
0
def draft(id):
    d = Draft.query.filter_by(id=id, author_id=current_user.id).first_or_404()
    form = RichTextEditorForm()
    form.publication.choices.extend([
        (pub.id, pub.name)
        for pub in current_user.followed_pubs_desc_by_time()
    ])

    if form.validate_on_submit():
        if form.reference_id.data:
            p = Post.query.filter_by(id=form.reference_id.data,
                                     author_id=current_user.id).first_or_404()
        else:
            p = Post()
            # Notice: using p.author = current_user._get_current_object() will causing instance(in here, "p") pending,
            # So, the following query will trigger auto-flush mechanism
            # (https://docs.sqlalchemy.org/en/13/orm/session_api.html#sqlalchemy.orm.session.Session.params.autoflush),
            #  but since p.title is empty and that field in the database has notNull constrain, this is unwanted.
            # unless using db.session.no_autoflush() context manager on top of following query.
            p.author_id = current_user.id

        tags = []
        if form.tags.data:
            for tag in form.tags.data.split():
                tag = Tag.get_or_create(tag.strip())
                if tag and tag not in p.tags:
                    tags.append(tag)

        publication = Publication.query.get(form.publication.data)
        p.update(title=form.title.data,
                 subtitle=form.subtitle.data,
                 description=form.description.data,
                 html=form.content.data,
                 is_public=not form.private.data,
                 publication=publication,
                 tags=tags)
        db.session.add(p)
        db.session.delete(d)
        db.session.commit()
        flash(_(u'Your post has been published.'))
        return redirect(url_for('post.article', id=p.id))
    # !!NOTE!! this is a wrong example.
    # form = RichTextEditorForm(id=id, reference_id=d.reference_id, type=d.type, title=d.title, subtitle=d.title,
    #                           description=d.description, content=d.content, tags=d.tags, private=not d.is_public)
    form.id.data = d.id
    form.reference_id.data = d.reference_id
    form.type.data = d.type
    form.title.data = d.title
    form.subtitle.data = d.subtitle
    form.description.data = d.description
    form.content.data = d.content
    form.publication.data = d.publication.id if d.publication else ''
    form.tags.data = d.tags
    form.private.data = not d.is_public
    return render_template('new.html', form=form)
예제 #2
0
def admin_add_post():
    form = PostForm()

    if request.method == 'POST' and form.validate_on_submit():
        post = Post(title=request.form['title'],
                    content=request.form['content'],
                    summary=request.form['summary'])

        post.author_id = ModelManager.current_user().id
        post.category_id = form.category.data

        post.before_save()

        db.session.add(post)
        db.session.commit()
        db.session.flush()
        flask.flash('Your post has been created', 'success')

    return flask.render_template(Theme.get_template(page='admin_add_post'),
                                 manager=ModelManager,
                                 post_form=form)
예제 #3
0
파일: views.py 프로젝트: lly365/flask-blog
def submission():
    form = PostForm()
    form.tag(placeholder='aaa')
    if form.validate_on_submit():
        save_to_db_objs = []
        post = Post()
        post.title = form.titile.data
        post.content = form.content.data
        post.category_id = form.category.data
        post.author_id = current_user.id
        if form.tag.data:
            tags = form.tag.data.split(',')
            for t in tags:
                tag_obj = Tag.query.filter_by(name=t).first()
                if tag_obj is None:
                    tag_obj = Tag(name=t)
                post.tags.append(tag_obj)
                save_to_db_objs.append(tag_obj)
        save_to_db_objs.append(post)
        db.session.add_all(save_to_db_objs)
        db.session.commit()
        return redirect(url_for('.post_index'))

    return render_template('frontend/submission.html', form=form)