Example #1
0
def edit_comment(id):
    comment = Comment.query.get(id)
    if comment is None:
        abort(404)
    snippet = comment.snippet
    form = dict(title=comment.title, text=comment.text)
    if request.method == 'POST':
        if 'delete' in request.form:
            db_session.delete(comment)
            db_session.commit()
            flash(u'Comment was deleted.')
            return redirect(snippet.url)
        elif 'cancel' in request.form:
            return redirect(snippet.url)
        form['title'] = request.form['title']
        form['text'] = request.form['text']
        if not form['text']:
            flash(u'Error: comment text is required.')
        else:
            comment.title = form['title']
            comment.text = form['text']
            db_session.commit()
            flash(u'Comment was updated.')
            return redirect(snippet.url)
    return render_template('snippets/edit_comment.html', form=form,
                           comment=comment)
Example #2
0
def delete_category(id):
    category = Category.query.get(id)
    if category is None:
        abort(404)
    if request.method == 'POST':
        if 'cancel' in request.form:
            flash(u'Deletion was aborted')
            return redirect(url_for('.manage_categories'))
        move_to_id = request.form.get('move_to', type=int)
        if move_to_id:
            move_to = Category.query.get(move_to_id)
            if move_to is None:
                flash(u'Category was removed in the meantime')
            else:
                for snippet in category.snippets.all():
                    snippet.category = move_to
                db_session.delete(category)
                flash(u'Category %s deleted and entries moved to %s.' %
                      (category.name, move_to.name))
        else:
            category.snippets.delete()
            db_session.delete(category)
            flash(u'Category %s deleted' % category.name)
        db_session.commit()
        return redirect(url_for('.manage_categories'))
    return render_template('snippets/delete_category.html',
                           category=category,
                           other_categories=Category.query
                              .filter(Category.id != category.id).all())
Example #3
0
def edit_comment(id):
    comment = Comment.query.get(id)
    if comment is None:
        abort(404)
    snippet = comment.snippet
    form = dict(title=comment.title, text=comment.text)
    if request.method == 'POST':
        if 'delete' in request.form:
            db_session.delete(comment)
            db_session.commit()
            flash(u'Comment was deleted.')
            return redirect(snippet.url)
        elif 'cancel' in request.form:
            return redirect(snippet.url)
        form['title'] = request.form['title']
        form['text'] = request.form['text']
        if not form['text']:
            flash(u'Error: comment text is required.')
        else:
            comment.title = form['title']
            comment.text = form['text']
            db_session.commit()
            flash(u'Comment was updated.')
            return redirect(snippet.url)
    return render_template('snippets/edit_comment.html',
                           form=form,
                           comment=comment)
Example #4
0
def delete_category(id):
    category = Category.query.get(id)
    if category is None:
        abort(404)
    if request.method == 'POST':
        if 'cancel' in request.form:
            flash(u'Deletion was aborted')
            return redirect(url_for('.manage_categories'))
        move_to_id = request.form.get('move_to', type=int)
        if move_to_id:
            move_to = Category.query.get(move_to_id)
            if move_to is None:
                flash(u'Category was removed in the meantime')
            else:
                for snippet in category.snippets.all():
                    snippet.category = move_to
                db_session.delete(category)
                flash(u'Category %s deleted and entries moved to %s.' %
                      (category.name, move_to.name))
        else:
            category.snippets.delete()
            db_session.delete(category)
            flash(u'Category %s deleted' % category.name)
        db_session.commit()
        return redirect(url_for('.manage_categories'))
    return render_template('snippets/delete_category.html',
                           category=category,
                           other_categories=Category.query.filter(
                               Category.id != category.id).all())
Example #5
0
def edit(id):
    snippet = Snippet.query.get(id)
    if snippet is None:
        abort(404)
    if g.user is None or (not g.user.is_admin and snippet.author != g.user):
        abort(401)
    preview = None
    form = dict(title=snippet.title,
                body=snippet.body,
                category=snippet.category.id)
    if request.method == 'POST':
        form['title'] = request.form['title']
        form['body'] = request.form['body']
        form['category'] = request.form.get('category', type=int)
        if 'preview' in request.form:
            preview = format_creole(request.form['body'])
        elif 'delete' in request.form:
            for comment in snippet.comments:
                db_session.delete(comment)
            db_session.delete(snippet)
            db_session.commit()
            flash(u'Your snippet was deleted')
            return redirect(url_for('snippets.index'))
        else:
            category_id = request.form.get('category', type=int)
            if not form['body']:
                flash(u'Error: you have to enter a snippet')
            else:
                category = Category.query.get(category_id)
                if category is not None:
                    snippet.title = form['title']
                    snippet.body = form['body']
                    snippet.category = category
                    db_session.commit()
                    flash(u'Your snippet was modified')
                    return redirect(snippet.url)
    return render_template('snippets/edit.html',
                           snippet=snippet,
                           preview=preview,
                           form=form,
                           categories=Category.query.order_by(
                               Category.name).all())
Example #6
0
def edit(id):
    snippet = Snippet.query.get(id)
    if snippet is None:
        abort(404)
    if g.user is None or (not g.user.is_admin and snippet.author != g.user):
        abort(401)
    preview = None
    form = dict(title=snippet.title, body=snippet.body,
                category=snippet.category.id)
    if request.method == 'POST':
        form['title'] = request.form['title']
        form['body'] = request.form['body']
        form['category'] = request.form.get('category', type=int)
        if 'preview' in request.form:
            preview = format_creole(request.form['body'])
        elif 'delete' in request.form:
            for comment in snippet.comments:
                db_session.delete(comment)
            db_session.delete(snippet)
            db_session.commit()
            flash(u'Your snippet was deleted')
            return redirect(url_for('snippets.index'))
        else:
            category_id = request.form.get('category', type=int)
            if not form['body']:
                flash(u'Error: you have to enter a snippet')
            else:
                category = Category.query.get(category_id)
                if category is not None:
                    snippet.title = form['title']
                    snippet.body = form['body']
                    snippet.category = category
                    db_session.commit()
                    flash(u'Your snippet was modified')
                    return redirect(snippet.url)
    return render_template('snippets/edit.html',
        snippet=snippet, preview=preview, form=form,
        categories=Category.query.order_by(Category.name).all())