Ticket #12875: ticket_12875_for_r12454_with_tests.diff

File ticket_12875_for_r12454_with_tests.diff, 4.3 KB (added by Manuel Saelices, 15 years ago)

Patch with tests

  • django/contrib/admin/options.py

     
    261261        )
    262262        return urlpatterns
    263263
     264    def get_ordering(self, request):
     265        return None
     266
    264267    def urls(self):
    265268        return self.get_urls()
    266269    urls = property(urls)
     
    325328        """
    326329        qs = self.model._default_manager.get_query_set()
    327330        # TODO: this should be handled by some parameter to the ChangeList.
    328         ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
     331        ordering = self.get_ordering(request) or self.ordering or () # otherwise we might try to *None, which is bad ;)
    329332        if ordering:
    330333            qs = qs.order_by(*ordering)
    331334        return qs
  • django/contrib/admin/views/main.py

     
    6363        if ERROR_FLAG in self.params:
    6464            del self.params[ERROR_FLAG]
    6565
    66         self.order_field, self.order_type = self.get_ordering()
     66        self.order_field, self.order_type = self.get_ordering(request)
    6767        self.query = request.GET.get(SEARCH_VAR, '')
    6868        self.query_set = self.get_query_set()
    6969        self.get_results(request)
     
    130130        self.multi_page = multi_page
    131131        self.paginator = paginator
    132132
    133     def get_ordering(self):
     133    def get_ordering(self, request):
    134134        lookup_opts, params = self.lookup_opts, self.params
    135         # For ordering, first check the "ordering" parameter in the admin
     135        # For ordering, first check if exists the "get_ordering" method
     136        # in model admin, then check the "ordering" parameter in admin
    136137        # options, then check the object's default ordering. If neither of
    137138        # those exist, order descending by ID by default. Finally, look for
    138139        # manually-specified ordering from the query string.
    139         ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
     140        ordering = self.model_admin.get_ordering(request) or self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
    140141
    141142        if ordering[0].startswith('-'):
    142143            order_field, order_type = ordering[0][1:], 'desc'
  • tests/regressiontests/admin_ordering/models.py

     
    4242>>> [b.name for b in ma.queryset(None)]
    4343[u'Radiohead', u'Van Halen', u'Aerosmith']
    4444
     45
     46Let's use a custom ModelAdmin that changes the ordering dinamically.
     47
     48>>> class DynOrderingBandAdmin(ModelAdmin):
     49...     def get_ordering(self, request):
     50...         if request.user.is_superuser:
     51...             return ('rank',)
     52...         else:
     53...             return ('name',)
     54
     55>>> from django.contrib.auth.models import User
     56>>> super_user = User.objects.create(username='admin', is_superuser=True)
     57>>> other_user = User.objects.create(username='other')
     58>>> class DummyRequest(object): pass
     59>>> request = DummyRequest()
     60
     61Ordering will be different for a super user and a normal one
     62
     63>>> request.user = super_user
     64>>> ma = DynOrderingBandAdmin(Band, None)
     65>>> [b.name for b in ma.queryset(request)]
     66[u'Radiohead', u'Van Halen', u'Aerosmith']
     67>>> request.user = other_user
     68>>> [b.name for b in ma.queryset(request)]
     69[u'Aerosmith', u'Radiohead', u'Van Halen']
     70
    4571"""
    4672}
  • tests/regressiontests/modeladmin/models.py

     
    88    name = models.CharField(max_length=100)
    99    bio = models.TextField()
    1010    sign_date = models.DateField()
    11    
     11
    1212    def __unicode__(self):
    1313        return self.name
    1414
     
    147147>>> from django import forms
    148148>>> class AdminBandForm(forms.ModelForm):
    149149...     delete = forms.BooleanField()
    150 ...     
     150...
    151151...     class Meta:
    152152...         model = Band
    153153
Back to Top