Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/templatetags/static.py: 32%

84 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from urllib.parse import quote, urljoin 

2 

3from django import template 

4from django.apps import apps 

5from django.utils.encoding import iri_to_uri 

6from django.utils.html import conditional_escape 

7 

8register = template.Library() 

9 

10 

11class PrefixNode(template.Node): 

12 def __repr__(self): 

13 return "<PrefixNode for %r>" % self.name 

14 

15 def __init__(self, varname=None, name=None): 

16 if name is None: 

17 raise template.TemplateSyntaxError( 

18 "Prefix nodes must be given a name to return." 

19 ) 

20 self.varname = varname 

21 self.name = name 

22 

23 @classmethod 

24 def handle_token(cls, parser, token, name): 

25 """ 

26 Class method to parse prefix node and return a Node. 

27 """ 

28 # token.split_contents() isn't useful here because tags using this 

29 # method don't accept variable as arguments. 

30 tokens = token.contents.split() 

31 if len(tokens) > 1 and tokens[1] != "as": 

32 raise template.TemplateSyntaxError( 

33 "First argument in '%s' must be 'as'" % tokens[0] 

34 ) 

35 if len(tokens) > 1: 

36 varname = tokens[2] 

37 else: 

38 varname = None 

39 return cls(varname, name) 

40 

41 @classmethod 

42 def handle_simple(cls, name): 

43 try: 

44 from django.conf import settings 

45 except ImportError: 

46 prefix = "" 

47 else: 

48 prefix = iri_to_uri(getattr(settings, name, "")) 

49 return prefix 

50 

51 def render(self, context): 

52 prefix = self.handle_simple(self.name) 

53 if self.varname is None: 

54 return prefix 

55 context[self.varname] = prefix 

56 return "" 

57 

58 

59@register.tag 

60def get_static_prefix(parser, token): 

61 """ 

62 Populate a template variable with the static prefix, 

63 ``settings.STATIC_URL``. 

64 

65 Usage:: 

66 

67 {% get_static_prefix [as varname] %} 

68 

69 Examples:: 

70 

71 {% get_static_prefix %} 

72 {% get_static_prefix as static_prefix %} 

73 """ 

74 return PrefixNode.handle_token(parser, token, "STATIC_URL") 

75 

76 

77@register.tag 

78def get_media_prefix(parser, token): 

79 """ 

80 Populate a template variable with the media prefix, 

81 ``settings.MEDIA_URL``. 

82 

83 Usage:: 

84 

85 {% get_media_prefix [as varname] %} 

86 

87 Examples:: 

88 

89 {% get_media_prefix %} 

90 {% get_media_prefix as media_prefix %} 

91 """ 

92 return PrefixNode.handle_token(parser, token, "MEDIA_URL") 

93 

94 

95class StaticNode(template.Node): 

96 child_nodelists = () 

97 

98 def __init__(self, varname=None, path=None): 

99 if path is None: 

100 raise template.TemplateSyntaxError( 

101 "Static template nodes must be given a path to return." 

102 ) 

103 self.path = path 

104 self.varname = varname 

105 

106 def __repr__(self): 

107 return ( 

108 f"{self.__class__.__name__}(varname={self.varname!r}, path={self.path!r})" 

109 ) 

110 

111 def url(self, context): 

112 path = self.path.resolve(context) 

113 return self.handle_simple(path) 

114 

115 def render(self, context): 

116 url = self.url(context) 

117 if context.autoescape: 

118 url = conditional_escape(url) 

119 if self.varname is None: 

120 return url 

121 context[self.varname] = url 

122 return "" 

123 

124 @classmethod 

125 def handle_simple(cls, path): 

126 if apps.is_installed("django.contrib.staticfiles"): 

127 from django.contrib.staticfiles.storage import staticfiles_storage 

128 

129 return staticfiles_storage.url(path) 

130 else: 

131 return urljoin(PrefixNode.handle_simple("STATIC_URL"), quote(path)) 

132 

133 @classmethod 

134 def handle_token(cls, parser, token): 

135 """ 

136 Class method to parse prefix node and return a Node. 

137 """ 

138 bits = token.split_contents() 

139 

140 if len(bits) < 2: 

141 raise template.TemplateSyntaxError( 

142 "'%s' takes at least one argument (path to file)" % bits[0] 

143 ) 

144 

145 path = parser.compile_filter(bits[1]) 

146 

147 if len(bits) >= 2 and bits[-2] == "as": 

148 varname = bits[3] 

149 else: 

150 varname = None 

151 

152 return cls(varname, path) 

153 

154 

155@register.tag("static") 

156def do_static(parser, token): 

157 """ 

158 Join the given path with the STATIC_URL setting. 

159 

160 Usage:: 

161 

162 {% static path [as varname] %} 

163 

164 Examples:: 

165 

166 {% static "myapp/css/base.css" %} 

167 {% static variable_with_path %} 

168 {% static "myapp/css/base.css" as admin_base_css %} 

169 {% static variable_with_path as varname %} 

170 """ 

171 return StaticNode.handle_token(parser, token) 

172 

173 

174def static(path): 

175 """ 

176 Given a relative path to a static asset, return the absolute path to the 

177 asset. 

178 """ 

179 return StaticNode.handle_simple(path)