@@ -30,6 +30,28 @@ def get_plugin_model(plugin_type: str) -> CMSPlugin:
30
30
31
31
32
32
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
+ """
33
55
if not placeholder :
34
56
return []
35
57
if not hasattr (placeholder , '_plugins_cache' ):
@@ -42,6 +64,23 @@ def assign_plugins(request, placeholders, template=None, lang=None):
42
64
Fetch all plugins for the given ``placeholders`` and
43
65
cast them down to the concrete instances in one query
44
66
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
45
84
"""
46
85
if not placeholders :
47
86
return
@@ -164,7 +203,7 @@ def copy_plugins_to_placeholder(plugins, placeholder, language=None,
164
203
#. Set the id/pk to None to it the id of the generic plugin instance above;
165
204
this will effectively change the generic plugin created above
166
205
into a concrete one
167
- #. find the position in the new plalceholder
206
+ #. find the position in the new placeholder
168
207
#. save the concrete plugin (which creates a new plugin in the database)
169
208
#. trigger the copy relations
170
209
#. return the plugin ids
@@ -251,6 +290,27 @@ def copy_plugins_to_placeholder(plugins, placeholder, language=None,
251
290
252
291
253
292
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
+ """
254
314
get_plugin = plugin_pool .get_plugin
255
315
plugin_types_map = defaultdict (list )
256
316
plugin_ids = []
@@ -281,6 +341,21 @@ def get_bound_plugins(plugins):
281
341
282
342
def downcast_plugins (plugins ,
283
343
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
+ """
284
359
plugin_types_map = defaultdict (list )
285
360
plugin_lookup = {}
286
361
plugin_ids = []
@@ -333,8 +408,21 @@ def downcast_plugins(plugins,
333
408
334
409
def has_reached_plugin_limit (placeholder , plugin_type , language , template = None ):
335
410
"""
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
+
338
426
"""
339
427
limits = get_placeholder_conf ("limits" , placeholder .slot , template )
340
428
if limits :
0 commit comments