示例#1
0
def create():
    if not check_permission(current_user, 'article'):
        flash('你没有权限')
        return redirect(url_for('Common.index'))
    form = CreateForm(request.form)
    form.category_id.choices = [(c.id, c.category_name) for c in Category.query.all()]
    if request.method == 'POST' and form.validate():
        title = form.title.data
        html_content = form.html_content.data
        raw_content = form.raw_content.data
        category_id = form.category_id.data
        brief = raw_content[:100]

        article = Article(current_user.id, title, brief, raw_content, html_content, category_id)

        f = request.files['cover']
        if f:
            try:
                article.cover = save_file(f)
            except Exception, e:
                pass

        article.save()

        return redirect(url_for('Common.index'))
示例#2
0
def update(article_id):
    article = Article.query.get(article_id)
    form = CreateForm(request.form)
    form.category_id.choices = [(c.id, c.category_name) for c in Category.query.all()]
    if form.validate():
        title = form.title.data
        html_content = form.html_content.data
        raw_content = form.raw_content.data
        category_id = form.category_id.data
        brief = raw_content[:100]
        category_id = form.category_id.data

        article.title = title
        article.brief = brief
        article.raw_content = raw_content
        article.html_content = html_content
        article.category_id = category_id

        f = request.files['cover']
        if f:
            try:
                article.cover = save_file(f)
            except Exception, e:
                pass

        article.update()

        return redirect(url_for('Article.retrieve', article_id=article.id))
示例#3
0
def carousel():
    if not check_permission(current_user, 'admin'):
        flash('你没有权限')
        return redirect(url_for('Common.index'))
    form = CarouselForm(request.form)
    if form.validate() and request.method == 'POST':
        order_num = form.order_num.data
        url = form.url.data
        title = form.title.data
        brief = form.brief.data
        cover = ''
        f = request.files['cover']
        if f:
            cover = save_file(f)
        c = Carousel(order_num, url, cover, title, brief)
        c.save()
    kwargs = dict(
        carousel_form=form,
        carousel_update_form=CarouselUpdateForm(),
        carousels=Carousel.query.filter_by(deleted=False).order_by(Carousel.order_num).all(),
    )
    return render_template('admin/carousel.html', **kwargs)
示例#4
0
def profile(user_id, page=1):
    user = User.query.get(user_id)
    if not user:
        return redirect(url_for('Common.index'))

    pagination = Article.query.filter_by(deleted=False, author=user).order_by(Article.id.desc())\
            .paginate(page, per_page=10, error_out=True)

    form = ProfileForm(request.form)
    if request.method == 'POST' and form.validate():
        profile = json.loads(user.verbose) if user.verbose else dict()

        profile['mail'] = form.mail.data
        profile['homepage'] = form.homepage.data
        profile['brief'] = form.brief.data

        user.verbose = json.dumps(profile)

        f = request.files['avatar']
        if f:
            try:
                filename = save_file(f)
                user.avatar = filename
            except:
                pass

        user.save()

    profile = json.loads(user.verbose) if user.verbose else None
    kwargs = dict(
        user=user,
        profile_form=form,
        profile=profile,
        pagination=pagination,
    )
    return render_template('user/profile.html', **kwargs)