def new_record(model_name):
from redberry.models import RedCategory, RedPost
from redberry.forms import CategoryForm, PostForm
if model_name == 'category':
form = CategoryForm()
new_record = RedCategory()
elif model_name == 'post':
form = PostForm()
new_record = RedPost()
# Convert category ids into objects for saving in the relationship.
if form.categories.data:
form.categories.data = RedCategory.query.filter(RedCategory.id.in_(form.categories.data)).all()
form.categories.choices = [(c, c.title) for c in RedCategory.sorted()]
else:
form.categories.choices = [(c.id, c.title) for c in RedCategory.sorted()]
if form.validate_on_submit():
form.populate_obj(new_record)
cms.config['db'].session.add(new_record)
cms.config['db'].session.flush()
build_sitemap()
flash("Saved %s %s" % (model_name, new_record.id), 'success')
return redirect(url_for('redberry.admin', model_name=model_name))
return render_template('redberry/admin/form.html', form=form, model_name=model_name)
评论列表
文章目录