Package: simple-flask-cms
Simple Flask CMSSimple flask CMS aims to be a very simple and flexible CMS system, suitable for a wide range of uses. The modular design
allows you to use or not use various parts depending on use.Right now, the plugin is still in development, however, it aims to support the following features:Currently, working featuresSimple page editor using SimpleMarkdownEditorAbility to upload and manage imagesAutomatically generate navigation menusSupport for SQLAuthenticationCurrently, WIP features:Headless modeSupport for MongoDBMore content types: Fragments and postsNow it uses SQL and mongoDB support was temporarily dropped, though it will be added again soon.InstallingInstall via pip:pipinstallsimple_flask_cmsAnd use in your application:importsimple_flask_cmsapp.register_blueprint(simple_flask_cms.cms)You can set some settings:importsimple_flask_cms.database_providers.sql_databasesimple_flask_cms.config.db_connection=simple_flask_cms.database_providers.sql_database.SQLDatabaseProvider(app)# The database to use# You can get a free mongoDB database from atlas, or use your own database. SQL support coming soon.simple_flask_cms.config.template_name='page.html'# The template to use for CMS pages, later section explains in more detail.simple_flask_cms.config.upload_folder='media/images'# Where to place uploaded imagessimple_flask_cms.config.authentication_function=simple_flask_cms.noop_authentication_function# Used to authenticate requestsSimple-Flask-CMS will attempt to connect to the database before the first request.Creating a page templateIf you want to use Simple-Flask-CMS for pages, you need to provide a page template. An example of a template would look
like this:<!DOCTYPE html><html><head><title>{{page.title}}</head><body><h1>{{page.title}}</h1><article>{{page.html|safe}}</article><nav>{%include'recursive_nav.html'%}</nav>The filerecursive_nav.html:<ul>{%forpageinnav%}<li><a href="{{page.get_absolute_url()}}">{{page.nav_title}}</a></li>{%ifpage.subpages%}{%withnav=page.subpages%}{%include'recursive_nav.html'%}{%endwith%}{%endif%}{%endfor%}</ul>In the aim for minimalism, Simple Flask CMS gives you full control over the styling of your pages. However, if you want
a preset look take a look at the example in the repository.Creating your first pageTo create a page, navigate to/cms/editor/[your new page path]. Enter your settings for the page here. The page will
be shown under/cms/[page path]. In the navigation/cms/bugs/ladybuywill appear under/cms/bug.TitleTypically displayed at the top of the page, though you can customize this in your template.Navigation titleTypically shown in the nav bar and in links to the page. Defaults to be the same as the title.Page contentThe page content is markdown, using thepython-markdownpackage. By default the code blocks
and highlight extension are enabled.Sort orderThe order that the page will be shown in under navigation. Defaults to the number of pages. 0 means auto.Adding AuthenticationBy default, anybody can create or delete pages. Typically, this is not what you want. Flask-Simple-CMS does not include
it's own authentication function, but rather you can supply your own.The typical way is to assign a function tocms.authentication_function. This function should returnNoneif the
request is authorized. If it returns anything else, the returned value will be sent as a response back to the client
instead of the action. An example of a authentication function using only basic auth is:defauthentication_function(action,parameters):ifactioninsimple_flask_cms.config.viewer_paths:returnNone# Anonymous users can view but not edit pagesauth=flask.request.authorizationifauthandauth.username==config["username"]andauth["password"]==config["password"]:returnNoneelse:returnflask.Response(status=401,headers={'WWW-Authenticate':'Basic realm="FlaskCMS"'})Then set it like:cms.authentication_function=authentication_functionIn this case,actionis the name of the endpoint, and parameters are the path parameters. For example, if you wanted
to hide a specific path from the viewer without authentication, replace the first if statement with this:ifactioninsimple_flask_cms.config.viewer_pathsandnotparameters["path"].startswith("secret"):returnNoneUse in headless modeTo use in headless mode, you can send a request from your frontend to/cms/<page>with the
headerContent-Type: Application/JSON. An example looks like this:{"page":{"path":"home","title":"Home","nav_title":"Home","sort_order":0,"date_modified":"2021-09-23T14:34:05.929918","subpages":[],"content":"Home\n\n![image.jpg](/cms/images/image.jpg)","html":"<p>Home</p>\n<p><img alt=\"image.jpg\" src=\"/cms/images/image.jpg\"></p>"},"nav":[{"path":"home","title":"Home","nav_title":"Home","sort_order":0,"date_modified":"2021-09-23T14:34:05.929918","subpages":[]}]}The response will contain both the raw markdown and the generated HTML by default. Usually, you want either one. You can
configure this behavior with either of these headers:X-IncludeMarkdown: false
X-IncludeHTML: falseFragmentsA fragment is a type of content intended for being embeded on another page. You would typically use a fragment if you
want a small part of a page to be editable via the CMS but also need some advanced interactions or layout on the page
that the CMS can't handle.To use fragments, assign a value tosimple_flask_cms.config.fragments. For example, if you want the home page to have
seperate boxes for the left and right column, you might do this:simple_flask_cms.config.fragments_map=[simple_flask_cms.dataclasses.FragmentType(name="Home Column 1",description="Displayed in the left column in the home page",url="/"),simple_flask_cms.dataclasses.FragmentType(name="Home Column 2",description="Displayed in the right column in the home page",url="/")]Here,nameis the unique identifier of the fragment. Must contain only url-safe characters.descriptionis displayed at the top of the fragment editor. You can describe where the fragment is used in more detail
here.Optionally, you can specify aurlwhich is where the "visit this page" button will lead to.You can then use it in your template like this:<div>{{cms_fragment('Home Column 1')|safe}}<a href="{{url_for('simple_flask_cms.fragment',name='Home Column 1')}}">Edit this fragment</a></div>You probably want to selectively show the editor button depending on a users permissions. or not show it at all.Custom context varsYou can add custom context variables for use in your page template by assigning to theconfig.extra_page_config_provider. This function takes the path as a paramter and should return a dict which will be
passed to the page template.Extra Custom Pages in navOften you might want to include other pages, not provided by the CMS visible in the navigation section too. You provide
a list of such pages inconfig.extra_nav_urls.nav_titleandsort_orderwork similar to normal pages, but
theredirectwill replace the destination of the link in nav.For example:simple_flask_cms.config.extra_nav_urls=[simple_flask_cms.dataclasses.ExtraNavUrl(nav_title="Color",path="/colors",redirect="/colors")]You can also render navigation on a custom page using thecms_navfunction, like this:{%withnav=cms_nav()%}{%forpageinnav%}<!-- Put content here -->{%endfor%}{%endwith%|