Django: Ticket #21987: Allow Media objects to have their own MEDIA_TYPEShttps://code.djangoproject.com/ticket/21987
<p>
Currently, <a class="ext-link" href="https://github.com/django/django/blob/a2814846ca006b4fbf3a893ec52cd9d444b3a3eb/django/forms/widgets.py#L35"><span class="icon">​</span>instances of Media</a> look to the module scope to get the known <a class="ext-link" href="https://github.com/django/django/blob/a2814846ca006b4fbf3a893ec52cd9d444b3a3eb/django/forms/widgets.py#L31"><span class="icon">​</span>MEDIA_TYPES</a>. This makes it difficult to subclass <tt>Media</tt> to add extra <tt>render_x</tt>/<tt>add_x</tt> methods because <tt>MEDIA_TYPES</tt> is hardcoded in <a class="ext-link" href="https://github.com/django/django/blob/a2814846ca006b4fbf3a893ec52cd9d444b3a3eb/django/forms/widgets.py#L45"><span class="icon">​</span>__init__</a>, <a class="ext-link" href="https://github.com/django/django/blob/a2814846ca006b4fbf3a893ec52cd9d444b3a3eb/django/forms/widgets.py#L56"><span class="icon">​</span>render</a>, <a class="ext-link" href="https://github.com/django/django/blob/a2814846ca006b4fbf3a893ec52cd9d444b3a3eb/django/forms/widgets.py#L83"><span class="icon">​</span>__getitem__</a> and <a class="ext-link" href="https://github.com/django/django/blob/a2814846ca006b4fbf3a893ec52cd9d444b3a3eb/django/forms/widgets.py#L102"><span class="icon">​</span>__add__</a> and isn't easily available for a subclass to add to without monkeypatching the module constant. As far as I can tell, the only use of <tt>MEDIA_TYPES</tt> is in the widgets module, and could be shifted to the class itself, leaving the module level one to raise a deprecation warning.
</p>
<p>
Rationale is that I want to be able to add <tt>inline_css</tt> and <tt>inline_js</tt>, or <tt>text</tt> (for JS templates etc) to a <tt>Media</tt> object, and currently that's more difficult than just adjusting a subclass' attribute and adding the appropriate methods.
</p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/21987
Trac 1.0.2aaugustinThu, 20 Mar 2014 21:39:27 GMTstage changed; needs_docs, needs_better_patch, needs_tests sethttps://code.djangoproject.com/ticket/21987#comment:1
https://code.djangoproject.com/ticket/21987#comment:1
<ul>
<li><strong>needs_docs</strong>
unset
</li>
<li><strong>needs_better_patch</strong>
unset
</li>
<li><strong>needs_tests</strong>
unset
</li>
<li><strong>stage</strong>
changed from <em>Unreviewed</em> to <em>Accepted</em>
</li>
</ul>
TicketaaugustinThu, 20 Mar 2014 21:40:20 GMThttps://code.djangoproject.com/ticket/21987#comment:2
https://code.djangoproject.com/ticket/21987#comment:2
<p>
See also <a class="assigned ticket" href="https://code.djangoproject.com/ticket/13978" title="New feature: Allow inline js/css in forms.Media (assigned)">#13978</a>.
</p>
TicketOsmoseTue, 15 Apr 2014 21:09:52 GMTstatus, owner, has_patch changedhttps://code.djangoproject.com/ticket/21987#comment:3
https://code.djangoproject.com/ticket/21987#comment:3
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>assigned</em>
</li>
<li><strong>owner</strong>
changed from <em>nobody</em> to <em>Osmose</em>
</li>
<li><strong>has_patch</strong>
set
</li>
</ul>
<p>
Pull request submitted: <a class="ext-link" href="https://github.com/django/django/pull/2562"><span class="icon">​</span>https://github.com/django/django/pull/2562</a>
</p>
TickethirokikySat, 03 May 2014 10:21:57 GMTneeds_better_patch changed; cc sethttps://code.djangoproject.com/ticket/21987#comment:4
https://code.djangoproject.com/ticket/21987#comment:4
<ul>
<li><strong>cc</strong>
<em>hirokiky@…</em> added
</li>
<li><strong>needs_better_patch</strong>
set
</li>
</ul>
<p>
this patch may need improvement.
you should consider the case when combining CustomMedias which has different MEDIA_TYPES,
see more <a class="ext-link" href="https://github.com/django/django/pull/2562#issuecomment-42101462"><span class="icon">​</span>https://github.com/django/django/pull/2562#issuecomment-42101462</a>
</p>
TicketOsmoseSat, 03 May 2014 18:50:07 GMThttps://code.djangoproject.com/ticket/21987#comment:5
https://code.djangoproject.com/ticket/21987#comment:5
<p>
Good point! Either throwing an exception on combining Media subclasses with different Media types, or throwing one when there's a name conflict in the MEDIA_TYPES list sound like good options to me; I'd probably lean towards the latter simply because it's more flexible and not really that complex to add.
</p>
<p>
However, I'm hesitant to make changes to the patch before <a class="new ticket" href="https://code.djangoproject.com/ticket/22298" title="Cleanup/optimization: Rename Media to Static (new)">#22298</a> is resolved, as if form media is going away, improvements aren't terribly relevant anymore. (I submitted the patch before someone pointed that issue out to me, of course :P).
</p>
TickethirokikySun, 04 May 2014 15:13:28 GMThttps://code.djangoproject.com/ticket/21987#comment:6
https://code.djangoproject.com/ticket/21987#comment:6
<p>
@Osmose thanks for your reply. the after one sounds greater! &gt; throwing one when there's a name conflict in the MEDIA_TYPES list
</p>
<p>
<a class="new ticket" href="https://code.djangoproject.com/ticket/22298" title="Cleanup/optimization: Rename Media to Static (new)">#22298</a> discussion looks hassle a little (´Д｀), the patch will probably have to wait for a while.
But you've done a great job. im pretty sure.
</p>
Ticket