From ca2a414ded67c85f76ea04a586013b528848d82d Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Mon, 6 Oct 2014 08:56:41 -0400 Subject: [PATCH] Added version specific overrides for get_queryset and get_query_set. Added new get_permissions_codename check for permissions in upcoming Django 1.8. Refactored all version checks. All checks are done in tuple format instead of checking for a django major and minor version number. Removed unused RequestFactory in tests. --- adminsortable/admin.py | 37 ++++++++++++------ .../database/python3-test-project.sqlite3 | Bin 74752 -> 74752 bytes python3_sample_project/manage.py | 0 sample_project/app/tests.py | 3 +- sample_project/database/test_project.sqlite | Bin 83968 -> 87040 bytes 5 files changed, 25 insertions(+), 15 deletions(-) mode change 100755 => 100644 python3_sample_project/manage.py diff --git a/adminsortable/admin.py b/adminsortable/admin.py index 5f3653d..6a4dc02 100644 --- a/adminsortable/admin.py +++ b/adminsortable/admin.py @@ -1,20 +1,22 @@ import json -from django import VERSION as DJANGO_VERSION +from django import VERSION from django.contrib.contenttypes.generic import (GenericStackedInline, GenericTabularInline) -DJANGO_MINOR_VERSION = DJANGO_VERSION[1] - from django.conf import settings -if DJANGO_MINOR_VERSION < 5: +if VERSION < (1, 5): from django.conf.urls.defaults import patterns, url else: from django.conf.urls import patterns, url from django.contrib.admin import ModelAdmin, TabularInline, StackedInline from django.contrib.admin.options import InlineModelAdmin + +if VERSION >= (1, 8): + from django.contrib.auth import get_permission_codename + from django.contrib.contenttypes.models import ContentType from django.http import HttpResponse from django.shortcuts import render @@ -90,8 +92,13 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): """ opts = self.model._meta - has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label, - opts.get_change_permission())) + if VERSION >= (1, 8): + codename = get_permission_codename('change', opts) + has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label, + codename)) + else: + has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label, + opts.get_change_permission())) # get sort group index from querystring if present sort_filter_index = request.GET.get('sort_filter') @@ -104,7 +111,8 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): pass # Apply any sort filters to create a subset of sortable objects - objects = self.queryset(request).filter(**filters) + qs_method = getattr(self, 'get_queryset', self.queryset) + objects = qs_method(request).filter(**filters) # Determine if we need to regroup objects relative to a # foreign key specified on the model class that is extending Sortable. @@ -266,18 +274,21 @@ class SortableInlineBase(SortableAdminBase, InlineModelAdmin): raise Warning(u'Models that are specified in SortableTabluarInline' ' and SortableStackedInline must inherit from Sortable') - def queryset(self, request): - qs = super(SortableInlineBase, self).queryset(request) + def get_queryset(self, request): + qs = super(SortableInlineBase, self).get_queryset(request) if get_is_sortable(qs): self.model.is_sortable = True else: self.model.is_sortable = False return qs + if VERSION < (1, 6): + queryset = get_queryset + class SortableTabularInline(TabularInline, SortableInlineBase): """Custom template that enables sorting for tabular inlines""" - if DJANGO_MINOR_VERSION <= 5: + if VERSION <= (1, 5): template = 'adminsortable/edit_inline/tabular-1.5.x.html' else: template = 'adminsortable/edit_inline/tabular.html' @@ -285,7 +296,7 @@ class SortableTabularInline(TabularInline, SortableInlineBase): class SortableStackedInline(StackedInline, SortableInlineBase): """Custom template that enables sorting for stacked inlines""" - if DJANGO_MINOR_VERSION <= 5: + if VERSION <= (1, 5): template = 'adminsortable/edit_inline/stacked-1.5.x.html' else: template = 'adminsortable/edit_inline/stacked.html' @@ -293,7 +304,7 @@ class SortableStackedInline(StackedInline, SortableInlineBase): class SortableGenericTabularInline(GenericTabularInline, SortableInlineBase): """Custom template that enables sorting for tabular inlines""" - if DJANGO_MINOR_VERSION <= 5: + if VERSION <= (1, 5): template = 'adminsortable/edit_inline/tabular-1.5.x.html' else: template = 'adminsortable/edit_inline/tabular.html' @@ -301,7 +312,7 @@ class SortableGenericTabularInline(GenericTabularInline, SortableInlineBase): class SortableGenericStackedInline(GenericStackedInline, SortableInlineBase): """Custom template that enables sorting for stacked inlines""" - if DJANGO_MINOR_VERSION <= 5: + if VERSION <= (1, 5): template = 'adminsortable/edit_inline/stacked-1.5.x.html' else: template = 'adminsortable/edit_inline/stacked.html' diff --git a/python3_sample_project/database/python3-test-project.sqlite3 b/python3_sample_project/database/python3-test-project.sqlite3 index 7435915f7d907899ebc93da37a490432bd89126d..c068fadedc593acd37ed61cc12f789d1c760ee74 100644 GIT binary patch delta 715 zcmah`O-~b16n$^*yn=zqM+C*slF$V)keO+x(`kaHv6WPvX=)0S>7ohBlwp`Dr4(dp zjQ9bPVEkYf{s6WnXl4OxO$;%Hg&RpQL=)ozT(Hp<3#Y|}tk}H!a_)KO+;iXTIz78i zzc_<)FV9{fIn?tL-VUom8_V=CVL!`7Lt>N{dwD?=*@*jD;BGVmZbH-t{M0K;PA47D z;i&ZJUd+_B>EOiVtspyPX``0TnPI*#H5}r{i+N^-*X4|^OG=u}_N%O_NOm?JV`MJL zOO*j$wj@?E5}ed8o2rs#QcBj8jX_g2ih{H8q@7AkI=|);_wN!SJs`1izA&y?6P4Sx zo)3-aMOG+RY_1e6%y5x#UKCX$$)x(ThFVG5YG#b_BrB>i5L6A%D5Khvp_{49pe`#( zTegyn%B4f8cu~lw`}#PB4fS~GIi{MPXQNEGmth4iB0Ln_XLDuh_*VpAA+TT-mSELw zX^cPsmQdiR1^66mG0@!w54Wr`>PQJN-?tf(R@i*o6i70`J}CH`8zxc3UYQ!Y4b9QP;In zX*4rro8zSx;$dwa_4Ku&G3@|C2%Yd~_4G}xP#8METIt%;dg83P7u;R9y{iRwJd+jv;0Nq^AwGe#w?aB70vOZ86Y|k?^-gLhfV;o-@nS0|qr)Rw*&nBxfFOJh)2}w` XFb_MPM)T%2)Ip6-ul{bp4X6GBJ3z~0 delta 318 zcmZoT!P0PoWr8%L_e2?IR&NH~XG|MYBF#k&EOm_x6^x9mj18;|E%eNcEX@tfHeWI~ z^5J4)>|tO!#@NGnX0swo9pmPn)-ooZNeoQPxeUy?%-fl_Zx-Zm6)9xl31COUnIl=6cU%@ zV3~Yovpk;=6PU-z>AcCS(3TMVVEp|me`3T|B>C1f&8~}ExAq=#}aHsiDX-{WV?>n zCSoB?d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