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 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)