changeset 5280:24239f8ea946

db: cleanup of repo group choices - better handling of root and better sorting groups_choices always get a groups list so there is no reason have code for creating a list. All the responsibility of creating the tuples is moved to _generate_choice - also for the magic top level entries. Also sort the choices by all path elements - not just the top level element.
author Mads Kiilerich <madski@unity3d.com>
date Mon, 20 Jul 2015 15:11:42 +0200
parents 346a56c690ff
children 82faecc21324
files kallithea/model/db.py
diffstat 1 files changed, 14 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/model/db.py	Mon Jul 20 15:11:42 2015 +0200
+++ b/kallithea/model/db.py	Mon Jul 20 15:11:42 2015 +0200
@@ -1512,23 +1512,23 @@
 
     @classmethod
     def _generate_choice(cls, repo_group):
-        from webhelpers.html import literal as _literal
-        _name = lambda k: _literal(cls.SEP.join(k))
-        return repo_group.group_id, _name(repo_group.full_path_splitted)
+        """Return tuple with group_id as unicode string and name as html literal"""
+        from webhelpers.html import literal
+        if repo_group is None:
+            return (u'-1', u'-- %s --' % _('top level'))
+        return unicode(repo_group.group_id), literal(cls.SEP.join(repo_group.full_path_splitted))
 
     @classmethod
-    def groups_choices(cls, groups=None, show_empty_group=True):
-        if not groups:
-            groups = cls.query().all()
-
-        repo_groups = []
+    def groups_choices(cls, groups, show_empty_group=True):
+        """Return tuples with group_id as unicode string and name as html literal."""
+
         if show_empty_group:
-            repo_groups = [('-1', u'-- %s --' % _('Top level'))]
-
-        repo_groups.extend([cls._generate_choice(x) for x in groups])
-
-        repo_groups = sorted(repo_groups, key=lambda t: t[1].split(cls.SEP)[0])
-        return repo_groups
+            groups = list(groups)
+            groups.append(None)
+
+        choices = [cls._generate_choice(g) for g in groups]
+
+        return sorted(choices, key=lambda c: c[1].split(cls.SEP))
 
     @classmethod
     def url_sep(cls):