Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.template.exceptions import TemplateDoesNotExist
4from superdjango.html.shortcuts import parse_template
6# Exports
8__all__ = (
9 "Asset",
10)
13# Classes
16class Asset(object):
17 """Base class for script and style collections."""
19 def __init__(self):
20 """All collections initialize identifiers and items."""
21 self._identifiers = list()
22 self._items = list()
24 def __iter__(self):
25 return iter(self._items)
27 def __repr__(self):
28 return "<%s %s>" % (self.__class__.__name__, len(self._items))
30 @property
31 def exists(self):
32 """Indicates whether one or more items have been added to the collection.
34 :rtype: bool
36 """
37 return len(self._items) > 0
39 def append(self, identifier, content=None, url=None, wrap=True):
40 """Append a URL or content to the collection.
42 :param identifier: A unique identifier for the included item. If this identifier already exists in the
43 collection, the item will be ignored.
44 :type identifier: str
46 :param content: The content to be included.
47 :type content: str
49 :param url: The URL to be included.
50 :type url: str
52 :param wrap: Indicates content should be wrapped in the appropriate HTML tag.
53 :type wrap: bool
55 :rtype: Script | Style
57 :raise: ValueError
59 .. note::
60 Child classes must implement this method, which includes:
62 - Checking for duplicate identifiers.
63 - Ensuring that either ``content`` or ``url`` is provided.
64 - Wrapping ``content`` as needed.
65 - Adding the ``identifier`` and ``content`` or ``url`` to the collection.
67 """
68 raise NotImplementedError()
70 def from_template(self, identifier, path, context=None, silent=True):
71 """Add media from a template.
73 :param identifier: A unique identifier for the content.
74 :type identifier: str
76 :param path: The path of the template file.
77 :type path: str
79 :param context: The context to use when parsing the template.
80 :type context: dict
82 :param silent: When ``False`` raise an exception rather than return ``False``.
83 :type silent: bool
85 :rtype: bool
87 :raise: TemplateDoesNotExist
89 .. note::
90 See notes on ``append()``.
92 """
93 try:
94 content = parse_template(path, context or dict())
95 except TemplateDoesNotExist as e:
96 if silent:
97 return False
99 raise ValueError("Failed to load media from template: %s" % e)
101 return self.append(identifier, content=content, wrap=True)
103 def merge(self, instance):
104 """Merge an existing collection.
106 :param instance: The instance to be merged.
107 :type: instance: BaseType[Asset]
109 .. tip::
110 This method checks for and ignores duplicate identifiers in the given instance. Additionally, ``instance``
111 may be ``None`` which allows easier runtime integration.
113 """
114 if instance is None:
115 return
117 for item in instance:
118 if item.identifier in self._identifiers:
119 continue
121 self._identifiers.append(item.identifier)
122 self._items.append(item)