Ticket #8787: django-admin-widget-test.2.diff

File django-admin-widget-test.2.diff, 4.6 KB (added by Alex Gaynor, 16 years ago)

Refactored methods for simplicty

  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 50e55dc..4454d7d 100644
    a b class ForeignKeyRawIdWidget(forms.TextInput):  
    105105        super(ForeignKeyRawIdWidget, self).__init__(attrs)
    106106
    107107    def render(self, name, value, attrs=None):
    108         from django.contrib.admin.views.main import TO_FIELD_VAR
    109108        related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower())
    110         params = {}
    111         if self.rel.limit_choices_to:
    112             params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()]))
    113         params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
    114         url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
     109        params = self.url_parameters()
     110        if params:
     111            url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
     112        else:
     113            url = ''
    115114        if not attrs.has_key('class'):
    116115          attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
    117116        output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
    class ForeignKeyRawIdWidget(forms.TextInput):  
    123122        if value:
    124123            output.append(self.label_for_value(value))
    125124        return mark_safe(u''.join(output))
    126 
     125   
     126    def base_url_parameters(self):
     127        params = {}
     128        if self.rel.limit_choices_to:
     129            params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()]))
     130        return params           
     131   
     132    def url_parameters(self):
     133        from django.contrib.admin.views.main import TO_FIELD_VAR
     134        params = self.base_url_parameters()
     135        params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
     136        return params
     137           
    127138    def label_for_value(self, value):
    128139        key = self.rel.get_related_field().name
    129140        obj = self.rel.to.objects.get(**{key: value})
    class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):  
    144155        else:
    145156            value = ''
    146157        return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
    147 
     158   
     159    def url_parameters(self):
     160        return self.base_url_parameters()
     161   
    148162    def label_for_value(self, value):
    149163        return ''
    150164
  • tests/regressiontests/admin_widgets/models.py

    diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
    index 2bc9075..19e2696 100644
    a b Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">alb  
    6969>>> rel = Album._meta.get_field('band').rel
    7070>>> w = ForeignKeyRawIdWidget(rel)
    7171>>> print conditional_escape(w.render('test', band.pk, attrs={}))
    72 <input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
     72<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
    7373
    7474>>> m1 = Member.objects.create(pk=1, name='Chester')
    7575>>> m2 = Member.objects.create(pk=2, name='Mike')
    True  
    100100>>> rel = Inventory._meta.get_field('parent').rel
    101101>>> w = ForeignKeyRawIdWidget(rel)
    102102>>> print w.render('test', core.parent_id, attrs={})
    103 <input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="/admin_media/img/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
     103<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
    104104""" % {
    105105    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
    106106    'STORAGE_URL': default_storage.url(''),
Back to Top