| | 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 |
| | 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 = ('/path/to/css1','/path/to/css2') |
| | 23 | ... js = ('/path/to/js1','http://media.other.com/path/to/js2') |
| | 24 | |
| | 25 | >>> w1 = MyWidget1() |
| | 26 | >>> print w1.media |
| | 27 | <link href="http://media.example.com/path/to/css1" type="text/css" rel="stylesheet" /> |
| | 28 | <link href="http://media.example.com/path/to/css2" type="text/css" rel="stylesheet" /> |
| | 29 | <script type="text/javascript" src="http://media.example.com/path/to/js1" /> |
| | 30 | <script type="text/javascript" src="http://media.other.com/path/to/js2" /> |
| | 31 | |
| | 32 | # Media objects can be interrogated by media type |
| | 33 | >>> print w1.media['css'] |
| | 34 | <link href="http://media.example.com/path/to/css1" type="text/css" rel="stylesheet" /> |
| | 35 | <link href="http://media.example.com/path/to/css2" type="text/css" rel="stylesheet" /> |
| | 36 | |
| | 37 | >>> print w1.media['js'] |
| | 38 | <script type="text/javascript" src="http://media.example.com/path/to/js1" /> |
| | 39 | <script type="text/javascript" src="http://media.other.com/path/to/js2" /> |
| | 40 | |
| | 41 | # Media objects can be combined. Any given media resource will appear only |
| | 42 | # once. Duplicated media definitions are ignored. |
| | 43 | >>> class MyWidget2(TextInput): |
| | 44 | ... class Media: |
| | 45 | ... css = ('/path/to/css2','/path/to/css3') |
| | 46 | ... js = ('/path/to/js1','/path/to/js3') |
| | 47 | |
| | 48 | >>> class MyWidget3(TextInput): |
| | 49 | ... class Media: |
| | 50 | ... css = ('/path/to/css3','/path/to/css1') |
| | 51 | ... js = ('/path/to/js1','/path/to/js3') |
| | 52 | |
| | 53 | >>> w2 = MyWidget2() |
| | 54 | >>> w3 = MyWidget3() |
| | 55 | >>> print w1.media + w2.media + w3.media |
| | 56 | <link href="http://media.example.com/path/to/css1" type="text/css" rel="stylesheet" /> |
| | 57 | <link href="http://media.example.com/path/to/css2" type="text/css" rel="stylesheet" /> |
| | 58 | <link href="http://media.example.com/path/to/css3" type="text/css" rel="stylesheet" /> |
| | 59 | <script type="text/javascript" src="http://media.example.com/path/to/js1" /> |
| | 60 | <script type="text/javascript" src="http://media.other.com/path/to/js2" /> |
| | 61 | <script type="text/javascript" src="http://media.example.com/path/to/js3" /> |
| | 62 | |
| | 63 | # If a widget extends another, media must be redefined |
| | 64 | >>> class MyWidget4(MyWidget1): |
| | 65 | ... pass |
| | 66 | |
| | 67 | >>> w4 = MyWidget4() |
| | 68 | >>> print w4.media |
| | 69 | <BLANKLINE> |
| | 70 | |
| | 71 | # If a widget extends another, media from the parent widget is ignored |
| | 72 | >>> class MyWidget5(MyWidget1): |
| | 73 | ... class Media: |
| | 74 | ... css = ('/path/to/css3','/path/to/css1') |
| | 75 | ... js = ('/path/to/js1','/path/to/js3') |
| | 76 | |
| | 77 | >>> w5 = MyWidget5() |
| | 78 | >>> print w5.media |
| | 79 | <link href="http://media.example.com/path/to/css3" type="text/css" rel="stylesheet" /> |
| | 80 | <link href="http://media.example.com/path/to/css1" type="text/css" rel="stylesheet" /> |
| | 81 | <script type="text/javascript" src="http://media.example.com/path/to/js1" /> |
| | 82 | <script type="text/javascript" src="http://media.example.com/path/to/js3" /> |
| | 83 | |
| | 84 | # You can ask a form for the media required by its widgets. |
| | 85 | >>> class MyForm(Form): |
| | 86 | ... field1 = CharField(max_length=20, widget=MyWidget1()) |
| | 87 | ... field2 = CharField(max_length=20, widget=MyWidget2()) |
| | 88 | >>> f1 = MyForm() |
| | 89 | >>> print f1.media |
| | 90 | <link href="http://media.example.com/path/to/css1" type="text/css" rel="stylesheet" /> |
| | 91 | <link href="http://media.example.com/path/to/css2" type="text/css" rel="stylesheet" /> |
| | 92 | <link href="http://media.example.com/path/to/css3" type="text/css" rel="stylesheet" /> |
| | 93 | <script type="text/javascript" src="http://media.example.com/path/to/js1" /> |
| | 94 | <script type="text/javascript" src="http://media.other.com/path/to/js2" /> |
| | 95 | <script type="text/javascript" src="http://media.example.com/path/to/js3" /> |
| | 96 | |
| | 97 | # Form media can be combined to produce a single media definition. |
| | 98 | >>> class AnotherForm(Form): |
| | 99 | ... field3 = CharField(max_length=20, widget=MyWidget3()) |
| | 100 | >>> f2 = AnotherForm() |
| | 101 | >>> print f1.media + f2.media |
| | 102 | <link href="http://media.example.com/path/to/css1" type="text/css" rel="stylesheet" /> |
| | 103 | <link href="http://media.example.com/path/to/css2" type="text/css" rel="stylesheet" /> |
| | 104 | <link href="http://media.example.com/path/to/css3" type="text/css" rel="stylesheet" /> |
| | 105 | <script type="text/javascript" src="http://media.example.com/path/to/js1" /> |
| | 106 | <script type="text/javascript" src="http://media.other.com/path/to/js2" /> |
| | 107 | <script type="text/javascript" src="http://media.example.com/path/to/js3" /> |
| | 108 | |
| | 109 | # Form media can be defined as a property, too |
| | 110 | >>> class MyWidget6(TextInput): |
| | 111 | ... def _media(self): |
| | 112 | ... return Media(css=('/some/path',)) |
| | 113 | ... media = property(_media) |
| | 114 | |
| | 115 | >>> w6 = MyWidget6() |
| | 116 | >>> print w6.media |
| | 117 | <link href="http://media.example.com/some/path" type="text/css" rel="stylesheet" /> |
| | 118 | |
| | 119 | # This means that you can inherit media, if required. |
| | 120 | >>> class MyWidget7(MyWidget6): |
| | 121 | ... def _media(self): |
| | 122 | ... return super(MyWidget7, self).media + Media(css=('/other/path',)) |
| | 123 | ... media = property(_media) |
| | 124 | |
| | 125 | >>> w7 = MyWidget7() |
| | 126 | >>> print w7.media |
| | 127 | <link href="http://media.example.com/some/path" type="text/css" rel="stylesheet" /> |
| | 128 | <link href="http://media.example.com/other/path" type="text/css" rel="stylesheet" /> |
| | 129 | |
| | 130 | """ |
| | 131 | No newline at end of file |