1
# -*- coding: utf-8 -*-
2
# Tests for the media handling on widgets and forms
5
>>> from django.forms import TextInput, Media, TextInput, CharField, Form, MultiWidget
6
>>> from django.conf import settings
7
>>> ORIGINAL_MEDIA_URL = settings.MEDIA_URL
8
>>> settings.MEDIA_URL = 'http://media.example.com/media/'
10
# Check construction of media objects
11
>>> m = Media(css={'all': ('path/to/css1','/path/to/css2')}, js=('/path/to/js1','http://media.other.com/path/to/js2','https://secure.other.com/path/to/js3'))
13
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
14
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
15
<script type="text/javascript" src="/path/to/js1"></script>
16
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
17
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
21
... 'all': ('path/to/css1','/path/to/css2')
23
... js = ('/path/to/js1','http://media.other.com/path/to/js2','https://secure.other.com/path/to/js3')
26
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
27
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
28
<script type="text/javascript" src="/path/to/js1"></script>
29
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
30
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
34
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
35
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
36
<script type="text/javascript" src="/path/to/js1"></script>
37
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
38
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
40
# A widget can exist without a media definition
41
>>> class MyWidget(TextInput):
48
###############################################################
49
# DSL Class-based media definitions
50
###############################################################
52
# A widget can define media if it needs to.
53
# Any absolute path will be preserved; relative paths are combined
54
# with the value of settings.MEDIA_URL
55
>>> class MyWidget1(TextInput):
58
... 'all': ('path/to/css1','/path/to/css2')
60
... js = ('/path/to/js1','http://media.other.com/path/to/js2','https://secure.other.com/path/to/js3')
64
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
65
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
66
<script type="text/javascript" src="/path/to/js1"></script>
67
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
68
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
70
# Media objects can be interrogated by media type
71
>>> print w1.media['css']
72
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
73
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
75
>>> print w1.media['js']
76
<script type="text/javascript" src="/path/to/js1"></script>
77
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
78
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
80
# Media objects can be combined. Any given media resource will appear only
81
# once. Duplicated media definitions are ignored.
82
>>> class MyWidget2(TextInput):
85
... 'all': ('/path/to/css2','/path/to/css3')
87
... js = ('/path/to/js1','/path/to/js4')
89
>>> class MyWidget3(TextInput):
92
... 'all': ('/path/to/css3','path/to/css1')
94
... js = ('/path/to/js1','/path/to/js4')
98
>>> print w1.media + w2.media + w3.media
99
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
100
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
101
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
102
<script type="text/javascript" src="/path/to/js1"></script>
103
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
104
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
105
<script type="text/javascript" src="/path/to/js4"></script>
107
# Check that media addition hasn't affected the original objects
109
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
110
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
111
<script type="text/javascript" src="/path/to/js1"></script>
112
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
113
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
115
###############################################################
116
# Property-based media definitions
117
###############################################################
119
# Widget media can be defined as a property
120
>>> class MyWidget4(TextInput):
121
... def _media(self):
122
... return Media(css={'all': ('/some/path',)}, js = ('/some/js',))
123
... media = property(_media)
127
<link href="/some/path" type="text/css" media="all" rel="stylesheet" />
128
<script type="text/javascript" src="/some/js"></script>
130
# Media properties can reference the media of their parents
131
>>> class MyWidget5(MyWidget4):
132
... def _media(self):
133
... return super(MyWidget5, self).media + Media(css={'all': ('/other/path',)}, js = ('/other/js',))
134
... media = property(_media)
138
<link href="/some/path" type="text/css" media="all" rel="stylesheet" />
139
<link href="/other/path" type="text/css" media="all" rel="stylesheet" />
140
<script type="text/javascript" src="/some/js"></script>
141
<script type="text/javascript" src="/other/js"></script>
143
# Media properties can reference the media of their parents,
144
# even if the parent media was defined using a class
145
>>> class MyWidget6(MyWidget1):
146
... def _media(self):
147
... return super(MyWidget6, self).media + Media(css={'all': ('/other/path',)}, js = ('/other/js',))
148
... media = property(_media)
152
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
153
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
154
<link href="/other/path" type="text/css" media="all" rel="stylesheet" />
155
<script type="text/javascript" src="/path/to/js1"></script>
156
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
157
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
158
<script type="text/javascript" src="/other/js"></script>
160
###############################################################
161
# Inheritance of media
162
###############################################################
164
# If a widget extends another but provides no media definition, it inherits the parent widget's media
165
>>> class MyWidget7(MyWidget1):
170
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
171
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
172
<script type="text/javascript" src="/path/to/js1"></script>
173
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
174
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
176
# If a widget extends another but defines media, it extends the parent widget's media by default
177
>>> class MyWidget8(MyWidget1):
180
... 'all': ('/path/to/css3','path/to/css1')
182
... js = ('/path/to/js1','/path/to/js4')
186
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
187
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
188
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
189
<script type="text/javascript" src="/path/to/js1"></script>
190
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
191
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
192
<script type="text/javascript" src="/path/to/js4"></script>
194
# If a widget extends another but defines media, it extends the parents widget's media,
195
# even if the parent defined media using a property.
196
>>> class MyWidget9(MyWidget4):
199
... 'all': ('/other/path',)
201
... js = ('/other/js',)
205
<link href="/some/path" type="text/css" media="all" rel="stylesheet" />
206
<link href="/other/path" type="text/css" media="all" rel="stylesheet" />
207
<script type="text/javascript" src="/some/js"></script>
208
<script type="text/javascript" src="/other/js"></script>
210
# A widget can disable media inheritance by specifying 'extend=False'
211
>>> class MyWidget10(MyWidget1):
215
... 'all': ('/path/to/css3','path/to/css1')
217
... js = ('/path/to/js1','/path/to/js4')
219
>>> w10 = MyWidget10()
221
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
222
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
223
<script type="text/javascript" src="/path/to/js1"></script>
224
<script type="text/javascript" src="/path/to/js4"></script>
226
# A widget can explicitly enable full media inheritance by specifying 'extend=True'
227
>>> class MyWidget11(MyWidget1):
231
... 'all': ('/path/to/css3','path/to/css1')
233
... js = ('/path/to/js1','/path/to/js4')
235
>>> w11 = MyWidget11()
237
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
238
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
239
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
240
<script type="text/javascript" src="/path/to/js1"></script>
241
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
242
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
243
<script type="text/javascript" src="/path/to/js4"></script>
245
# A widget can enable inheritance of one media type by specifying extend as a tuple
246
>>> class MyWidget12(MyWidget1):
248
... extend = ('css',)
250
... 'all': ('/path/to/css3','path/to/css1')
252
... js = ('/path/to/js1','/path/to/js4')
254
>>> w12 = MyWidget12()
256
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
257
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
258
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
259
<script type="text/javascript" src="/path/to/js1"></script>
260
<script type="text/javascript" src="/path/to/js4"></script>
262
###############################################################
263
# Multi-media handling for CSS
264
###############################################################
266
# A widget can define CSS media for multiple output media types
267
>>> class MultimediaWidget(TextInput):
270
... 'screen, print': ('/file1','/file2'),
271
... 'screen': ('/file3',),
272
... 'print': ('/file4',)
274
... js = ('/path/to/js1','/path/to/js4')
276
>>> multimedia = MultimediaWidget()
277
>>> print multimedia.media
278
<link href="/file4" type="text/css" media="print" rel="stylesheet" />
279
<link href="/file3" type="text/css" media="screen" rel="stylesheet" />
280
<link href="/file1" type="text/css" media="screen, print" rel="stylesheet" />
281
<link href="/file2" type="text/css" media="screen, print" rel="stylesheet" />
282
<script type="text/javascript" src="/path/to/js1"></script>
283
<script type="text/javascript" src="/path/to/js4"></script>
285
###############################################################
286
# Multiwidget media handling
287
###############################################################
289
# MultiWidgets have a default media definition that gets all the
290
# media from the component widgets
291
>>> class MyMultiWidget(MultiWidget):
292
... def __init__(self, attrs=None):
293
... widgets = [MyWidget1, MyWidget2, MyWidget3]
294
... super(MyMultiWidget, self).__init__(widgets, attrs)
296
>>> mymulti = MyMultiWidget()
297
>>> print mymulti.media
298
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
299
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
300
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
301
<script type="text/javascript" src="/path/to/js1"></script>
302
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
303
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
304
<script type="text/javascript" src="/path/to/js4"></script>
306
###############################################################
307
# Media processing for forms
308
###############################################################
310
# You can ask a form for the media required by its widgets.
311
>>> class MyForm(Form):
312
... field1 = CharField(max_length=20, widget=MyWidget1())
313
... field2 = CharField(max_length=20, widget=MyWidget2())
316
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
317
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
318
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
319
<script type="text/javascript" src="/path/to/js1"></script>
320
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
321
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
322
<script type="text/javascript" src="/path/to/js4"></script>
324
# Form media can be combined to produce a single media definition.
325
>>> class AnotherForm(Form):
326
... field3 = CharField(max_length=20, widget=MyWidget3())
327
>>> f2 = AnotherForm()
328
>>> print f1.media + f2.media
329
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
330
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
331
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
332
<script type="text/javascript" src="/path/to/js1"></script>
333
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
334
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
335
<script type="text/javascript" src="/path/to/js4"></script>
337
# Forms can also define media, following the same rules as widgets.
338
>>> class FormWithMedia(Form):
339
... field1 = CharField(max_length=20, widget=MyWidget1())
340
... field2 = CharField(max_length=20, widget=MyWidget2())
342
... js = ('/some/form/javascript',)
344
... 'all': ('/some/form/css',)
346
>>> f3 = FormWithMedia()
348
<link href="http://media.example.com/media/path/to/css1" type="text/css" media="all" rel="stylesheet" />
349
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet" />
350
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet" />
351
<link href="/some/form/css" type="text/css" media="all" rel="stylesheet" />
352
<script type="text/javascript" src="/path/to/js1"></script>
353
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
354
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
355
<script type="text/javascript" src="/path/to/js4"></script>
356
<script type="text/javascript" src="/some/form/javascript"></script>
358
>>> settings.MEDIA_URL = ORIGINAL_MEDIA_URL
b'\\ No newline at end of file'