Added Person model with sorting_filters set to only order people who are board members.

Added custom template override to specify which people are sortable in change list.
Added initial data fixture for people.
master
Brandon Taylor 2014-02-05 11:06:15 -05:00
parent e51f7535ca
commit 1b4730fef9
10 changed files with 35 additions and 37 deletions

View File

@ -28,6 +28,8 @@ STATIC_URL = settings.STATIC_URL
class SortableAdminBase(object): class SortableAdminBase(object):
filtered_objects = []
def changelist_view(self, request, extra_context=None): def changelist_view(self, request, extra_context=None):
""" """
If the model that inherits Sortable has more than one object, If the model that inherits Sortable has more than one object,
@ -35,12 +37,11 @@ class SortableAdminBase(object):
object_tools block to take people to the view to change the sorting. object_tools block to take people to the view to change the sorting.
""" """
if self.model.ordering_subset() is not None: # Apply any additional filters to create a subset of sortable objects
objects = self.model.ordering_subset() self.filtered_objects = self.queryset(request).filter(
else: **self.model.sorting_filters)
objects = self.queryset(request)
if get_is_sortable(objects): if get_is_sortable(self.filtered_objects):
self.change_list_template = \ self.change_list_template = \
self.sortable_change_list_with_sort_link_template self.sortable_change_list_with_sort_link_template
self.is_sortable = True self.is_sortable = True
@ -66,8 +67,6 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
'adminsortable/change_list_with_sort_link.html' 'adminsortable/change_list_with_sort_link.html'
sortable_change_form_template = 'adminsortable/change_form.html' sortable_change_form_template = 'adminsortable/change_form.html'
sortable_change_list_template = 'adminsortable/change_list.html' sortable_change_list_template = 'adminsortable/change_list.html'
sortable_javascript_includes_template = \
'adminsortable/shared/javascript_includes.html'
change_form_template_extends = 'admin/change_form.html' change_form_template_extends = 'admin/change_form.html'
change_list_template_extends = 'admin/change_list.html' change_list_template_extends = 'admin/change_list.html'
@ -107,10 +106,7 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
has_perm = request.user.has_perm('{}.{}'.format(opts.app_label, has_perm = request.user.has_perm('{}.{}'.format(opts.app_label,
opts.get_change_permission())) opts.get_change_permission()))
if self.model.ordering_subset() is not None: objects = self.filtered_objects
objects = self.model.ordering_subset()
else:
objects = self.queryset(request)
# 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.
@ -169,9 +165,7 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
'group_expression': sortable_by_expression, 'group_expression': sortable_by_expression,
'sortable_by_class': sortable_by_class, 'sortable_by_class': sortable_by_class,
'sortable_by_class_is_sortable': sortable_by_class_is_sortable, 'sortable_by_class_is_sortable': sortable_by_class_is_sortable,
'sortable_by_class_display_name': sortable_by_class_display_name, 'sortable_by_class_display_name': sortable_by_class_display_name
'sortable_javascript_includes_template':
self.sortable_javascript_includes_template
} }
return render(request, self.sortable_change_list_template, context) return render(request, self.sortable_change_list_template, context)
@ -208,8 +202,6 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
self.change_form_template = self.sortable_change_form_template self.change_form_template = self.sortable_change_form_template
extra_context.update({ extra_context.update({
'sortable_javascript_includes_template':
self.sortable_javascript_includes_template,
'has_sortable_tabular_inlines': 'has_sortable_tabular_inlines':
self.has_sortable_tabular_inlines, self.has_sortable_tabular_inlines,
'has_sortable_stacked_inlines': 'has_sortable_stacked_inlines':

View File

@ -31,6 +31,7 @@ class Sortable(models.Model):
order = models.PositiveIntegerField(editable=False, default=1, order = models.PositiveIntegerField(editable=False, default=1,
db_index=True) db_index=True)
is_sortable = False is_sortable = False
sorting_filters = {}
# legacy support # legacy support
sortable_by = None sortable_by = None
@ -43,10 +44,6 @@ class Sortable(models.Model):
def model_type_id(cls): def model_type_id(cls):
return ContentType.objects.get_for_model(cls).id return ContentType.objects.get_for_model(cls).id
@classmethod
def ordering_subset(cls):
return None
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Sortable, self).__init__(*args, **kwargs) super(Sortable, self).__init__(*args, **kwargs)

View File

@ -9,11 +9,11 @@
{% block extrahead %} {% block extrahead %}
{{ block.super }} {{ block.super }}
<script type="text/javascript" src="{% static 'admin/js/jquery.js' %}"></script> <script src="{% static 'admin/js/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.init.js' %}"></script> <script src="{% static 'admin/js/jquery.init.js' %}"></script>
<script type="text/javascript" src="{% static 'adminsortable/js/jquery-ui-django-admin.min.js' %}"></script> <script src="{% static 'adminsortable/js/jquery-ui-django-admin.min.js' %}"></script>
<script type="text/javascript" src="{% static 'adminsortable/js/jquery.django-csrf.js' %}"></script> <script src="{% static 'adminsortable/js/jquery.django-csrf.js' %}"></script>
<script type="text/javascript" src="{% static 'adminsortable/js/admin.sortable.js' %}"></script> <script src="{% static 'adminsortable/js/admin.sortable.js' %}"></script>
{% endblock %} {% endblock %}
{% block title %}{% blocktrans with opts.verbose_name_plural|capfirst as model %}Drag and drop {{ model }} to change display order{% endblocktrans %} | {% trans 'Django site admin' %}{% endblock %} {% block title %}{% blocktrans with opts.verbose_name_plural|capfirst as model %}Drag and drop {{ model }} to change display order{% endblocktrans %} | {% trans 'Django site admin' %}{% endblock %}

View File

@ -1,4 +1,5 @@
def get_is_sortable(objects): def get_is_sortable(objects):
if objects:
if objects.count() > 1: if objects.count() > 1:
return True return True
return False return False

View File

@ -60,4 +60,8 @@ class ProjectAdmin(SortableAdmin):
admin.site.register(Project, ProjectAdmin) admin.site.register(Project, ProjectAdmin)
admin.site.register(Person, SortableAdmin) class PersonAdmin(SortableAdmin):
sortable_change_list_with_sort_link_template = 'app/person/admin/change_list_with_sort_link.html'
admin.site.register(Person, PersonAdmin)

View File

@ -105,9 +105,7 @@ class Person(Sortable):
last_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
is_board_member = models.BooleanField(default=False) is_board_member = models.BooleanField(default=False)
sorting_filters = {'is_board_member': True}
def __unicode__(self): def __unicode__(self):
return '{} {}'.format(self.first_name, self.last_name) return '{} {}'.format(self.first_name, self.last_name)
@classmethod
def ordering_subset(cls):
return cls.objects.filter(is_board_member=True)

View File

@ -1,2 +1,2 @@
django==1.5.2 django==1.6.1
south==0.8.1 south==0.8.1

View File

@ -109,10 +109,7 @@ ROOT_URLCONF = 'sample_project.urls'
WSGI_APPLICATION = 'sample_project.wsgi.application' WSGI_APPLICATION = 'sample_project.wsgi.application'
TEMPLATE_DIRS = ( TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or map_path('templates'),
# "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
) )
INSTALLED_APPS = ( INSTALLED_APPS = (

View File

@ -0,0 +1,9 @@
{% extends change_list_template_extends %}
{% load i18n %}
{% block object-tools-items %}
<li>
<a href="./sort/">{% trans 'Change Order of Board Members' %}</a>
</li>
{{ block.super }}
{% endblock %}