CMS Models¶
The CMS section consists of two base type models, that
define attributes, look and behaviour of their implementing
counterparts. The PageType is implemented by Page
and BlockType have their Block.
Additionally, the ReferencingPage is a special subclass
that only accepts base attributes as well as a referenced page
from which it will get all its attributes.
-
class
PageType(*args, **values)[source]¶ A page type defines the template being used for a single page.
Page types will have any number of content areas, which can later be filled by blocks (which in turn are defined by blocktypes).
A page type contains one or more areas that will be placed on the page according to its template.
-
class
BlockType(*args, **values)[source]¶ A single block inside a page type template.
A block type defines the expected content defined by the implementing block.
BlockTypes specify fields with a defined type that will be defined by the implementing Block.
ex: BlockType Product defines a sub-template that displays a product’s details, contains only one field to select a product, which will be chosen on the Block instance.
(This could also make for dynamic blocks, that fill the given blocktype’s fields from, say, a URL parameter for automatic browsing of models maybe).
-
identifier¶ The identifier of a blocktype is different from others, in that it is actually a Jinja2 template. You can use all common Jinja2 template tags and filters when defining your preview template.
This template currently receives the following context variables:
blockThe block we want to create a preview for.blocktypeThe BlockType object of this block.requestThe current HTTP request to the XMM system.
It is currently not possible to reference the current page the block is on.
Example usage:
Show a scalar value in the template:
{{ block.fields.my_decimal }} {{ block.fields.my_text }} {{ block.fields.my_translated.de }}
Render a reference or file attribute:
{% set file = block.get_attribute('myfile') %} File size: {{ file.view_size() }}<br /> File name: {{ file.filename }}<br /> Preview: <img src="//xmm.mysite.com/{{ url_for('main.thumbnail', object_id=file.id, size='100x100') }}" />
Rendering a reference attribute requires using the
get_attributemethod, otherwise the value would just be an ObjectId.Show an attribute with multiple values:
<ul> {% for film in block.fields.films %} <li><b>{{ film }}</b> (Runtime: {{ film.fields.length }}min) <ul> {% for category in film.fields.categories %} <li>{{ category }}</li> {% endfor %} </ul> </li> {% endfor %} </ul>
-
-
class
Page(*args, **values)[source]¶ A page that must implement a single page type (i.e. template).
Metadata such as SEO description and keywords will be stored on this model instance.
Associated with a page should be all blocks, and blocks in turn are defined by their block type, so we can define the way they will be rendered on the page.
-
api_move_block(source_path=None, target_path=None, pos=None, copy=False)[source]¶ Move a block around on this page.
Moving a block to another page will be supported at another time.
Parameters: Rules: Position numbering starts at 1. Blocks below the one being moved get their position adjusted -1.
If the new position exceeds the number of blocks currently in that area, it will be set to n+1 instead.
If the new target+position are already occupied, all blocks on and below the new position move one position down (+1)
-
build_urls(host=None, absolute=False)[source]¶ Build page url(s) without trailing slash.
If a slug is not defined in a different language, we will use the slug provided in the default language.
Parameters:
-
get_blocks(block_key, path=None)[source]¶ Get a specific block from this page.
Return type: xmm.models.cms.block.Block
-
classmethod
get_elastic_mapping()[source]¶ Return the ES mapping for the Page class, extended with field mappings for all blockstypes.
-
classmethod
pre_save(sender, document, **kwargs)[source]¶ Ensure proper positions of this page and the contained blocks.
-
classmethod
resolve_url(url_parts, lang=None, host=None, **kwargs)[source]¶ Resolve a CMS page URL.
Walks up the URL until it doesn’t find a match and throws a 404 or returns the desired page.
Parameters: - url_parts (list|str) – The url, or url parts to resolve.
- lang (str) – Language code to use if not default.
- host (str) – Domain or IP to look up pages on.
- parent_id (ObjectId) – Parent ID to look up in this iteration.
- version (int) – A specific version to lookup (not possible for drafts)
- find_deleted (bool) – Returns deleted pages as well.
-
-
class
ReferencingPage(*args, **values)[source]¶ A special kind of Page that will get all its type and fields from a referenced page.
Apart from the base attributes, only the
referenced_pagecan and must be set.
-
class
Block(*args, **kwargs)[source]¶ A block that implements a single blocktype’s expected contents.
Will show a field to be filled out for every defined property in the block’s type.
This can be anything from freetext, free HTML, a number or a number of models from a defined list.
Could also possibly be dynamically calculated by the current page’s parameters and status, such as URL and user login status.