Hide keyboard shortcuts

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 

2 

3from django.template.exceptions import TemplateDoesNotExist 

4from superdjango.html.shortcuts import parse_template 

5 

6# Exports 

7 

8__all__ = ( 

9 "Asset", 

10) 

11 

12 

13# Classes 

14 

15 

16class Asset(object): 

17 """Base class for script and style collections.""" 

18 

19 def __init__(self): 

20 """All collections initialize identifiers and items.""" 

21 self._identifiers = list() 

22 self._items = list() 

23 

24 def __iter__(self): 

25 return iter(self._items) 

26 

27 def __repr__(self): 

28 return "<%s %s>" % (self.__class__.__name__, len(self._items)) 

29 

30 @property 

31 def exists(self): 

32 """Indicates whether one or more items have been added to the collection. 

33 

34 :rtype: bool 

35 

36 """ 

37 return len(self._items) > 0 

38 

39 def append(self, identifier, content=None, url=None, wrap=True): 

40 """Append a URL or content to the collection. 

41 

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 

45 

46 :param content: The content to be included. 

47 :type content: str 

48 

49 :param url: The URL to be included. 

50 :type url: str 

51 

52 :param wrap: Indicates content should be wrapped in the appropriate HTML tag. 

53 :type wrap: bool 

54 

55 :rtype: Script | Style 

56 

57 :raise: ValueError 

58 

59 .. note:: 

60 Child classes must implement this method, which includes: 

61 

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. 

66 

67 """ 

68 raise NotImplementedError() 

69 

70 def from_template(self, identifier, path, context=None, silent=True): 

71 """Add media from a template. 

72 

73 :param identifier: A unique identifier for the content. 

74 :type identifier: str 

75 

76 :param path: The path of the template file. 

77 :type path: str 

78 

79 :param context: The context to use when parsing the template. 

80 :type context: dict 

81 

82 :param silent: When ``False`` raise an exception rather than return ``False``. 

83 :type silent: bool 

84 

85 :rtype: bool 

86 

87 :raise: TemplateDoesNotExist 

88 

89 .. note:: 

90 See notes on ``append()``. 

91 

92 """ 

93 try: 

94 content = parse_template(path, context or dict()) 

95 except TemplateDoesNotExist as e: 

96 if silent: 

97 return False 

98 

99 raise ValueError("Failed to load media from template: %s" % e) 

100 

101 return self.append(identifier, content=content, wrap=True) 

102 

103 def merge(self, instance): 

104 """Merge an existing collection. 

105 

106 :param instance: The instance to be merged. 

107 :type: instance: BaseType[Asset] 

108 

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. 

112 

113 """ 

114 if instance is None: 

115 return 

116 

117 for item in instance: 

118 if item.identifier in self._identifiers: 

119 continue 

120 

121 self._identifiers.append(item.identifier) 

122 self._items.append(item)