8000 docs: Add documentation for some cms.utils.plugins functions (#7884) · django-cms/django-cms@564f184 · GitHub
[go: up one dir, main page]

Skip to content

Commit 564f184

Browse files
authored
docs: Add documentation for some cms.utils.plugins functions (#7884)
* Adds reference docs to some plugin utility functions. * add more docstrings * Update docs
1 parent 3fba0c8 commit 564f184

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

cms/utils/plugins.py

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ def get_plugin_model(plugin_type: str) -> CMSPlugin:
3030

3131

3232
def get_plugins(request, placeholder, template, lang=None):
33+
"""
34+
Get a list of plugins for a placeholder in a specified template. Respects the placeholder's cache.
35+
36+
:param request: (HttpRequest) The HTTP request object.
37+
:param placeholder: (Placeholder) The placeholder object for which to retrieve plugins.
38+
:param template: (Template) The template object in which the placeholder resides (not used).
39+
:param lang: (str, optional) The language code for localization. Defaults to None.
40+
41+
Returns:
42+
list: A list of plugins for the specified placeholder in the template.
43+
44+
Raises:
45+
None.
46+
47+
Examples::
48+
49+
# Get plugins for a placeholder in a template
50+
plugins = get_plugins(request, placeholder, template)
51+
52+
# Get plugins for a placeholder in a template with specific language
53+
plugins = get_plugins(request, placeholder, template, lang='en')
54+
"""
3355
if not placeholder:
3456
return []
3557
if not hasattr(placeholder, '_plugins_cache'):
@@ -42,6 +64,23 @@ def assign_plugins(request, placeholders, template=None, lang=None):
4264
Fetch all plugins for the given ``placeholders`` and
4365
cast them down to the concrete instances in one query
4466
per type.
67+
68+
:param request: The current request.
69+
:param placeholders: An iterable of placeholder objects.
70+
:param template: (optional) The template object.
71+
:param lang: (optional) The language code.
72+
73+
This method assigns plugins to the given placeholders. It retrieves the plugins from the database based on the
74+
placeholders and the language. The plugins are then downcasted to their specific plugin types.
75+
76+
The plugins are split up by placeholder and stored in a dictionary where the key is the placeholder ID and the
77+
value is a list of plugins.
78+
79+
For each placeholder, if there are plugins assigned to it, the plugins are organized as a layered tree structure.
80+
Otherwise, an empty list is assigned.
81+
82+
The list of all plugins for each placeholder is stored in the `_all_plugins_cache` attribute of the placeholder,
83+
while the list of root plugins is stored in the `_plugins_cache` attribute
4584
"""
4685
if not placeholders:
4786
return
@@ -164,7 +203,7 @@ def copy_plugins_to_placeholder(plugins, placeholder, language=None,
164203
#. Set the id/pk to None to it the id of the generic plugin instance above;
165204
this will effectively change the generic plugin created above
166205
into a concrete one
167-
#. find the position in the new plalceholder
206+
#. find the position in the new placeholder
168207
#. save the concrete plugin (which creates a new plugin in the database)
169208
#. trigger the copy relations
170209
#. return the plugin ids
@@ -251,6 +290,27 @@ def copy_plugins_to_placeholder(plugins, placeholder, language=None,
251290

252291

253292
def get_bound_plugins(plugins):
293+
"""
294+
Get the bound plugins by downcasting the plugins to their respective classes. Raises a KeyError if the plugin type
295+
is not available.
296+
297+
Creates a map of plugin types and their corresponding plugin IDs for later use in downcasting.
298+
Then, retrieves the plugin instances from the plugin model using the mapped plugin IDs.
299+
Finally, iterates over the plugins and yields the downcasted versions if they have a valid parent.
300+
Does not affect caching.
301+
302+
:param plugins: (list) List of ``CMSPlugin`` instances.
303+
304+
Yields:
305+
- instance (``CMSPlugin`` sub-class): Downcasted Plugin instance.
306+
307+
Example::
308+
309+
plugins = [plugin_instance1, plugin_instance2]
310+
for bound_plugin in get_bound_plugins(plugins):
311+
# Do something with the bound_plugin
312+
pass
313+
"""
254314
get_plugin = plugin_pool.get_plugin
255315
plugin_types_map = defaultdict(list)
256316
plugin_ids = []
@@ -281,6 +341,21 @@ def get_bound_plugins(plugins):
281341

282342
def downcast_plugins(plugins,
283343
placeholders=None, select_placeholder=False, request=None):
344+
"""
345+
Downcasts the given list of plugins to their respective classes. Ignores any plugins
346+
that are not available.
347+
348+
:param plugins: List of plugins to downcast.
349+
:type plugins: List[CMSPlugin]
350+
:param placeholders: List of placeholders associated with the plugins.
351+
:type placeholders: Optional[List[Placeholder]]
352+
:param select_placeholder: If True, select_related the plugin queryset with placeholder.
353+
:type select_placeholder: bool
354+
:param request: The current request.
355+
:type request: Optional[HttpRequest]
356+
:return: Generator that yields the downcasted plugins.
357+
:rtype: Generator[CMSPlugin, None, None]
358+
"""
284359
plugin_types_map = defaultdict(list)
285360
plugin_lookup = {}
286361
plugin_ids = []
@@ -333,8 +408,21 @@ def downcast_plugins(plugins,
333408

334409
def has_reached_plugin_limit(placeholder, plugin_type, language, template=None):
335410
"""
336-
Checks if placeholder has reached its global plugin limit,
337-
if not then it checks if it has reached its plugin_type limit.
411+
Checks if the global maximum limit for plugins in a placeholder has been reached.
412+
If not then it checks if it has reached its maximum plugin_type limit.
413+
414+
Parameters:
415+
- placeholder: The placeholder object to check the limit for.
416+
- plugin_type: The type of plugin to check the limit for.
417+
- language: The language code for the plugins.
418+
- template: The template object for the placeholder. Optional.
419+
420+
Returns:
421+
- False if the limit has not been reached.
422+
423+
Raises:
424+
- PluginLimitReached: If the limit has been reached for the placeholder.
425+
338426
"""
339427
limits = get_placeholder_conf("limits", placeholder.slot, template)
340428
if limits:

docs/reference/utility-functions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Plugins
4545

4646
.. module:: cms.utils.plugins
4747

48+
.. autofunction:: get_plugins
49+
4850
.. autofunction:: assign_plugins
4951

5052
.. autofunction:: has_reached_plugin_limit
@@ -57,3 +59,6 @@ Plugins
5759

5860
.. autofunction:: copy_plugins_to_placeholder
5961

62+
.. autofunction:: downcast_plugins
63+
64+
.. autofunction:: get_bound_plugins

0 commit comments

Comments
 (0)
0