| 1 | # -*- coding: utf-8 -*- |
| 2 | # Tests for the media handling on widgets and forms |
| 3 | |
| 4 | media_tests = r""" |
| 5 | >>> from django.newforms import TextInput, Media, TextInput, CharField, Form, MultiWidget |
| 6 | >>> from django.conf import settings |
| 7 | >>> settings.MEDIA_URL = 'http://media.example.com' |
| 8 | |
| 9 | # A widget can exist without a media definition |
| 10 | >>> class MyWidget(TextInput): |
| 11 | ... pass |
| 12 | |
| 13 | >>> w = MyWidget() |
| 14 | >>> print w.media |
| 15 | <BLANKLINE> |
| 16 | |
| 17 | # A widget can define media if it needs to. |
| 18 | # Any absolute path will be preserved; relative paths are combined |
| 19 | # with the value of settings.MEDIA_URL |
| 20 | >>> class MyWidget1(TextInput): |
| 21 | ... class Media: |
| 22 | ... css = { |
| 23 | ... 'all': ('/path/to/css1','/path/to/css2') |
| 24 | ... } |
| 25 | ... js = ('/path/to/js1','http://media.other.com/path/to/js2','https://secure.other.com/path/to/js3') |
| 26 | |
| 27 | >>> w1 = MyWidget1() |
| 28 | >>> print w1.media |
| 29 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 30 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 31 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 32 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 33 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 34 | |
| 35 | # Media objects can be interrogated by media type |
| 36 | >>> print w1.media['css'] |
| 37 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 38 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 39 | |
| 40 | >>> print w1.media['js'] |
| 41 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 42 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 43 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 44 | |
| 45 | # Media objects can be combined. Any given media resource will appear only |
| 46 | # once. Duplicated media definitions are ignored. |
| 47 | >>> class MyWidget2(TextInput): |
| 48 | ... class Media: |
| 49 | ... css = { |
| 50 | ... 'all': ('/path/to/css2','/path/to/css3') |
| 51 | ... } |
| 52 | ... js = ('/path/to/js1','/path/to/js4') |
| 53 | |
| 54 | >>> class MyWidget3(TextInput): |
| 55 | ... class Media: |
| 56 | ... css = { |
| 57 | ... 'all': ('/path/to/css3','/path/to/css1') |
| 58 | ... } |
| 59 | ... js = ('/path/to/js1','/path/to/js4') |
| 60 | |
| 61 | >>> w2 = MyWidget2() |
| 62 | >>> w3 = MyWidget3() |
| 63 | >>> print w1.media + w2.media + w3.media |
| 64 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 65 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 66 | <link href="http://media.example.com/path/to/css3" type="text/css" media="all" rel="stylesheet" /> |
| 67 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 68 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 69 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 70 | <script type="text/javascript" src="http://media.example.com/path/to/js4"></script> |
| 71 | |
| 72 | # Check that media addition hasn't affected the original objects |
| 73 | >>> print w1.media |
| 74 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 75 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 76 | <script type="text/javascript" src="http://media.example.com/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 | |
| 81 | # If a widget extends another, it inherits the parent widget's media |
| 82 | >>> class MyWidget4(MyWidget1): |
| 83 | ... pass |
| 84 | |
| 85 | >>> w4 = MyWidget4() |
| 86 | >>> print w4.media |
| 87 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 88 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 89 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 90 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 91 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 92 | |
| 93 | # If a widget extends another but defines media, it overrides the parent widget's media. |
| 94 | >>> class MyWidget5(MyWidget1): |
| 95 | ... class Media: |
| 96 | ... css = { |
| 97 | ... 'all': ('/path/to/css3','/path/to/css1') |
| 98 | ... } |
| 99 | ... js = ('/path/to/js1','/path/to/js4') |
| 100 | |
| 101 | >>> w5 = MyWidget5() |
| 102 | >>> print w5.media |
| 103 | <link href="http://media.example.com/path/to/css3" type="text/css" media="all" rel="stylesheet" /> |
| 104 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 105 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 106 | <script type="text/javascript" src="http://media.example.com/path/to/js4"></script> |
| 107 | |
| 108 | # You can ask a form for the media required by its widgets. |
| 109 | >>> class MyForm(Form): |
| 110 | ... field1 = CharField(max_length=20, widget=MyWidget1()) |
| 111 | ... field2 = CharField(max_length=20, widget=MyWidget2()) |
| 112 | >>> f1 = MyForm() |
| 113 | >>> print f1.media |
| 114 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 115 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 116 | <link href="http://media.example.com/path/to/css3" type="text/css" media="all" rel="stylesheet" /> |
| 117 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 118 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 119 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 120 | <script type="text/javascript" src="http://media.example.com/path/to/js4"></script> |
| 121 | |
| 122 | # Form media can be combined to produce a single media definition. |
| 123 | >>> class AnotherForm(Form): |
| 124 | ... field3 = CharField(max_length=20, widget=MyWidget3()) |
| 125 | >>> f2 = AnotherForm() |
| 126 | >>> print f1.media + f2.media |
| 127 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 128 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 129 | <link href="http://media.example.com/path/to/css3" type="text/css" media="all" rel="stylesheet" /> |
| 130 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 131 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 132 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 133 | <script type="text/javascript" src="http://media.example.com/path/to/js4"></script> |
| 134 | |
| 135 | # Form media can be defined as a property, too |
| 136 | >>> class MyWidget6(TextInput): |
| 137 | ... def _media(self): |
| 138 | ... return Media(css={'all': ('/some/path',)}) |
| 139 | ... media = property(_media) |
| 140 | |
| 141 | >>> w6 = MyWidget6() |
| 142 | >>> print w6.media |
| 143 | <link href="http://media.example.com/some/path" type="text/css" media="all" rel="stylesheet" /> |
| 144 | |
| 145 | # You can extend media, if required. |
| 146 | >>> class MyWidget7(MyWidget6): |
| 147 | ... def _media(self): |
| 148 | ... return super(MyWidget7, self).media + Media(css={'all': ('/other/path',)}) |
| 149 | ... media = property(_media) |
| 150 | |
| 151 | >>> w7 = MyWidget7() |
| 152 | >>> print w7.media |
| 153 | <link href="http://media.example.com/some/path" type="text/css" media="all" rel="stylesheet" /> |
| 154 | <link href="http://media.example.com/other/path" type="text/css" media="all" rel="stylesheet" /> |
| 155 | |
| 156 | # A widget can define CSS media for multiple output media types |
| 157 | >>> class MyWidget8(TextInput): |
| 158 | ... class Media: |
| 159 | ... css = { |
| 160 | ... 'screen, print': ('/file1','/file2'), |
| 161 | ... 'screen': ('/file3',), |
| 162 | ... 'print': ('/file4',) |
| 163 | ... } |
| 164 | ... js = ('/path/to/js1','/path/to/js4') |
| 165 | |
| 166 | >>> w8 = MyWidget8() |
| 167 | >>> print w8.media |
| 168 | <link href="http://media.example.com/file4" type="text/css" media="print" rel="stylesheet" /> |
| 169 | <link href="http://media.example.com/file3" type="text/css" media="screen" rel="stylesheet" /> |
| 170 | <link href="http://media.example.com/file1" type="text/css" media="screen, print" rel="stylesheet" /> |
| 171 | <link href="http://media.example.com/file2" type="text/css" media="screen, print" rel="stylesheet" /> |
| 172 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 173 | <script type="text/javascript" src="http://media.example.com/path/to/js4"></script> |
| 174 | |
| 175 | # MultiWidgets have a default media definition that gets all the |
| 176 | # media from the component widgets |
| 177 | >>> class MyWidget9(MultiWidget): |
| 178 | ... def __init__(self, attrs=None): |
| 179 | ... widgets = [MyWidget1, MyWidget2, MyWidget3] |
| 180 | ... MultiWidget.__init__(self, widgets, attrs) |
| 181 | |
| 182 | >>> w9 = MyWidget9() |
| 183 | >>> print w9.media |
| 184 | <link href="http://media.example.com/path/to/css1" type="text/css" media="all" rel="stylesheet" /> |
| 185 | <link href="http://media.example.com/path/to/css2" type="text/css" media="all" rel="stylesheet" /> |
| 186 | <link href="http://media.example.com/path/to/css3" type="text/css" media="all" rel="stylesheet" /> |
| 187 | <script type="text/javascript" src="http://media.example.com/path/to/js1"></script> |
| 188 | <script type="text/javascript" src="http://media.other.com/path/to/js2"></script> |
| 189 | <script type="text/javascript" src="https://secure.other.com/path/to/js3"></script> |
| 190 | <script type="text/javascript" src="http://media.example.com/path/to/js4"></script> |
| 191 | |
| 192 | """ |