|
| 1 | +===================== |
| 2 | +Contents and Regions |
| 3 | +===================== |
| 4 | + |
| 5 | +Regions |
| 6 | +======= |
| 7 | + |
| 8 | +The included ``Contents`` class and its helpers (``contents_*``) and |
| 9 | +the ``ContentEditor`` admin class expect a ``regions`` attribute or |
| 10 | +property (**not** a method) on their model which returns a list of ``Region`` instances. |
| 11 | + |
| 12 | +Regions have the following attributes: |
| 13 | + |
| 14 | +* ``title``: Something nice, will be visible in the content editor. |
| 15 | +* ``key``: The region key, used in the content proxy as attribute name |
| 16 | + for the list of plugins. Must contain a valid Python identifier. |
| 17 | + ``"regions"`` and names starting with an underscore cannot be used. The |
| 18 | + recommendation is to use ``"main"`` when you only have a single region and no |
| 19 | + better idea. |
| 20 | +* ``inherited``: Only has an effect if you are using the |
| 21 | + ``inherit_from`` argument to ``contents_for_item``: Model instances |
| 22 | + inherit content from their other instances if a region with |
| 23 | + ``inherited = True`` is empty. |
| 24 | + |
| 25 | +You are free to define additional attributes -- simply pass them |
| 26 | +when instantiating a new region. |
| 27 | + |
| 28 | +Example: |
| 29 | + |
| 30 | +.. code-block:: python |
| 31 | +
|
| 32 | + from content_editor.models import Region |
| 33 | +
|
| 34 | + class Article(models.Model): |
| 35 | + title = models.CharField(max_length=200) |
| 36 | +
|
| 37 | + regions = [ |
| 38 | + Region(key="main", title="Main content"), |
| 39 | + Region(key="sidebar", title="Sidebar", inherited=True), |
| 40 | + ] |
| 41 | +
|
| 42 | +Contents class and helpers |
| 43 | +========================== |
| 44 | + |
| 45 | +The ``content_editor.contents`` module offers a few helpers for |
| 46 | +fetching content blocks from the database. The ``Contents`` class |
| 47 | +knows how to group content blocks by region and how to merge |
| 48 | +contents from several main models. This is especially useful in |
| 49 | +inheritance scenarios, for example when a page in a hierarchical |
| 50 | +page tree inherits some aside-content from its ancestors. |
| 51 | + |
| 52 | +.. note:: |
| 53 | + |
| 54 | + **Historical note** |
| 55 | + |
| 56 | + The ``Contents`` class and the helpers replace the monolithic |
| 57 | + ``ContentProxy`` concept in FeinCMS_. |
| 58 | + |
| 59 | +Contents class |
| 60 | +-------------- |
| 61 | + |
| 62 | +Simple usage is as follows: |
| 63 | + |
| 64 | +.. code-block:: python |
| 65 | +
|
| 66 | + from content_editor.contents import Contents |
| 67 | +
|
| 68 | + article = Article.objects.get(...) |
| 69 | + c = Contents(article.regions) |
| 70 | + for item in article.app_richtext_set.all(): |
| 71 | + c.add(item) |
| 72 | + for item in article.app_download_set.all(): |
| 73 | + c.add(item) |
| 74 | +
|
| 75 | + # Returns a list of all items, sorted by the definition |
| 76 | + # order of article.regions and by item ordering |
| 77 | + list(c) |
| 78 | +
|
| 79 | + # Returns a list of all items from the given region |
| 80 | + c["main"] |
| 81 | + # or |
| 82 | + c.main |
| 83 | +
|
| 84 | + # How many items do I have? |
| 85 | + len(c) |
| 86 | +
|
| 87 | + # Inherit content from the given contents instance if one of my |
| 88 | + # own regions is empty and has its "inherited" flag set. |
| 89 | + c.inherit_regions(some_other_contents_instance) |
| 90 | +
|
| 91 | + # Plugins from unknown regions end up in _unknown_region_contents: |
| 92 | + c._unknown_region_contents |
| 93 | +
|
| 94 | +For most use cases you'll probably want to take a closer look at the |
| 95 | +following helper methods instead of instantiating a ``Contents`` class |
| 96 | +directly: |
| 97 | + |
| 98 | +contents_for_items |
| 99 | +------------------- |
| 100 | + |
| 101 | +Returns a contents instance for a list of main models: |
| 102 | + |
| 103 | +.. code-block:: python |
| 104 | +
|
| 105 | + articles = Article.objects.all()[:10] |
| 106 | + contents = contents_for_items( |
| 107 | + articles, |
| 108 | + plugins=[RichText, Download], |
| 109 | + ) |
| 110 | +
|
| 111 | + something = [ |
| 112 | + (article, contents[article]) |
| 113 | + for article in articles |
| 114 | + ] |
| 115 | +
|
| 116 | +contents_for_item |
| 117 | +------------------ |
| 118 | + |
| 119 | +Returns the contents instance for a given main model (note that this |
| 120 | +helper calls ``contents_for_items`` to do the real work): |
| 121 | + |
| 122 | +.. code-block:: python |
| 123 | +
|
| 124 | + # ... |
| 125 | + contents = contents_for_item( |
| 126 | + article, |
| 127 | + plugins=[RichText, Download], |
| 128 | + ) |
| 129 | +
|
| 130 | +It is also possible to add additional items for inheriting regions. |
| 131 | +This is most useful with a page tree where i.e. sidebar contents are |
| 132 | +inherited from ancestors (this example uses methods added by |
| 133 | +django-tree-queries_ as used in feincms3_): |
| 134 | + |
| 135 | +.. code-block:: python |
| 136 | +
|
| 137 | + page = ... |
| 138 | + contents = contents_for_item( |
| 139 | + page, |
| 140 | + plugins=[RichText, Download], |
| 141 | + page.ancestors().reverse(), # Prefer content closer to the |
| 142 | + # current page |
| 143 | + ) |
| 144 | +
|
| 145 | +.. _FeinCMS: https://github.com/feincms/feincms/ |
| 146 | +.. _django-tree-queries: https://github.com/matthiask/django-tree-queries/ |
| 147 | +.. _feincms3: https://feincms3.readthedocs.io/ |
0 commit comments