Commits

Luke Plant  committed 4ccdcdd

Added ordering by category, with tests.

  • Participants
  • Parent commits 9e9b710

Comments (0)

Files changed (3)

File semanticeditor/fixtures/test_classes.json

     "pk": 1, 
     "model": "semanticeditor.cssclass", 
     "fields": {
+      "category": 2, 
+      "templates": "cms_harness/example.html", 
+      "name": "greenborder", 
+      "verbose_name": "Green border", 
+      "column_equiv": null, 
+      "allowed_elements": "h1 h2 h3 h4 h5 h6 p blockquote ul ol li newrow newcol", 
+      "description": ""
+    }
+  },
+  {
+    "pk": 2, 
+    "model": "semanticeditor.cssclass", 
+    "fields": {
       "category": null, 
       "templates": "cms_harness/example.html", 
       "name": "funky", 
     }
   }, 
   {
-    "pk": 2, 
+    "pk": 3, 
     "model": "semanticeditor.cssclass", 
     "fields": {
       "category": 2, 
       "templates": "cms_harness/example.html", 
-      "name": "redborder", 
-      "verbose_name": "Red border", 
+      "name": "blueborder", 
+      "verbose_name": "Blue border", 
       "column_equiv": null, 
       "allowed_elements": "h1 h2 h3 h4 h5 h6 p blockquote ul ol li newrow newcol", 
       "description": ""

File semanticeditor/models.py

 
 
 def get_classes(template):
-    classes = CssClass.objects.all().order_by('verbose_name')
-    # Can't do filter in DB easily, because 'templates' is actually
-    # a comma separated list in DB.
+    # Can't do filter in DB easily, because 'templates' is actually a comma
+    # separated list in DB.
+    classes = CssClass.objects.all().order_by('category__name', 'verbose_name')
     return filter(lambda c: template in c.templates, classes)

File semanticeditor/tests.py

         classes = CssClass.objects.all()
         classes_2 = get_classes('cms_harness/example.html')
         self.assertEqual(len(classes), len(classes_2))
+
+    def test_retrieve_styles_order(self):
+        classes = get_classes('cms_harness/example.html')
+        # Should be ordered by category, alphabetically, with classes with no
+        # category at the beginning.
+        categories = [c.category.name if c.category is not None else '' for c in classes]
+        self.assertEqual(categories, sorted(categories))
+
+        # Should be ordered alphabetically within categories
+        c2 = [c for c in classes if c.category is not None and c.category.name == 'Borders']
+        assert len(c2) > 0 # Sanity
+        self.assertEqual(c2, list(CssClass.objects.filter(category__name='Borders').order_by('verbose_name')))