def category_add(): error_fields = [] if request.method == 'POST': name = request.form['name'].strip() if len(name) <= 0: error_fields.append('name') published = request.form.get('published', False) if published is not False: published = True if len(error_fields) <= 0: slug = slugify(name) # ensure slug is unique, add - until it is while Category.query.filter_by(slug=slug).count() > 0: slug += '-' db.session.add(Category(name, slug, published)) db.session.commit() cache.set('blog_categories', list_categories()) flash("Category added.") return redirect(url_for('admin.categories')) return render_template('admin/category_add.html', error_fields=error_fields)
def page_edit(page_id): page = Page.query.get_or_404(page_id) form = PageForm() if request.method == 'POST' and form.validate(): slug = form.slug.data if len(slug) <= 0: slug = slugify(form.title.data) # ensure slug is unique, add - until it is iff we are changing it if slug != page.slug: while Page.query.filter_by(slug=slug).count() > 0: slug += '-' page.slug = slug page.name = form.title.data page.menu = form.section.data page.published = form.published.data page.update_content(form.content.data) try: db.session.commit() except: db.session.rollback() raise cache.set('menus', get_menus()) revision = PageRevision(page_id=page.id, author_id=current_user.id, name=form.title.data, content=form.content.data) db.session.add(revision) try: db.session.commit() except: db.session.rollback() raise flash("Page Saved") return redirect(url_for('admin.pages')) elif request.method == 'DELETE': db.session.delete(page) db.session.commit() cache.set('menus', get_menus()) return jsonify({ '_csrf_token': app.jinja_env.globals['csrf_token'](), }) return render_template('admin/page_edit.html', sections=app.config['NAV_TOP_SECTIONS'], page=page, form=form)
def category_edit(cat_id): category = Category.query.get_or_404(cat_id) error_fields = [] if request.method == 'POST': name = request.form['name'].strip() if len(name) <= 0: error_fields.append('name') slug = request.form['slug'].strip() if len(slug) <= 0: # generate a new slug if none is provided slug = slugify(name) published = request.form.get('published', False) if published is not False: published = True if len(error_fields) <= 0: if slug != category.slug: # if slug has changed, ensure it is unique while Category.query.filter_by(slug=slug).count() > 0: slug += '-' category.name = name category.slug = slug category.published = published try: db.session.commit() except: db.session.rollback() raise cache.set('blog_categories', list_categories()) flash("Category saved.") return redirect(url_for('admin.categories')) elif request.method == 'DELETE': db.session.delete(category) try: db.session.commit() except: db.session.rollback() raise cache.set('blog_categories', list_categories()) return jsonify({ '_csrf_token': app.jinja_env.globals['csrf_token'](), }) return render_template('admin/category_edit.html', category=category, error_fields=error_fields)
def page_add(): error_fields = [] if request.method == 'POST': # Title title = request.form.get('title', "").strip() if len(title) <= 0: error_fields.append('title') # Slug slug = request.form.get('slug', "") if slug != "": slug = slugify(slug) if len(slug) <= 0 or slug is None: error_fields.append('slug') elif len(slug) <= 0 and len(title) > 0: slug = slugify(title) # Menu section = request.form['section'].strip() published = request.form.get('published', False) if published is not False: published = True content = request.form.get('content', "").strip() if len(error_fields) <= 0: # ensure slug is unique, add - until it is while Page.query.filter_by(slug=slug).count() > 0: slug += '-' page = Page(title, slug, content, published, section) db.session.add(page) db.session.commit() cache.set('menus', get_menus()) flash("Page Saved") return redirect(url_for('admin.pages')) sections = app.config['NAV_TOP_SECTIONS'] return render_template('admin/page_add.html', sections=sections, error_fields=error_fields)
def page_add(): form = PageForm() if form.is_submitted() and form.validate(): slug = form.slug.data if len(slug) <= 0: slug = slugify(form.title.data) # ensure slug is unique, add - until it is while Page.query.filter_by(slug=slug).count() > 0: slug += '-' page = Page(name=form.title.data, slug=slug, content=form.content.data, published=form.published.data, menu=form.section.data) db.session.add(page) try: db.session.commit() except: db.session.rollback() raise cache.set('menus', get_menus()) revision = PageRevision(page_id=page.id, author_id=current_user.id, name=form.title.data, content=form.content.data) db.session.add(revision) try: db.session.commit() except: db.session.rollback() raise flash("Page Saved") return redirect(url_for('admin.pages')) return render_template('admin/page_add.html', sections=app.config['NAV_TOP_SECTIONS'], form=form)
def page_edit(page_id): page = Page.query.get_or_404(page_id) error_fields = [] if request.method == 'POST': # Title title = request.form.get('title', "").strip() if len(title) <= 0: error_fields.append('title') # Slug slug = request.form.get('slug', "") if slug != "": slug = slugify(slug) if len(slug) <= 0 or slug is None: error_fields.append('slug') elif len(slug) <= 0 and len(title) > 0: slug = slugify(title) # Menu section = request.form['section'].strip() published = request.form.get('published', False) if published is not False: published = True content = request.form.get('content', "").strip() if len(error_fields) <= 0: # ensure slug is unique, add - until it is iff we are changing it if slug != page.slug: while Page.query.filter_by(slug=slug).count() > 0: slug += '-' page.slug = slug page.name = title page.menu = section page.published = published page.content = content page.update_content(content) # render HTML db.session.commit() cache.set('menus', get_menus()) flash("Page Saved") return redirect(url_for('admin.pages')) elif request.method == 'DELETE': db.session.delete(page) db.session.commit() cache.set('menus', get_menus()) return jsonify({ '_csrf_token': app.jinja_env.globals['csrf_token'](), }) sections = app.config['NAV_TOP_SECTIONS'] return render_template('admin/page_edit.html', sections=sections, page=page, error_fields=error_fields)