예제 #1
0
파일: views.py 프로젝트: B-O-P/innovator
def edit_news(news_id):
    """
    Edit an existing piece of news. It also provides a chance to change news image, but if the Image File field is not filled, it just simply doesn't change the image.

    On GET method, it helps fill title, content and summary fields with original values, aiding the edit process.

    Upon handling a new image, it combines the current UNIX format time with the original filename (fileterd by :func:secure_filename) to form a filename used in local storage.

    .. note::
       You need to create static/news_upload manually!

    :param int news_id: Which news to modify.
    """
    form = NewsEditForm()
    delete_form = NewsDeleteForm()
    news_edited = News.get_by_id(news_id)

    if form.validate_on_submit():
        if form.image.has_file():
            filename = secure_filename(str(calendar.timegm(time.gmtime())) + form.image.data.filename)
            upload_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../static/news_upload/')
            form.image.data.save(upload_dir+filename)
            news_edited.update(title=form.title.data, summary=form.summary.data, content=form.content.data, image_path=url_for('static', filename='news_upload/'+filename))
        else:
            news_edited.update(title=form.title.data, summary=form.summary.data, content=form.content.data)
        flash("News edited. You're all set.", 'success')
        return redirect(url_for('admin.news_panel'))
    else:
        flash_errors(form)
    # populate fields when the page is fetched
    if request.method == 'GET':
        form.title.data = news_edited.title
        form.content.data = news_edited.content
        form.summary.data = news_edited.summary
    return render_template('admin/news_edit.html', delete_form=delete_form, news_id=news_id, edit_form=form)
예제 #2
0
파일: views.py 프로젝트: B-O-P/innovator
def compose_news():
    """
    Compose a new piece of news. It behaves almost the same as news edit page.
    """
    form = NewsComposeForm()
    if form.validate_on_submit():
        if form.image.has_file():
            filename = secure_filename(str(calendar.timegm(time.gmtime())) + form.image.data.filename)
            upload_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../static/news_upload/')
            form.image.data.save(upload_dir+filename)
            News.create(title=form.title.data, summary=form.summary.data, content=form.content.data, image_path=url_for('static', filename='news_upload/'+filename))
        else:
            News.create(title=form.title.data, summary=form.summary.data, content=form.content.data, image_path=url_for('static', filename='reisen.png'))
        flash("News posted. You're all set.", 'success')
        return redirect(url_for('admin.news_panel'))
    else:
        flash_errors(form)
    return render_template('admin/news_compose.html', compose_form=form)
예제 #3
0
파일: views.py 프로젝트: B-O-P/innovator
def delete_news(news_id):
    """
    Delete existing news. It only accepts POST method. User is redirected to news panel after successful deletion.

    :param int news_id: Which news to delete.
    """
    deleted_news = News.get_by_id(news_id)
    deleted_news.delete()
    flash("News deleted. You're all set.", 'success')
    return redirect(url_for('admin.news_panel'))