Added Python 3 compatibility to sample project.
Removed utils file and moved map_path function to settings.py. Refactored tests for Python 2 and 3 compatibility. Added inheritance check to get proper determination if a SortableForeignKey field is defined but the specified model does not inherit from Sortable.
This commit is contained in:
@@ -71,13 +71,13 @@ class ProjectAdmin(SortableAdmin):
|
||||
CreditInline, NoteInline, GenericNoteInline,
|
||||
NonSortableCreditInline, NonSortableNoteInline
|
||||
]
|
||||
list_display = ['__unicode__', 'category']
|
||||
list_display = ['__str__', 'category']
|
||||
|
||||
admin.site.register(Project, ProjectAdmin)
|
||||
|
||||
|
||||
class PersonAdmin(SortableAdmin):
|
||||
list_display = ['__unicode__', 'is_board_member']
|
||||
list_display = ['__str__', 'is_board_member']
|
||||
|
||||
admin.site.register(Person, PersonAdmin)
|
||||
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
from adminsortable.fields import SortableForeignKey
|
||||
from adminsortable.models import Sortable
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class SimpleModel(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
title = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
@@ -28,11 +30,12 @@ class Category(SimpleModel, Sortable):
|
||||
|
||||
|
||||
# A model with an override of its queryset for admin
|
||||
@python_2_unicode_compatible
|
||||
class Widget(SimpleModel, Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
pass
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
@@ -47,6 +50,7 @@ class Project(SimpleModel, Sortable):
|
||||
|
||||
|
||||
# Registered as a tabular inline on `Project`
|
||||
@python_2_unicode_compatible
|
||||
class Credit(Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
pass
|
||||
@@ -55,11 +59,12 @@ class Credit(Sortable):
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '{0} {1}'.format(self.first_name, self.last_name)
|
||||
|
||||
|
||||
# Registered as a stacked inline on `Project`
|
||||
@python_2_unicode_compatible
|
||||
class Note(Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
pass
|
||||
@@ -67,30 +72,33 @@ class Note(Sortable):
|
||||
project = models.ForeignKey(Project)
|
||||
text = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.text
|
||||
|
||||
|
||||
# Registered as a tabular inline on `Project` which can't be sorted
|
||||
@python_2_unicode_compatible
|
||||
class NonSortableCredit(models.Model):
|
||||
project = models.ForeignKey(Project)
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '{0} {1}'.format(self.first_name, self.last_name)
|
||||
|
||||
|
||||
# Registered as a stacked inline on `Project` which can't be sorted
|
||||
@python_2_unicode_compatible
|
||||
class NonSortableNote(models.Model):
|
||||
project = models.ForeignKey(Project)
|
||||
text = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.text
|
||||
|
||||
|
||||
# A generic bound model
|
||||
@python_2_unicode_compatible
|
||||
class GenericNote(SimpleModel, Sortable):
|
||||
content_type = models.ForeignKey(ContentType,
|
||||
verbose_name=u"Content type", related_name="generic_notes")
|
||||
@@ -101,22 +109,23 @@ class GenericNote(SimpleModel, Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
pass
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return u'{0}: {1}'.format(self.title, self.content_object)
|
||||
|
||||
|
||||
# An model registered as an inline that has a custom queryset
|
||||
@python_2_unicode_compatible
|
||||
class Component(SimpleModel, Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
pass
|
||||
|
||||
widget = SortableForeignKey(Widget)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
verbose_name_plural = 'People'
|
||||
@@ -134,19 +143,21 @@ class Person(Sortable):
|
||||
('Non-Board Members', {'is_board_member': False}),
|
||||
)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '{0} {1}'.format(self.first_name, self.last_name)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class NonSortableCategory(SimpleModel):
|
||||
class Meta(SimpleModel.Meta):
|
||||
verbose_name = 'Non-Sortable Category'
|
||||
verbose_name_plural = 'Non-Sortable Categories'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class SortableCategoryWidget(SimpleModel, Sortable):
|
||||
class Meta(Sortable.Meta):
|
||||
verbose_name = 'Sortable Category Widget'
|
||||
@@ -154,10 +165,11 @@ class SortableCategoryWidget(SimpleModel, Sortable):
|
||||
|
||||
non_sortable_category = SortableForeignKey(NonSortableCategory)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class SortableNonInlineCategory(SimpleModel, Sortable):
|
||||
"""Example of a model that is sortable, but has a SortableForeignKey
|
||||
that is *not* sortable, and is also not defined as an inline of the
|
||||
@@ -169,5 +181,5 @@ class SortableNonInlineCategory(SimpleModel, Sortable):
|
||||
|
||||
non_sortable_category = SortableForeignKey(NonSortableCategory)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import httplib
|
||||
try:
|
||||
import httplib
|
||||
except ImportError:
|
||||
import http.client as httplib
|
||||
|
||||
import json
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
@@ -118,7 +122,8 @@ class SortableTestCase(TestCase):
|
||||
#make a normal POST
|
||||
response = self.client.post(self.get_sorting_url(),
|
||||
data=self.get_category_indexes(category1, category2, category3))
|
||||
content = json.loads(response.content)
|
||||
content = json.loads(response.content.decode(encoding='UTF-8'),
|
||||
'latin-1')
|
||||
self.assertFalse(content.get('objects_sorted'),
|
||||
'Objects should not have been sorted. An ajax post is required.')
|
||||
|
||||
@@ -134,7 +139,8 @@ class SortableTestCase(TestCase):
|
||||
response = self.client.post(self.get_sorting_url(),
|
||||
data=self.get_category_indexes(category3, category2, category1),
|
||||
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
content = json.loads(response.content)
|
||||
content = json.loads(response.content.decode(encoding='UTF-8'),
|
||||
'latin-1')
|
||||
self.assertTrue(content.get('objects_sorted'),
|
||||
'Objects should have been sorted.')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user