Ticket #12875: ticket_12875_for_r16341.diff

File ticket_12875_for_r16341.diff, 5.0 KB (added by Manuel Saelices, 13 years ago)

A new patch with tests for the [16341] version

  • django/contrib/admin/options.py

     
    208208        """
    209209        qs = self.model._default_manager.get_query_set()
    210210        # TODO: this should be handled by some parameter to the ChangeList.
    211         ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
     211        ordering = self.get_ordering(request) or self.ordering or () # otherwise we might try to *None, which is bad ;)
    212212        if ordering:
    213213            qs = qs.order_by(*ordering)
    214214        return qs
     
    260260            clean_lookup = LOOKUP_SEP.join(parts)
    261261            return clean_lookup in self.list_filter or clean_lookup == self.date_hierarchy
    262262
     263    def get_ordering(self, request):
     264        return None
     265
     266
    263267class ModelAdmin(BaseModelAdmin):
    264268    "Encapsulates all admin options and functionality for a given model."
    265269
  • django/contrib/admin/views/main.py

     
    2727ERROR_FLAG = 'e'
    2828
    2929IGNORED_PARAMS = (
    30     ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR)
     30    ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR, )
    3131
    3232# Text to display within change-list table cells if the value is blank.
    3333EMPTY_CHANGELIST_VALUE = ugettext_lazy('(None)')
     
    7676            self.list_editable = ()
    7777        else:
    7878            self.list_editable = list_editable
    79         self.ordering = self.get_ordering()
     79        self.ordering = self.get_ordering(request)
    8080        self.query = request.GET.get(SEARCH_VAR, '')
    8181        self.query_set = self.get_query_set(request)
    8282        self.get_results(request)
     
    172172            ordering = self.lookup_opts.ordering
    173173        return ordering
    174174
    175     def get_ordering(self):
     175    def get_ordering(self, request):
    176176        params = self.params
    177         # For ordering, first check the "ordering" parameter in the admin
     177        # For ordering, first check the if exists the "get_ordering" method
     178        # in model admin, then check "ordering" parameter in the admin
    178179        # options, then check the object's default ordering. Finally, a
    179180        # manually-specified ordering from the query string overrides anything.
    180         ordering = self._get_default_ordering()
     181        ordering = self.model_admin.get_ordering(request) or self._get_default_ordering()
    181182
    182183        if ORDER_VAR in params:
    183184            # Clear ordering and used params
  • tests/regressiontests/admin_ordering/tests.py

     
    11from django.test import TestCase
     2from django.contrib.auth.models import User
    23from django.contrib.admin.options import ModelAdmin
    34
    4 from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering
     5from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering, DynOrderingBandAdmin
    56
    67class TestAdminOrdering(TestCase):
    78    """
     
    3839        names = [b.name for b in ma.queryset(None)]
    3940        self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names)
    4041
     42    def test_dynamic_ordering(self):
     43        """
     44        Let's use a custom ModelAdmin that changes the ordering dinamically.
     45        """
     46        super_user = User.objects.create(username='admin', is_superuser=True)
     47        other_user = User.objects.create(username='other')
     48        class DummyRequest(object): pass
     49        request = DummyRequest()
     50        request.user = super_user
     51        ma = DynOrderingBandAdmin(Band, None)
     52        names = [b.name for b in ma.queryset(request)]
     53        self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names)
     54        request.user = other_user
     55        names = [b.name for b in ma.queryset(request)]
     56        self.assertEqual([u'Aerosmith', u'Radiohead', u'Van Halen'], names)
     57
     58
    4159class TestInlineModelAdminOrdering(TestCase):
    4260    """
    4361    Let's make sure that InlineModelAdmin.queryset uses the ordering we define
  • tests/regressiontests/admin_ordering/models.py

     
    1818    class Meta:
    1919        ordering = ('name',)
    2020
     21class DynOrderingBandAdmin(admin.ModelAdmin):
     22
     23    def get_ordering(self, request):
     24        if request.user.is_superuser:
     25            return ('rank',)
     26        else:
     27            return ('name',)
     28
    2129class SongInlineDefaultOrdering(admin.StackedInline):
    2230    model = Song
    2331
    2432class SongInlineNewOrdering(admin.StackedInline):
    2533    model = Song
    2634    ordering = ('duration', )
     35
     36
Back to Top