[damned-lies] Replace obsolete __cmp__ method



commit 001a0cb9521a344e681edcdf6fd204ad9bcc26e7
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Aug 15 15:43:53 2015 +0200

    Replace obsolete __cmp__ method
    
    This was not compatible with future versions of Python.

 stats/forms.py       |    2 +-
 stats/models.py      |   19 +++++++++++--------
 stats/tests/tests.py |   18 ++++++++++++++----
 stats/views.py       |    2 +-
 4 files changed, 27 insertions(+), 14 deletions(-)
---
diff --git a/stats/forms.py b/stats/forms.py
index df5c720..e0ad414 100644
--- a/stats/forms.py
+++ b/stats/forms.py
@@ -15,7 +15,7 @@ class ModuleBranchForm(forms.Form):
         self.module = module
         self.branch_fields = []
         default_cat_name = None
-        for branch in module.get_branches(reverse=True):
+        for branch in module.get_branches():
             categs = branch.category_set.order_by('name', 'release__name')
             if len(categs):
                 for cat in categs:
diff --git a/stats/models.py b/stats/models.py
index d8c1d81..15ec3d6 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -23,8 +23,9 @@ import fnmatch
 import logging
 import os, sys, re
 import threading
-from time import sleep
 from datetime import datetime
+from functools import total_ordering
+from time import sleep
 from urllib2 import URLError
 
 from django.conf import settings
@@ -138,9 +139,7 @@ class Module(models.Model):
 
     def get_branches(self, reverse=False):
         """ Return module branches, in ascending order by default (descending order if reverse == True) """
-        branches = list(self.branch_set.all())
-        branches.sort(reverse=reverse)
-        return branches
+        return sorted(self.branch_set.all(), reverse=reverse)
 
     def get_head_branch(self):
         """ Returns the HEAD (trunk, master, ...) branch of the module """
@@ -182,6 +181,7 @@ class ModuleLock(object):
         return os.path.exists(self.dirpath)
 
 
+ total_ordering
 class Branch(models.Model):
     """ Branch of a module """
     name        = models.CharField(max_length=50)
@@ -245,12 +245,15 @@ class Branch(models.Model):
             shutil.rmtree(self.output_dir('ui'))
         super(Branch, self).delete()
 
-    def __cmp__(self, other):
+    def __eq__(self, other):
+        return (self.module.name, self.name) == (other.module.name, other.name)
+
+    def __lt__(self, other):
         if self.name in BRANCH_HEAD_NAMES:
-            return -1
+            return False
         elif other.name in BRANCH_HEAD_NAMES:
-            return 1
-        return cmp(self.weight, other.weight) or -cmp(self.name, other.name)
+            return True
+        return (self.weight, self.name) < (other.weight, other.name)
 
     @property
     def img_url_prefix(self):
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index e6a2cf3..df3cb30 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -175,10 +175,20 @@ class ModuleTestCase(TestCase):
         b1.save(update_statistics=False)
         b2 = Branch(name='p-branch', module=self.mod)
         b2.save(update_statistics=False)
-        self.assertEqual([b.name for b in sorted(self.mod.branch_set.all())], 
['master','p-branch','a-branch'])
-        b1.weight = -1
-        b1.save(update_statistics=False)
-        self.assertEqual([b.name for b in sorted(self.mod.branch_set.all())], 
['master','a-branch','p-branch'])
+        self.assertEqual(
+            [b.name for b in sorted(self.mod.branch_set.all())],
+            ['a-branch', 'p-branch', 'master']
+        )
+        b2.weight = -1
+        b2.save(update_statistics=False)
+        self.assertEqual(
+            [b.name for b in sorted(self.mod.branch_set.all())],
+            ['p-branch', 'a-branch', 'master']
+        )
+        self.assertEqual(
+            [b.name for b in self.mod.get_branches(reverse=True)],
+            ['master', 'a-branch', 'p-branch']
+        )
 
     @test_scratchdir
     def test_string_frozen_mail(self):
diff --git a/stats/views.py b/stats/views.py
index 1b658eb..181a25b 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -49,7 +49,7 @@ def modules(request, format='html'):
 
 def module(request, module_name):
     mod = get_object_or_404(Module, name=module_name)
-    branches = mod.get_branches()
+    branches = mod.get_branches(reverse=True)
     if request.user.is_authenticated():
         person = Person.get_by_user(request.user)
         langs = person.get_languages()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]