[Python-modules-commits] [python-django-ordered-model] 01/07: Import python-django-ordered-model_1.3.0.orig.tar.gz

Michael Fladischer fladi at moszumanska.debian.org
Mon Oct 17 08:56:35 UTC 2016


This is an automated email from the git hooks/post-receive script.

fladi pushed a commit to branch master
in repository python-django-ordered-model.

commit e65c3d1cd99648233be5e7a96d2b9de81d444af4
Author: Michael Fladischer <FladischerMichael at fladi.at>
Date:   Mon Oct 17 10:36:53 2016 +0200

    Import python-django-ordered-model_1.3.0.orig.tar.gz
---
 CHANGES.md                    |  6 ++++
 PKG-INFO                      |  2 +-
 README.md                     | 13 +++++++
 ordered_model/admin.py        | 16 ++++-----
 ordered_model/models.py       | 43 ++++++++++++++---------
 ordered_model/tests/models.py |  3 +-
 ordered_model/tests/tests.py  | 82 +++++++++++++++++++++++++++++++++++++++++++
 setup.py                      |  2 +-
 8 files changed, 140 insertions(+), 27 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index b11cb70..de7b0a8 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,6 +1,12 @@
 Change log
 ==========
 
+1.3.0 – 2016-10-08
+------------------
+
+ - Add `extra_update` argument to various methods.
+ - Fix bug in `order_with_respect_to` when using string in Python 3.
+
 1.2.1 – 2016-07-12
 ------------------
 
diff --git a/PKG-INFO b/PKG-INFO
index 47397ba..057d05d 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: django-ordered-model
-Version: 1.2.1
+Version: 1.3.0
 Summary: Allows Django models to be ordered and provides a simple admin interface for reordering them.
 Home-page: http://github.com/bfirsh/django-ordered-model
 Author: Ben Firshman
diff --git a/README.md b/README.md
index 5c8af74..2de5075 100644
--- a/README.md
+++ b/README.md
@@ -92,6 +92,19 @@ the order value of all objects that were above the moved object by one.
 This sets the order value to the highest value found in the stack and decreases
 the order value of all objects that were below the moved object by one.
 
+### Updating fields that would be updated during save()
+
+For performance reasons, the delete(), to(), below(), above(), top(), and bottom()
+methods use Django's update() method to change the order of other objects that
+are shifted as a result of one of these calls. If the model has fields that
+are typically updated in a customized save() method, or through other app level 
+functionality such as DateTimeField(auto_now=True), you can add additional fields
+to be passed through to update(). This will only impact objects where their order
+is being shifted as a result of an operation on the target object, not the target
+object itself.
+
+    foo.to(12, extra_update={'modified': now()}
+ 
 ## Subset Ordering
 
 In some cases, ordering objects is required only on a subset of objects. For example,
diff --git a/ordered_model/admin.py b/ordered_model/admin.py
index 4f2014c..9aef17a 100644
--- a/ordered_model/admin.py
+++ b/ordered_model/admin.py
@@ -51,7 +51,7 @@ class OrderedModelAdmin(admin.ModelAdmin):
         obj = get_object_or_404(self.model, pk=unquote(object_id))
         obj.move(direction, qs)
 
-        return HttpResponseRedirect('../../%s' % self.request_query_string)
+        return HttpResponseRedirect('../../{0!s}'.format(self.request_query_string))
 
     def move_up_down_links(self, obj):
         model_info = self._get_model_info()
@@ -194,13 +194,13 @@ class OrderedTabularInline(admin.TabularInline):
         # Apply keyword searches.
         def construct_search(field_name):
             if field_name.startswith('^'):
-                return "%s__istartswith" % field_name[1:]
+                return "{0!s}__istartswith".format(field_name[1:])
             elif field_name.startswith('='):
-                return "%s__iexact" % field_name[1:]
+                return "{0!s}__iexact".format(field_name[1:])
             elif field_name.startswith('@'):
-                return "%s__search" % field_name[1:]
+                return "{0!s}__search".format(field_name[1:])
             else:
-                return "%s__icontains" % field_name
+                return "{0!s}__icontains".format(field_name)
 
         use_distinct = False
         search_fields = cls.get_search_fields(request)
@@ -226,7 +226,7 @@ class OrderedTabularInline(admin.TabularInline):
         obj = get_object_or_404(cls.model, pk=unquote(object_id))
         obj.move(direction, qs)
 
-        return HttpResponseRedirect('../../../%s' % cls.request_query_string)
+        return HttpResponseRedirect('../../../{0!s}'.format(cls.request_query_string))
 
     @classmethod
     def get_preserved_filters(cls, request):
@@ -236,8 +236,8 @@ class OrderedTabularInline(admin.TabularInline):
         match = request.resolver_match
         if cls.preserve_filters and match:
             opts = cls.model._meta
-            current_url = '%s:%s' % (match.app_name, match.url_name)
-            changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
+            current_url = '{0!s}:{1!s}'.format(match.app_name, match.url_name)
+            changelist_url = 'admin:{0!s}_{1!s}_changelist'.format(opts.app_label, opts.model_name)
             if current_url == changelist_url:
                 preserved_filters = request.GET.urlencode()
             else:
diff --git a/ordered_model/models.py b/ordered_model/models.py
index 916cc91..8da7772 100644
--- a/ordered_model/models.py
+++ b/ordered_model/models.py
@@ -4,6 +4,7 @@ from django.core.urlresolvers import reverse
 from django.db import models
 from django.db.models import Max, Min, F
 from django.utils.translation import ugettext as _
+import six
 
 
 class OrderedModelBase(models.Model):
@@ -23,7 +24,7 @@ class OrderedModelBase(models.Model):
         abstract = True
 
     def _get_order_with_respect_to(self):
-        if type(self.order_with_respect_to) is str:
+        if isinstance(self.order_with_respect_to, six.string_types):
             self.order_with_respect_to = (self.order_with_respect_to,)
         if self.order_with_respect_to is None:
             raise AssertionError(('ordered model admin "{0}" has not specified "order_with_respect_to"; note that this '
@@ -52,8 +53,12 @@ class OrderedModelBase(models.Model):
 
     def delete(self, *args, **kwargs):
         qs = self.get_ordering_queryset()
+        update_kwargs = {self.order_field_name: F(self.order_field_name) - 1}
+        extra = kwargs.pop('extra_update', None)
+        if extra:
+            update_kwargs.update(extra) 
         qs.filter(**{self.order_field_name + '__gt': getattr(self, self.order_field_name)})\
-          .update(**{self.order_field_name: F(self.order_field_name) - 1})
+          .update(**update_kwargs)
         super(OrderedModelBase, self).delete(*args, **kwargs)
 
     def _move(self, up, qs=None):
@@ -116,7 +121,7 @@ class OrderedModelBase(models.Model):
             return
         if not self._valid_ordering_reference(replacement):
             raise ValueError(
-                "%r can only be swapped with instances of %r with equal %s fields." % (
+                "{0!r} can only be swapped with instances of {1!r} with equal {2!s} fields.".format(
                     self, self.__class__, ' and '.join(["'{}'".format(o[0]) for o in self._get_order_with_respect_to()])
                 )
             )
@@ -140,7 +145,7 @@ class OrderedModelBase(models.Model):
         """
         self.swap(self.get_ordering_queryset().filter(**{self.order_field_name + '__gt': getattr(self, self.order_field_name)}))
 
-    def to(self, order):
+    def to(self, order, extra_update=None):
         """
         Move object to a certain position, updating all affected objects to move accordingly up or down.
         """
@@ -149,23 +154,29 @@ class OrderedModelBase(models.Model):
             return
         qs = self.get_ordering_queryset()
         if getattr(self, self.order_field_name) > order:
+            update_kwargs = {self.order_field_name: F(self.order_field_name) + 1}
+            if extra_update:
+                update_kwargs.update(extra_update)
             qs.filter(**{self.order_field_name + '__lt': getattr(self, self.order_field_name),
                          self.order_field_name + '__gte': order})\
-              .update(**{self.order_field_name: F(self.order_field_name) + 1})
+              .update(**update_kwargs)
         else:
+            update_kwargs = {self.order_field_name: F(self.order_field_name) - 1}
+            if extra_update:
+                update_kwargs.update(extra_update)
             qs.filter(**{self.order_field_name + '__gt': getattr(self, self.order_field_name),
                          self.order_field_name + '__lte': order})\
-              .update(**{self.order_field_name: F(self.order_field_name) - 1})
+              .update(**update_kwargs)
         setattr(self, self.order_field_name, order)
         self.save()
 
-    def above(self, ref):
+    def above(self, ref, extra_update=None):
         """
         Move this object above the referenced object.
         """
         if not self._valid_ordering_reference(ref):
             raise ValueError(
-                "%r can only be swapped with instances of %r with equal %s fields." % (
+                "{0!r} can only be swapped with instances of {1!r} with equal {2!s} fields.".format(
                     self, self.__class__, ' and '.join(["'{}'".format(o[0]) for o in self._get_order_with_respect_to()])
                 )
             )
@@ -178,15 +189,15 @@ class OrderedModelBase(models.Model):
                     .filter(**{self.order_field_name + '__lt': getattr(ref, self.order_field_name)})\
                     .aggregate(Max(self.order_field_name))\
                     .get(self.order_field_name + '__max') or 0
-        self.to(o)
+        self.to(o, extra_update=extra_update)
 
-    def below(self, ref):
+    def below(self, ref, extra_update=None):
         """
         Move this object below the referenced object.
         """
         if not self._valid_ordering_reference(ref):
             raise ValueError(
-                "%r can only be swapped with instances of %r with equal %s fields." % (
+                "{0!r} can only be swapped with instances of {1!r} with equal {2!s} fields.".format(
                     self, self.__class__, ' and '.join(["'{}'".format(o[0]) for o in self._get_order_with_respect_to()])
                 )
             )
@@ -199,21 +210,21 @@ class OrderedModelBase(models.Model):
                     .get(self.order_field_name + '__min') or 0
         else:
             o = getattr(ref, self.order_field_name)
-        self.to(o)
+        self.to(o, extra_update=extra_update)
 
-    def top(self):
+    def top(self, extra_update=None):
         """
         Move this object to the top of the ordered stack.
         """
         o = self.get_ordering_queryset().aggregate(Min(self.order_field_name)).get(self.order_field_name + '__min')
-        self.to(o)
+        self.to(o, extra_update=extra_update)
 
-    def bottom(self):
+    def bottom(self, extra_update=None):
         """
         Move this object to the bottom of the ordered stack.
         """
         o = self.get_ordering_queryset().aggregate(Max(self.order_field_name)).get(self.order_field_name + '__max')
-        self.to(o)
+        self.to(o, extra_update=extra_update)
 
 
 class OrderedModel(OrderedModelBase):
diff --git a/ordered_model/tests/models.py b/ordered_model/tests/models.py
index a4a77d8..b0dda26 100644
--- a/ordered_model/tests/models.py
+++ b/ordered_model/tests/models.py
@@ -23,12 +23,13 @@ class Answer(OrderedModel):
         ordering = ('question', 'user', 'order')
 
     def __unicode__(self):
-        return u"Answer #%d of question #%d for user #%d" % (self.order, self.question_id, self.user_id)
+        return u"Answer #{0:d} of question #{1:d} for user #{2:d}".format(self.order, self.question_id, self.user_id)
 
 
 class CustomItem(OrderedModel):
     id = models.CharField(max_length=100, primary_key=True)
     name = models.CharField(max_length=100)
+    modified = models.DateTimeField(null=True, blank=True)
 
 
 class CustomOrderFieldModel(OrderedModelBase):
diff --git a/ordered_model/tests/tests.py b/ordered_model/tests/tests.py
index 604889a..8ebb1b6 100644
--- a/ordered_model/tests/tests.py
+++ b/ordered_model/tests/tests.py
@@ -1,5 +1,6 @@
 from django.contrib import admin
 from django.contrib.auth.models import User
+from django.utils.timezone import now
 from django.test import TestCase
 import uuid
 from ordered_model.tests.models import (
@@ -202,6 +203,87 @@ class CustomPKTest(TestCase):
                 (self.item4.pk, 3)
             ]
         )
+    
+    def test_order_to_extra_update(self):
+        modified_time = now()
+        self.item1.to(3, extra_update={'modified':modified_time})
+        self.assertSequenceEqual(
+            CustomItem.objects.values_list('pk', 'order', 'modified'), [
+                (self.item2.pk, 0, modified_time),
+                (self.item3.pk, 1, modified_time),
+                (self.item4.pk, 2, modified_time),
+                # This one is the primary item being operated on and modified would be 
+                # handled via auto_now or something
+                (self.item1.pk, 3, None)
+            ]
+        )
+    
+    def test_bottom_extra_update(self):
+        modified_time = now()
+        self.item1.bottom(extra_update={'modified':modified_time})
+        self.assertSequenceEqual(
+            CustomItem.objects.values_list('pk', 'order', 'modified'), [
+                (self.item2.pk, 0, modified_time),
+                (self.item3.pk, 1, modified_time),
+                (self.item4.pk, 2, modified_time),
+                # This one is the primary item being operated on and modified would be 
+                # handled via auto_now or something
+                (self.item1.pk, 3, None)
+            ]
+        )
+    
+    def test_top_extra_update(self):
+        modified_time = now()
+        self.item4.top(extra_update={'modified':modified_time})
+        self.assertSequenceEqual(
+            CustomItem.objects.values_list('pk', 'order', 'modified'), [
+                (self.item4.pk, 0, None),
+                (self.item1.pk, 1, modified_time),
+                (self.item2.pk, 2, modified_time),
+                # This one is the primary item being operated on and modified would be 
+                # handled via auto_now or something
+                (self.item3.pk, 3, modified_time)
+            ]
+        )
+    
+    def test_below_extra_update(self):
+        modified_time = now()
+        self.item1.below(self.item4, extra_update={'modified':modified_time})
+        self.assertSequenceEqual(
+            CustomItem.objects.values_list('pk', 'order', 'modified'), [
+                (self.item2.pk, 0, modified_time),
+                (self.item3.pk, 1, modified_time),
+                (self.item4.pk, 2, modified_time),
+                # This one is the primary item being operated on and modified would be 
+                # handled via auto_now or something
+                (self.item1.pk, 3, None)
+            ]
+        )
+    
+    def test_above_extra_update(self):
+        modified_time = now()
+        self.item4.above(self.item1, extra_update={'modified':modified_time})
+        self.assertSequenceEqual(
+            CustomItem.objects.values_list('pk', 'order', 'modified'), [
+                (self.item4.pk, 0, None),
+                (self.item1.pk, 1, modified_time),
+                (self.item2.pk, 2, modified_time),
+                # This one is the primary item being operated on and modified would be 
+                # handled via auto_now or something
+                (self.item3.pk, 3, modified_time)
+            ]
+        )
+    
+    def test_delete_extra_update(self):
+        modified_time = now()
+        self.item1.delete(extra_update={'modified':modified_time})
+        self.assertSequenceEqual(
+            CustomItem.objects.values_list('pk', 'order', 'modified'), [
+                (self.item2.pk, 0, modified_time),
+                (self.item3.pk, 1, modified_time),
+                (self.item4.pk, 2, modified_time),
+            ]
+        )
 
 
 
diff --git a/setup.py b/setup.py
index 7af3bbd..678315b 100644
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@ from distutils.core import setup
  
 setup(
     name='django-ordered-model',
-    version='1.2.1',
+    version='1.3.0',
     description='Allows Django models to be ordered and provides a simple admin interface for reordering them.',
     author='Ben Firshman',
     author_email='ben at firshman.co.uk',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-django-ordered-model.git



More information about the Python-modules-commits mailing list