Commit Graph

107 Commits (26b11e8b41ff04de0b652aa134ef324c4a671d5c)

Author SHA1 Message Date
Brandon Taylor 478600fd5a - Appplied filters to queryset check for length of items in change list
- Added conditional in template to show/hide sort link
2019-02-14 16:04:17 -05:00
Brandon Taylor 1e395ab3f0 Properly handle querystring filters 2019-02-12 11:15:17 -05:00
Brandon Taylor ff4dc3184f Fix Missing CSRF Token
- Set default CSRF_HEADER_NAME to previous value instead of Django default
- Added csrfmiddlewaretoken value to Ajax POST data from sorting views.
- Moved csrf token template tag inside object representation form
2019-01-17 13:32:02 -05:00
Brandon Taylor c15f307e5a CSRF_HEADER_NAME
- Get CSRF_HEADER_NAME from settings to pass to jquery.django-csrf.html template
- Version bump to 2.1.10
- Update readme
2019-01-14 09:40:54 -05:00
Brandon Taylor 6d5f9e97b4 Added `after_sorting` method to SortableAdminBase class that can be defined on a model admin to be executed after sorting has occurred. 2018-07-09 07:49:55 -04:00
Brandon Taylor 5ad2c0e4ec Merged after-sort-callback branch and fixed conflicts. 2018-06-18 11:41:20 -04:00
Brandon Taylor e35f36b25a Refactored sorting JS files to be includes so that server-side variables may be passed to them.
Added 'after_sorting_js_callback_name' attribute to SortableAdminBase.
Added callback to be executed after sorting for each of the possible sorting scenarios.
Added custom template examples to add a callback to be executed when sorting is finished.
2018-06-18 11:40:24 -04:00
timur-orudzhov f135ad906d add support for django admin filters 2018-05-16 16:05:40 +03:00
Brandon Taylor fa8a5f12a8 Merge branch 'master' into develop 2018-03-19 21:40:06 -04:00
Brandon Taylor 041558823e Fixed return outside of function 2018-03-19 21:23:59 -04:00
Tai Lee 108ef6dd18 Improve performance of sort view for moods and elements.
* Use `{% include  "..." with ... %}` instead of template tags that do
  nothing but pass through or rename context variables and render a
  template. This appears to yield a 2x increase in performance.

  As a side effect, this change also appears to fix some glitches with
  the rendering of `fa-sort`, `fa-sort-asc` and `fa-sort-desc` icons.

* Move queryset filtering from `sort_view()` to new `get_sort_view_queryset()`
  method, so subclasses can override to apply different or additional
  filtering (based on `request` and `sortable_by_expression`) to reduce
  the number of objects being reordered.

  `django-admin-sortable` already provides a mechanism to reorder a
  subset of objects via `sorting_filters`, but this is restricted to a
  small number of hard coded filters, and we found it not very useful.

  We have tens of thousands of nested objects grouped under hundreds or
  thousands of parent objects, and we needed a way to reorder child
  objects just within their own group.

  We also needed a way to reorder a subset of flat (not grouped by
  parent) sortable objects with much more flexibility.

Here's an example of additional filtering that allows us to reorder a
contiguous sequence of objects (nested or flat) that bounded by the min
and max (by ordering) selected objects:

```python
class MyBaseSortableAdmin(SortableAdmin):

    def get_sort_view_queryset(self, request, sortable_by_expression):
        """
        Filter the sort view queryset to include only a contiguous sequence of
        objects between the first and last of given parent objects, according
        to the current ordering.

        This should avoid inconsistent or ambiguous behaviour that might occur
        when re-ordering a non-contiguous sequence.
        """
        sortable_by_expression = sortable_by_expression or 'pk'
        queryset = super(MyBaseSortableAdmin, self) \
            .get_sort_view_queryset(request, sortable_by_expression)
        pks = [
            int(pk) for pk in request.GET.get('pks', '').split(',') if pk
        ]
        if pks:
            queryset = queryset.filter(**{
                '%s__in' % sortable_by_expression: pks,
            })
        return queryset

    def reorder_children(self, qs, child):
        # Get the min and max order field value for the selected objects, then
        # get contiguous PKs for objects between the min and max and pass to
        # the sort view, to avoid inconsistent or ambiguous behaviour.
        field = self.opts.ordering[0].replace('-', '')
        qs = qs.model.objects.filter(**qs.aggregate(**{
            '%s__gte' % field: Min(field),
            '%s__lte' % field: Max(field),
        }))
        ct = ContentType.objects.get_for_model(child)
        url = '%ssort/?pks=%s' % (
            reverse('admin:%s_%s_changelist' % (ct.app_label, ct.model)),
            ','.join([str(pk) for pk in qs.values_list('pk', flat=True)]),
        )
        return http.HttpResponseRedirect(url)

class MyModelAdmin(MyBaseSortableAdmin):
    actions = (
        "reorder_mymodel",
        "reorder_childmodel",
    )

    def reorder_mymodel(self, request, qs):
        return self.reorder_children(qs, MyModel)
    reorder_chapters.short_description = 'Reorder selected MyModels'

    def reorder_childmodel(self, request, qs):
        return self.reorder_children(qs, ChildModel)
    reorder_elements.short_description = 'Reorder ChildModels for the selected MyModels'
```

This could be made generic enough for inclusion by default with a few
tweaks, so that `Reorder selected {{ parent.verbose_name_plural }}` and
`Reorder {{ child.verbose_name_plural }} for selected {{ parent.verbose_name_plural }}`
admin actions could be included in sortable change lists.
2018-03-13 23:06:05 +11:00
Jaap Roes 49a7c41896 Use remote_field.model in favor of rel.to when possible 2017-03-16 16:21:59 +01:00
Jaap Roes 8b88dfa9cd Remove pointless fallback code (it just repeats the previously failed statement) 2017-03-16 16:21:58 +01:00
Jaap Roes 356d88dfde Remove ordering field lookup fallback 2017-03-16 16:21:58 +01:00
Jaap Roes ac507e4484 Remove get_queryset fallback 2017-03-16 16:21:58 +01:00
Jaap Roes f17db22938 Remove inline admin fallback templates 2017-03-16 16:21:58 +01:00
Jaap Roes 74e0c92455 Remove contenttypes.generic import fallbacks 2017-03-16 16:21:58 +01:00
Jaap Roes e0a85c554b Remove django.conf.urls import fallback 2017-03-16 16:21:58 +01:00
Anton Shevchenko eb5f14fe22 Clarify comment. 2017-03-11 11:20:16 -05:00
Anton Shevchenko eb5a9e0a8a Avoid unnecessary db queries: perform the update iff the order field has changed. 2017-03-08 23:28:40 -05:00
Anton Shevchenko f2f5daf831 Use 'update_fields' to limit the object's save() method to just the order field, so that other fields are not accidentally overwritten with stale data. 2017-03-08 22:14:48 -05:00
Brandon Taylor d1911519ed Added version check for 1.7 to create context dict. 2016-11-02 21:29:31 -04:00
Igor Sobolev 12fbb62302 Proper context and breadcrumbs 2016-11-02 12:47:36 -04:00
Brandon Taylor 863ff69719 Fix Sorting by ForeignKey
Removed do_inline_sorting_url and get_object_or_404 check in admin. The model_type_id should *always* be passed in.
Removed test that asserted that Categories weren't sortable as part of the Project admin. Categories *should* be sortable as part of Project admin as they are a Sortable ForeignKey.
Fixed object_rep template to pass in model_type_id again.
Updated README.
Version bump to 2.0.21
2016-10-30 11:33:20 -04:00
Brandon Taylor 5566faed5b Update sortable inline templates for Django 1.10.x compatibility. 2016-08-08 20:36:35 -04:00
Brandon Taylor e9b03a3a4f Merge pull request #143 from MagicSolutions/fix/admin-urs
Use separate URLs per model in administration to do the sorting
2016-08-03 10:06:29 -04:00
Brandon Taylor 54482b3bb3 Merge branch 'master' into develop 2016-06-23 21:48:45 -04:00
Venelin Stoykov 2697422497 Play nice with other apps which dynamically choose `change_form_template`
If other app admin class is using `property` for `change_form_template` adminsortable fails.
2016-06-23 16:02:01 +03:00
Venelin Stoykov a479198799 Use separate URLs per model in administration to do the sorting
- Correctly check if current user has permissions to made the change

Fixes #142
2016-06-16 14:41:03 +03:00
Brandon Taylor 307db84b66 Testing admin url cleanup 2016-06-15 20:25:56 -04:00
Venelin Stoykov fb6e1118db Simplify SortableAdmin.get_urls
Preserve compatibility
2016-06-15 16:54:07 +03:00
Brandon Taylor f004d97bf1 Refactored exception handling for order_field_name 2016-03-14 11:09:18 -04:00
Adam M. Peacock bbb88d57ab Removed extra comma 2016-02-23 12:29:59 -05:00
Adam M. Peacock 3ef33043d7 Passed csrf_cookie_name into the changelist_form extra_context to support variable CSRF_COOKIE_NAME 2016-02-23 12:25:18 -05:00
Brandon Taylor c891f25bdf Merge branch 'develop'
Adds custom CSRF_COOKIE_NAME support.
2016-02-16 09:59:50 -05:00
Brandon Taylor c1181b0e52 Support CSRF_COOKIE_NAME
Added support for custom CSRF_COOKIE_NAME by refactoring the jquery.django-csrf.js file into a separate .html file that can be used as an include, passing in the CSRF_COOKIE_NAME from settings.
2016-02-16 08:48:32 -05:00
Brandon Taylor 5823af8047 Merge branch 'master' of github.com:iambrandontaylor/django-admin-sortable 2016-01-27 12:05:12 -05:00
Brandon Taylor a7a6c12deb Refactored check for ordering information to keep backwards compatibility with Django 1.5.x 2016-01-27 11:50:42 -05:00
Brandon Taylor 15d8f20178 Fixed merged conflict in __init__.py
Removed unused import in admin.py
2016-01-27 11:26:03 -05:00
Brandon Taylor 66c73a6bdc Fixed access of custom order property in model meta, falling back to order for legacy implementations.
Updated readme and version bump to 2.0.10
2016-01-27 10:58:58 -05:00
giacomo 84e42720e7 Fix typo in warning
Tabluar -> Tabular
2016-01-12 16:13:15 +01:00
Brandon Taylor 5dee27e077 Added sorting icons.
Refactored determination of sortability of classes referenced as sortable foreign keys in admin in a more reliable way.
2015-12-23 16:39:45 -05:00
Brandon Taylor 698ca136d5 Refactored admin url patterns to be compatible with Django 1.8 or higher.
Refactored sample_project imports to be compatible with Django 1.9
Updated unit tests for get_next/previous.
Version bump to 2.0.8.
Fixed typo in docs.
2015-12-21 14:11:13 -05:00
Daniel Otero 11e445d99e Variable jQuery path for Django version 1.9 and above 2015-12-08 22:04:05 +01:00
zakdoek 995baee2d3 Fix bug when used with descending order 2015-10-20 14:11:49 +02:00
Brandon Taylor bee71e6d40 Refactored order_field_name out of Sortable class, instead deriving from Meta.ordering, since you have to set Meta.ordering to the order field to get the proper Sorting anyway. 2015-09-02 09:38:10 -04:00
Brandon Taylor 6c5a9058fe Minor PEP8, text and formatting tweaks. 2015-08-24 08:18:22 -04:00
Brandon Taylor 43fef78876 Work in progress on refactoring the Sortable class into a non-obtrusive mixin. 2015-08-23 22:25:55 -04:00
Brandon Taylor da0da5e690 Fixes: Bug in get_previous() method #108
Added ordering to _filter_objects internal method to correctly select previous object.
2015-07-05 22:37:20 -04:00
Brandon Taylor ad09081ab4 Added exception handling for get_queryset attribute. 2015-04-08 22:06:14 -04:00