~ubuntu-branches/debian/squeeze/python-django/squeeze

« back to all changes in this revision

Viewing changes to tests/regressiontests/forms/media.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb, Chris Lamb, David Spreen, Sandro Tosi
  • Date: 2008-11-19 21:31:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081119213100-gp0lqhxl1qxa6dgl
Tags: 1.0.2-1
[ Chris Lamb ]
* New upstream bugfix release. Closes: #505783
* Add myself to Uploaders with ACK from Brett.

[ David Spreen ]
* Remove python-pysqlite2 from Recommends because Python 2.5 includes
  sqlite library used by Django. Closes: 497886

[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# Tests for the media handling on widgets and forms
3
 
 
4
 
media_tests = r"""
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/'
9
 
 
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'))
12
 
>>> print m
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>
18
 
 
19
 
>>> class Foo:
20
 
...     css = {
21
 
...        'all': ('path/to/css1','/path/to/css2')
22
 
...     }
23
 
...     js = ('/path/to/js1','http://media.other.com/path/to/js2','https://secure.other.com/path/to/js3')
24
 
>>> m3 = Media(Foo)
25
 
>>> print m3
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>
31
 
 
32
 
>>> m3 = Media(Foo)
33
 
>>> print m3
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>
39
 
 
40
 
# A widget can exist without a media definition
41
 
>>> class MyWidget(TextInput):
42
 
...     pass
43
 
 
44
 
>>> w = MyWidget()
45
 
>>> print w.media
46
 
<BLANKLINE>
47
 
 
48
 
###############################################################
49
 
# DSL Class-based media definitions
50
 
###############################################################
51
 
 
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):
56
 
...     class Media:
57
 
...         css = {
58
 
...            'all': ('path/to/css1','/path/to/css2')
59
 
...         }
60
 
...         js = ('/path/to/js1','http://media.other.com/path/to/js2','https://secure.other.com/path/to/js3')
61
 
 
62
 
>>> w1 = MyWidget1()
63
 
>>> print w1.media
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>
69
 
 
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" />
74
 
 
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>
79
 
 
80
 
# Media objects can be combined. Any given media resource will appear only
81
 
# once. Duplicated media definitions are ignored.
82
 
>>> class MyWidget2(TextInput):
83
 
...     class Media:
84
 
...         css = {
85
 
...            'all': ('/path/to/css2','/path/to/css3')
86
 
...         }
87
 
...         js = ('/path/to/js1','/path/to/js4')
88
 
 
89
 
>>> class MyWidget3(TextInput):
90
 
...     class Media:
91
 
...         css = {
92
 
...            'all': ('/path/to/css3','path/to/css1')
93
 
...         }
94
 
...         js = ('/path/to/js1','/path/to/js4')
95
 
 
96
 
>>> w2 = MyWidget2()
97
 
>>> w3 = MyWidget3()
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>
106
 
 
107
 
# Check that media addition hasn't affected the original objects
108
 
>>> print w1.media
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>
114
 
 
115
 
###############################################################
116
 
# Property-based media definitions
117
 
###############################################################
118
 
 
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)
124
 
 
125
 
>>> w4 = MyWidget4()
126
 
>>> print w4.media
127
 
<link href="/some/path" type="text/css" media="all" rel="stylesheet" />
128
 
<script type="text/javascript" src="/some/js"></script>
129
 
 
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)
135
 
 
136
 
>>> w5 = MyWidget5()
137
 
>>> print w5.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>
142
 
 
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)
149
 
 
150
 
>>> w6 = MyWidget6()
151
 
>>> print w6.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>
159
 
 
160
 
###############################################################
161
 
# Inheritance of media
162
 
###############################################################
163
 
 
164
 
# If a widget extends another but provides no media definition, it inherits the parent widget's media
165
 
>>> class MyWidget7(MyWidget1):
166
 
...     pass
167
 
 
168
 
>>> w7 = MyWidget7()
169
 
>>> print w7.media
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>
175
 
 
176
 
# If a widget extends another but defines media, it extends the parent widget's media by default
177
 
>>> class MyWidget8(MyWidget1):
178
 
...     class Media:
179
 
...         css = {
180
 
...            'all': ('/path/to/css3','path/to/css1')
181
 
...         }
182
 
...         js = ('/path/to/js1','/path/to/js4')
183
 
 
184
 
>>> w8 = MyWidget8()
185
 
>>> print w8.media
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>
193
 
 
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):
197
 
...     class Media:
198
 
...         css = {
199
 
...             'all': ('/other/path',)
200
 
...         }
201
 
...         js = ('/other/js',)
202
 
 
203
 
>>> w9 = MyWidget9()
204
 
>>> print w9.media
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>
209
 
 
210
 
# A widget can disable media inheritance by specifying 'extend=False'
211
 
>>> class MyWidget10(MyWidget1):
212
 
...     class Media:
213
 
...         extend = False
214
 
...         css = {
215
 
...            'all': ('/path/to/css3','path/to/css1')
216
 
...         }
217
 
...         js = ('/path/to/js1','/path/to/js4')
218
 
 
219
 
>>> w10 = MyWidget10()
220
 
>>> print w10.media
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>
225
 
 
226
 
# A widget can explicitly enable full media inheritance by specifying 'extend=True'
227
 
>>> class MyWidget11(MyWidget1):
228
 
...     class Media:
229
 
...         extend = True
230
 
...         css = {
231
 
...            'all': ('/path/to/css3','path/to/css1')
232
 
...         }
233
 
...         js = ('/path/to/js1','/path/to/js4')
234
 
 
235
 
>>> w11 = MyWidget11()
236
 
>>> print w11.media
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>
244
 
 
245
 
# A widget can enable inheritance of one media type by specifying extend as a tuple
246
 
>>> class MyWidget12(MyWidget1):
247
 
...     class Media:
248
 
...         extend = ('css',)
249
 
...         css = {
250
 
...            'all': ('/path/to/css3','path/to/css1')
251
 
...         }
252
 
...         js = ('/path/to/js1','/path/to/js4')
253
 
 
254
 
>>> w12 = MyWidget12()
255
 
>>> print w12.media
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>
261
 
 
262
 
###############################################################
263
 
# Multi-media handling for CSS
264
 
###############################################################
265
 
 
266
 
# A widget can define CSS media for multiple output media types
267
 
>>> class MultimediaWidget(TextInput):
268
 
...     class Media:
269
 
...         css = {
270
 
...            'screen, print': ('/file1','/file2'),
271
 
...            'screen': ('/file3',),
272
 
...            'print': ('/file4',)
273
 
...         }
274
 
...         js = ('/path/to/js1','/path/to/js4')
275
 
 
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>
284
 
 
285
 
###############################################################
286
 
# Multiwidget media handling
287
 
###############################################################
288
 
 
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)
295
 
            
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>
305
 
 
306
 
###############################################################
307
 
# Media processing for forms
308
 
###############################################################
309
 
 
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())
314
 
>>> f1 = MyForm()
315
 
>>> print f1.media
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>
323
 
 
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>
336
 
 
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())
341
 
...     class Media:
342
 
...         js = ('/some/form/javascript',)
343
 
...         css = {
344
 
...             'all': ('/some/form/css',)
345
 
...         }
346
 
>>> f3 = FormWithMedia()
347
 
>>> print f3.media
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>
357
 
 
358
 
>>> settings.MEDIA_URL = ORIGINAL_MEDIA_URL
359
 
"""
 
 
b'\\ No newline at end of file'