示例#1
0
def writeblog():
    if not current_user.can(Permission.WRITE_ARTICLES):
        abort(403)
    form = PostForm()
    sources = [(s.id, s.name) for s in Source.query.all()]
    form.source.choices = sources
    menu = [(m.id, m.name) for m in Menu.query.all()]
    form.menu.choices = menu
    if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit(
    ):  # 检查用户是否有写文章的权限
        title = form.title.data
        source_id = form.source.data
        body = form.body.data
        menu_id = form.menu.data
        summary = form.summary.data
        author = current_user._get_current_object()
        source = Source.query.get(source_id)
        menu = Menu.query.get(menu_id)
        if source and menu:
            #将文章内容生成post实例
            post = Post(title=title,
                        source=source,
                        summary=summary,
                        author=author,
                        menu=menu,
                        body=body)
            db.session.add(post)
            db.session.commit()
            flash('Write A Blog Success!', 'success')
            return redirect(url_for('main.post', id=post.id))
    if form.errors:
        flash('Write A Blog Er!', 'danger')
    return render_template('main/writeblog.html', form=form)
示例#2
0
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author_id and not current_user.can(
            Permission.WRITE_ARTICLES):
        abort(403)
    form = PostForm()
    sources = [(s.id, s.name) for s in Source.query.all()]
    form.source.choices = sources
    menu = [(m.id, m.name) for m in Menu.query.all()]
    form.menu.choices = menu
    if form.validate_on_submit():
        post.title = form.title.data
        post.source_id = form.source.data
        post.body = form.body.data
        post.menu_id = form.menu.data
        post.summary = form.summary.data
        post.author = current_user._get_current_object()
        post.update_time = datetime.utcnow()
        source = Source.query.get(post.source_id)
        menu = Menu.query.get(post.menu_id)

        if source and menu:
            db.session.add(post)
            db.session.commit()
        flash('The post has been updated.')
        return redirect(url_for('main.post', id=post.id))
    form.title.data = post.title
    form.source.data = post.source_id
    form.body.data = post.body
    form.menu.data = post.menu_id
    form.summary.data = post.summary
    return render_template('main/edit_post.html', form=form, post=post)
示例#3
0
def post_edit(id):
    post = Post.query.get(id)

    #prefill forum with with post object
    form = PostForm(obj=post)

    if form.validate_on_submit():

        print(" form title text")
        print(form.title.data)

        #populate from form to object fields
        form.populate_obj(post)

        #updating the time
        post.timestamp=datetime.now(timezone.utc)

        #update author 
        post.author_p = current_user


        print("done")
        #save the object
        post.save()

        flash("Post has been updated successfully" , 'success')
        return redirect(url_for('post.posts'))
    
    return render_template('admin/post/edit.html' , form=form , post= post)
示例#4
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        form.save(set_author=True)
        flash('Added post.', 'success')

        return form.redirect(url_for('admin.index'))

    return render_template('admin/post_form.html', title='New post', form=form)
示例#5
0
def edit_post(id, slug=None):
    post = Post.query.get_or_404(id)
    if slug is None:
        return redirect(url_for('admin.edit_post', id=id, slug=post.slug))

    form = PostForm(obj=post)
    if form.validate_on_submit():
        form.save()
        flash('Edited post.', 'success')
        return form.redirect(url_for('admin.index'))

    return render_template('admin/post_form.html', title='Edit post', form=form)
示例#6
0
def edit_post(post_id):
    post = Post.query.get_or_404(post_id)
    if not (current_user.is_admin or
            int(post.user_id) == int(current_user.user_id)):
        abort(403)
    form = PostForm(form=request.form)
    post_tags = post.tags
    if request.method == 'POST' and form.validate():
        publish = False
        filename = None
        if form.publish.data:
            publish = True
        if 'cover_image' in request.files:
            cover_image = request.files['cover_image']
            image_name = cover_image.filename
            image_name_parts = image_name.split('.')
            image_name = append_timestamp(
                image_name_parts[0]) + '.' + image_name_parts[1]
            filename = uploads.save(cover_image, name=image_name)
        if post.title != form.title.data:
            post.post_title = form.title.data
        post.content = form.content.data
        post.published = publish
        if filename:
            # Remove previous image file, to save space
            if post.cover_image:
                upload_dir = current_app.config.get('UPLOAD_DIR', '')
                os.unlink(os.path.join(upload_dir, post.cover_image))
            post.cover_image = filename
        # remove previous tags
        remove_tags(post_id)
        # add new tags
        tags = form.tags.data
        for tag_id in tags:
            tag = check_tag(tag_id)
            if tag:
                post.tags.append(tag)
        db.session.add(post)
        db.session.commit()

        flash('Post edited successfully', 'success')
        return redirect(url_for('admin.list_posts'))

    form.title.data = post.title
    form.tags.data = [tag.tag_id for tag in post_tags]
    form.content.data = post.content
    form.publish.data = bool(post.published)
    data = {
        'title': 'Edit Post',
        'form': form,
        'post': post,
    }
    return render_template('admin/posts/edit.html', **data)
示例#7
0
def post_edit(slug):
    cprint('YELLOW', 'REQUEST METHOD: {}'.format(request.method))
    post = Post.query.filter(Post.slug == slug).first_or_404()
    if request.method == 'GET':
        return jsonify({'status': 'ok', 'title': post.title, 'body': post.body})
    if request.method == 'POST':

        try:
            form = PostForm(formdata=request.form, obj=post)
            form.populate_obj(post)
            db.session.commit()
        except Exception as ex:
            PrintException(err_pth)
            return jsonify({'status': '[PYTHON] error: {}'.format(ex)})

        return jsonify({'status': 'ok'})
示例#8
0
def post_new():

    form = PostForm()

    if form.validate_on_submit():
        #make a new post
        p= Post(form.title.data , form.body.data ,current_user.id )
        #save the post 
        p.save()
        
        #print post id 
        print(p.id)

        flash("post has been added", 'success')
        return redirect( url_for('post.posts'))

    return render_template("admin/post/new.html" , form = form)
示例#9
0
def post():
    if current_user.is_adminenticated:
        return redirect(url_for('admin.admin'))
    if not current_user.real_name:
        flash(_('完善资料后再试'))
        return redirect(url_for('admin.index'))
    if not current_user.is_authed:
        flash(_('用户信息审核中, 稍后再试'))
        return redirect(url_for('admin.index'))
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    body=form.body.data,
                    author=current_user)
        form.photo.data.filename = post.set_photo(form.photo.data.filename)
        filename = sphotos.save(form.photo.data)
        file_url = sphotos.url(filename)
        post.set_photo(file_url.split('/')[-1], token=True)
        db.session.add(post)
        db.session.commit()
        flash(_('课程申请已提交'))
        return redirect(url_for('admin.index'))
    return render_template('admin/post.html', title=_('申请课程'), form=form)
示例#10
0
def create_post():
    form = PostForm(form=request.form)
    if request.method == 'POST' and form.validate():
        publish = False
        filename = None
        if form.publish.data:
            publish = True
        if 'cover_image' in request.files:
            cover_image = request.files['cover_image']
            image_name = cover_image.filename
            image_name_parts = image_name.split('.')
            image_name = append_timestamp(
                image_name_parts[0]) + '.' + image_name_parts[1]
            filename = uploads.save(cover_image, name=image_name)
        post = Post()
        post.post_title = form.title.data
        post.content = form.content.data
        post.user_id = current_user.user_id
        post.published = publish
        if filename:
            post.cover_image = filename
        tags = form.tags.data
        for tag_id in tags:
            tag = check_tag(tag_id)
            if tag:
                post.tags.append(tag)
        db.session.add(post)
        db.session.commit()

        flash('Post created successfully', 'success')
        return redirect(url_for('admin.list_posts'))

    data = {
        'title': 'Create Post',
        'form': form,
    }
    return render_template('admin/posts/create.html', **data)
示例#11
0
def edit_course(title):
    if current_user.is_adminenticated:
        flash(_('这里不能审核课程!'))
        return redirect(url_for('admin.index'))
    post = Post.query.filter_by(title=title).first_or_404()
    if not post.author == current_user:
        # print(2333)
        flash(_('你没有权限这么做!'))
    form = PostForm(post.title, post.body)
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        form.photo.data.filename = post.set_photo(form.photo.data.filename)
        filename = sphotos.save(form.photo.data)
        file_url = sphotos.url(filename)
        post.set_photo(file_url.split('/')[-1], token=True)
        db.session.add(post)
        db.session.commit()
        flash(_('此课程信息已更新'))
        return redirect(url_for('admin.index'))
    elif request.method == 'GET':
        form.title.data = post.title
        form.body.data = post.body
    return render_template('admin/user/edit_course.html', form=form)
示例#12
0
def index():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']

        try:
            post = Post(title=title, body=body)
            db.session.add(post)
            db.session.commit()
        except Exception as ex:
            cprint("RED", "Error: Post insert tk db. ex: {}".format(ex))

        return redirect(url_for('admin.index'))

    form = PostForm()
    q = request.args.get('q')

    page = request.args.get('page')
    if page and page.isdigit():
        page = int(page)
    else:
        page = 1

    if q:
        posts = Post.query.filter(Post.title.contains(q) | Post.body.contains(q))  # .all()
    else:
        # posts = Post.query.all()
        posts = Post.query.order_by(Post.created.desc())

    pages = posts.paginate(page=page, per_page=7)
    
    # print(posts.paginate)
    # for i in dir(pages):
    #     cprint("GREEN", "{}".format(i))
    #
    # cprint("YELLOW", pages.per_page)
    # cprint("PURPLE", pages.total)
    # for post in pages.items:
    #     cprint("GREEN", "pages: {}".format(post.body))

    return render_template("admin/home.html", pgname="Home", company=Configuration.HTML_TITLE_COMPANY, url_prefix='/{}'.format(admin_panel.name), form=form, pages=pages)
示例#13
0
    def test_save(self):
        form_1 = PostForm(DummyPostData(title='foo', short_text='bar', tags='foo, bar, foo bar'))
        post_1 = form_1.save()
        tags_1 = Tag.query.all()

        self.assertListEqual(tags_1, post_1.tags.all())

        form_2 = PostForm(DummyPostData(title='foo', short_text='bar', tags='foo, bar, someothertag'))
        form_2.save()

        # Assert that only a single tag ('someothertag') has been added
        self.assertEqual(len(Tag.query.all()), 4)

        form_3 = PostForm(obj=post_1)
        form_3.tags.data = ['foo', 'bar', 'baz', 'duplicate', 'duplicate']
        form_3.save()

        tag_1 = Tag.query.filter_by(name='baz').first()
        tag_2 = Tag.query.filter_by(name='foo bar').first()

        self.assertIsNotNone(tag_1)
        self.assertNotIn(tag_2, post_1.tags.all())
        # Assert that the 'duplicate' tag has only been added once
        self.assertEqual(Tag.query.filter_by(name='duplicate').count(), 1)