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.
master
Brandon Taylor 2014-10-06 08:56:41 -04:00
parent b705c924f0
commit ca2a414ded
5 changed files with 25 additions and 15 deletions

View File

@ -1,20 +1,22 @@
import json import json
from django import VERSION as DJANGO_VERSION from django import VERSION
from django.contrib.contenttypes.generic import (GenericStackedInline, from django.contrib.contenttypes.generic import (GenericStackedInline,
GenericTabularInline) GenericTabularInline)
DJANGO_MINOR_VERSION = DJANGO_VERSION[1]
from django.conf import settings from django.conf import settings
if DJANGO_MINOR_VERSION < 5: if VERSION < (1, 5):
from django.conf.urls.defaults import patterns, url from django.conf.urls.defaults import patterns, url
else: else:
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from django.contrib.admin import ModelAdmin, TabularInline, StackedInline from django.contrib.admin import ModelAdmin, TabularInline, StackedInline
from django.contrib.admin.options import InlineModelAdmin 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.contrib.contenttypes.models import ContentType
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render from django.shortcuts import render
@ -90,6 +92,11 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
""" """
opts = self.model._meta opts = self.model._meta
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, has_perm = request.user.has_perm('{0}.{1}'.format(opts.app_label,
opts.get_change_permission())) opts.get_change_permission()))
@ -104,7 +111,8 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
pass pass
# Apply any sort filters to create a subset of sortable objects # 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 # Determine if we need to regroup objects relative to a
# foreign key specified on the model class that is extending Sortable. # 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' raise Warning(u'Models that are specified in SortableTabluarInline'
' and SortableStackedInline must inherit from Sortable') ' and SortableStackedInline must inherit from Sortable')
def queryset(self, request): def get_queryset(self, request):
qs = super(SortableInlineBase, self).queryset(request) qs = super(SortableInlineBase, self).get_queryset(request)
if get_is_sortable(qs): if get_is_sortable(qs):
self.model.is_sortable = True self.model.is_sortable = True
else: else:
self.model.is_sortable = False self.model.is_sortable = False
return qs return qs
if VERSION < (1, 6):
queryset = get_queryset
class SortableTabularInline(TabularInline, SortableInlineBase): class SortableTabularInline(TabularInline, SortableInlineBase):
"""Custom template that enables sorting for tabular inlines""" """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' template = 'adminsortable/edit_inline/tabular-1.5.x.html'
else: else:
template = 'adminsortable/edit_inline/tabular.html' template = 'adminsortable/edit_inline/tabular.html'
@ -285,7 +296,7 @@ class SortableTabularInline(TabularInline, SortableInlineBase):
class SortableStackedInline(StackedInline, SortableInlineBase): class SortableStackedInline(StackedInline, SortableInlineBase):
"""Custom template that enables sorting for stacked inlines""" """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' template = 'adminsortable/edit_inline/stacked-1.5.x.html'
else: else:
template = 'adminsortable/edit_inline/stacked.html' template = 'adminsortable/edit_inline/stacked.html'
@ -293,7 +304,7 @@ class SortableStackedInline(StackedInline, SortableInlineBase):
class SortableGenericTabularInline(GenericTabularInline, SortableInlineBase): class SortableGenericTabularInline(GenericTabularInline, SortableInlineBase):
"""Custom template that enables sorting for tabular inlines""" """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' template = 'adminsortable/edit_inline/tabular-1.5.x.html'
else: else:
template = 'adminsortable/edit_inline/tabular.html' template = 'adminsortable/edit_inline/tabular.html'
@ -301,7 +312,7 @@ class SortableGenericTabularInline(GenericTabularInline, SortableInlineBase):
class SortableGenericStackedInline(GenericStackedInline, SortableInlineBase): class SortableGenericStackedInline(GenericStackedInline, SortableInlineBase):
"""Custom template that enables sorting for stacked inlines""" """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' template = 'adminsortable/edit_inline/stacked-1.5.x.html'
else: else:
template = 'adminsortable/edit_inline/stacked.html' template = 'adminsortable/edit_inline/stacked.html'

0
python3_sample_project/manage.py 100755 → 100644
View File

View File

@ -4,7 +4,7 @@ import json
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.test import TestCase from django.test import TestCase
from django.test.client import Client, RequestFactory from django.test.client import Client
from adminsortable.fields import SortableForeignKey from adminsortable.fields import SortableForeignKey
from adminsortable.models import Sortable from adminsortable.models import Sortable
@ -29,7 +29,6 @@ class SortableTestCase(TestCase):
def setUp(self): def setUp(self):
self.client = Client() self.client = Client()
self.factory = RequestFactory()
self.user_raw_password = 'admin' self.user_raw_password = 'admin'
self.user = User.objects.create_user('admin', 'admin@admin.com', self.user = User.objects.create_user('admin', 'admin@admin.com',
self.user_raw_password) self.user_raw_password)