Merge branch 'sortable-by-refactor'
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
VERSION = (1, 1, 1, "f", 0) # following PEP 386
|
||||
VERSION = (1, 2, "f", 0) # following PEP 386
|
||||
DEV_N = None
|
||||
|
||||
|
||||
|
||||
+20
-10
@@ -53,18 +53,28 @@ class SortableAdmin(ModelAdmin):
|
||||
has_perm = request.user.has_perm(opts.app_label + '.' + opts.get_change_permission())
|
||||
objects = self.model.objects.all()
|
||||
|
||||
"""
|
||||
Determine if we need to regroup objects relative to a foreign key specified on the
|
||||
model class that is extending Sortable.
|
||||
"""
|
||||
#Determine if we need to regroup objects relative to a foreign key specified on the
|
||||
# model class that is extending Sortable.
|
||||
sortable_by = getattr(self.model, 'sortable_by', None)
|
||||
if sortable_by:
|
||||
sortable_by_class, sortable_by_expression = sortable_by()
|
||||
#backwards compatibility for < 1.1.1, where sortable_by was a classmethod instead of a property
|
||||
try:
|
||||
sortable_by_class, sortable_by_expression = sortable_by()
|
||||
except TypeError, ValueError:
|
||||
sortable_by_class = self.model.sortable_by
|
||||
sortable_by_expression = sortable_by_class.__name__.lower()
|
||||
|
||||
sortable_by_class_display_name = sortable_by_class._meta.verbose_name_plural
|
||||
sortable_by_class_is_sortable = sortable_by_class.is_sortable()
|
||||
|
||||
# Order the objects by the property they are sortable by, then by the order, otherwise the regroup
|
||||
# template tag will not show the objects correctly as
|
||||
# shown in https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#regroup
|
||||
objects = objects.order_by(sortable_by_expression, 'order')
|
||||
|
||||
else:
|
||||
sortable_by_class = sortable_by_expression = sortable_by_class_display_name = \
|
||||
sortable_by_class_is_sortable = None
|
||||
sortable_by_class = sortable_by_expression = sortable_by_class_display_name =\
|
||||
sortable_by_class_is_sortable = None
|
||||
|
||||
try:
|
||||
verbose_name_plural = opts.verbose_name_plural.__unicode__()
|
||||
@@ -110,7 +120,6 @@ class SortableAdmin(ModelAdmin):
|
||||
This view sets the ordering of the objects for the model type and primary keys
|
||||
passed in. It must be an Ajax POST.
|
||||
"""
|
||||
|
||||
if request.is_ajax() and request.method == 'POST':
|
||||
try:
|
||||
indexes = map(str, request.POST.get('indexes', []).split(','))
|
||||
@@ -131,7 +140,7 @@ class SortableAdmin(ModelAdmin):
|
||||
else:
|
||||
response = {'objects_sorted' : False}
|
||||
return HttpResponse(json.dumps(response, ensure_ascii=False),
|
||||
mimetype='application/json')
|
||||
mimetype='application/json')
|
||||
|
||||
|
||||
class SortableInlineBase(InlineModelAdmin):
|
||||
@@ -139,7 +148,8 @@ class SortableInlineBase(InlineModelAdmin):
|
||||
super(SortableInlineBase, self).__init__(*args, **kwargs)
|
||||
|
||||
if not issubclass(self.model, Sortable):
|
||||
raise Warning(u'Models that are specified in SortableTabluarInline and SortableStackedInline must inherit from Sortable')
|
||||
raise Warning(u'Models that are specified in SortableTabluarInline and SortableStackedInline '
|
||||
'must inherit from Sortable')
|
||||
|
||||
self.is_sortable = self.model.is_sortable()
|
||||
|
||||
|
||||
@@ -14,8 +14,12 @@ class Sortable(models.Model):
|
||||
`model_type_id` returns the ContentType.id for the Model that inherits Sortable
|
||||
|
||||
`save` the override of save increments the last/highest value of order by 1
|
||||
|
||||
Override `sortable_by` method to make your model be sortable by a foreign key field.
|
||||
Set `sortable_by` to the class specified in the foreign key relationship.
|
||||
"""
|
||||
order = models.PositiveIntegerField(editable=False, default=1, db_index=True)
|
||||
sortable_by = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
{% if objects %}
|
||||
<div id="sortable">
|
||||
{% if group_expression %}
|
||||
{% render_nested_sortable_objects objects group_expression %}
|
||||
{% render_nested_sortable_objects objects group_expression %}
|
||||
{% else %}
|
||||
{% render_sortable_objects objects %}
|
||||
{% endif %}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/jquery.ui.draggable.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/jquery.ui.droppable.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/jquery.ui.sortable.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/jquery.effects.core.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/jquery.effects.highlight.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/admin.sortable.stacked.inlines.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/admin.sortable.js"></script>
|
||||
<script type="text/javascript" src="{{ STATIC_URL }}adminsortable/js/admin.sortable.js"></script>
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
{% load django_template_additions adminsortable_tags %}
|
||||
{% dynamic_regroup objects by group_expression as regrouped_objects %}
|
||||
{% if regrouped_objects %}
|
||||
|
||||
<ul {% if sortable_by_class_is_sortable %}class="sortable"{% endif %}>
|
||||
{% for regrouped_object in regrouped_objects %}
|
||||
<li>
|
||||
{% with object=regrouped_object.grouper %}
|
||||
{% render_object_rep object %}
|
||||
{% endwith %}
|
||||
{% if regrouped_object.list %}
|
||||
<ul {% if regrouped_object.grouper.is_sortable %}class="sortable"{% endif %}>
|
||||
{% render_list_items regrouped_object.list %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
<ul {% if sortable_by_class_is_sortable %}class="sortable"{% endif %}>
|
||||
{% for regrouped_object in regrouped_objects %}
|
||||
<li>
|
||||
{% with object=regrouped_object.grouper %}
|
||||
{% render_object_rep object %}
|
||||
{% endwith %}
|
||||
{% if regrouped_object.list %}
|
||||
<ul {% if regrouped_object.grouper.is_sortable %}class="sortable"{% endif %}>
|
||||
{% render_list_items regrouped_object.list %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
@@ -25,11 +25,9 @@ class DynamicRegroupNode(template.Node):
|
||||
# List of dictionaries in the format:
|
||||
# {'grouper': 'key', 'list': [list of contents]}.
|
||||
|
||||
"""
|
||||
Try to resolve the filter expression from the template context.
|
||||
If the variable doesn't exist, accept the value that passed to the
|
||||
template tag and convert it to a string
|
||||
"""
|
||||
#Try to resolve the filter expression from the template context.
|
||||
#If the variable doesn't exist, accept the value that passed to the
|
||||
#template tag and convert it to a string
|
||||
try:
|
||||
exp = self.expression.resolve(context)
|
||||
except template.VariableDoesNotExist:
|
||||
@@ -47,6 +45,15 @@ class DynamicRegroupNode(template.Node):
|
||||
|
||||
@register.tag
|
||||
def dynamic_regroup(parser, token):
|
||||
"""
|
||||
Django expects the value of `expression` to be an attribute available on
|
||||
your objects. The value you pass to the template tag gets converted into a
|
||||
FilterExpression object from the literal.
|
||||
|
||||
Sometimes we need the attribute to group on to be dynamic. So, instead
|
||||
of converting the value to a FilterExpression here, we're going to pass the
|
||||
value as-is and convert it in the Node.
|
||||
"""
|
||||
firstbits = token.contents.split(None, 3)
|
||||
if len(firstbits) != 4:
|
||||
raise TemplateSyntaxError("'regroup' tag takes five arguments")
|
||||
@@ -58,20 +65,8 @@ def dynamic_regroup(parser, token):
|
||||
raise TemplateSyntaxError("next-to-last argument to 'regroup' tag must"
|
||||
" be 'as'")
|
||||
|
||||
"""
|
||||
Django expects the value of `expression` to be an attribute available on
|
||||
your objects. The value you pass to the template tag gets converted into a
|
||||
FilterExpression object from the literal.
|
||||
|
||||
Sometimes we need the attribute to group on to be dynamic. So, instead
|
||||
of converting the value to a FilterExpression here, we're going to pass the
|
||||
value as-is and convert it in the Node.
|
||||
"""
|
||||
expression = lastbits_reversed[2][::-1]
|
||||
var_name = lastbits_reversed[0][::-1]
|
||||
|
||||
"""
|
||||
We also need to hand the parser to the node in order to convert the value
|
||||
for `expression` to a FilterExpression.
|
||||
"""
|
||||
#We also need to hand the parser to the node in order to convert the value
|
||||
#for `expression` to a FilterExpression.
|
||||
return DynamicRegroupNode(target, parser, expression, var_name)
|
||||
|
||||
Reference in New Issue
Block a user