Example #1
0
def admin_index():
    if request.method == 'GET':
        user = request.args.get('user')

        if user == app.config['LEIHUNAG_ADMIN_KEY']:
            categories = Category.query.all()
            return render_template('admin/index.html', categories=categories)
        else:
            return render_template('404.html')
    else:
        post = Post()
        post.title = request.values.get("title")
        post.path_name = request.values.get("path")

        post.category_id = request.values.get("category")
        post.author = request.values.get("author")
        category = Category.query.filter_by(
            category_id=post.category_id).first()
        category.post_num += 1
        post.content = request.values.get("content")

        post.create_time = datetime.now()
        if exist_post(post.title):
            return '已经存在此博客'
        if exist_path_name(post.path_name):
            return '已经存在此路径的博客'

        db.session.add(post)
        db.session.flush()
        db.session.commit()
        return 'ok'
Example #2
0
def save_draft_api():
    code = int(request.form['code'])
    post_id = request.form['post_id']
    post_title = request.form['post_title']
    post_category = request.form['post_category']
    post_tag = request.form['post_tag']
    post_content = request.form['post_content']
    tags_id = post_tag.split(',')
    content_md = post_content
    content_html = md2html(content_md)
    print(session.get('draft_id'))
    if code == 0 and not session.get('draft_id'):
        post = Post(id=post_id,
                    title=post_title,
                    content_html=content_html,
                    content_md=content_md,
                    category_id=post_category,
                    date=datetime.now(),
                    draft_flag=True)
        if tags_id != ['']:
            for tag_id in tags_id:
                tag = Tag.query.filter_by(id=tag_id).first()
                post.tags.append(tag)
        db.session.add(post)
        db.session.commit()
        session['draft_id'] = post_id
    else:
        post = Post.query.filter_by(id=post_id).first()
        post.title = post_title
        post.category_id = post_category
        post.content_md = content_md
        post.content_html = content_html
        post.draft_flag = True
        if tags_id != ['']:
            for tag_id in tags_id:
                tag = Tag.query.filter_by(id=int(tag_id)).first()
                if not tag in post.tags:
                    post.tags.append(tag)
            for tag in post.tags:
                if not str(tag.id) in tags_id:
                    post.tags.remove(tag)
    result = {"status": 0}
    return jsonify(result)
Example #3
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)
Example #4
0
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)