Commits

Luke Plant committed a2422cf

Factored out models.get_classes function from views.retrieve_styles for easier testing

Added basic test.

  • Participants
  • Parent commits bae0c64

Comments (0)

Files changed (5)

File semanticeditor/api.py

 # This module contains the 'public' API for parsing/formatting HTML,
 # used by views.py
 
+from semanticeditor.clean import clean_html
 from semanticeditor.definitions import AllUserErrors, COMMANDS, PresentationInfo, PresentationClass
 from semanticeditor.extract import extract_presentation, extract_structure
 from semanticeditor.format import format_html, preview_html
-from semanticeditor.clean import clean_html
-
+from semanticeditor.models import get_classes

File semanticeditor/fixtures/test_classes.json

+[
+  {
+    "pk": 2, 
+    "model": "semanticeditor.cssclasscategory", 
+    "fields": {
+      "name": "Borders"
+    }
+  }, 
+  {
+    "pk": 1, 
+    "model": "semanticeditor.cssclasscategory", 
+    "fields": {
+      "name": "Columns"
+    }
+  }, 
+  {
+    "pk": 1, 
+    "model": "semanticeditor.cssclass", 
+    "fields": {
+      "category": null, 
+      "templates": "cms_harness/example.html", 
+      "name": "funky", 
+      "verbose_name": "Funky", 
+      "column_equiv": null, 
+      "allowed_elements": "h1 h2 h3 h4 h5 h6 p blockquote ul ol li newrow newcol", 
+      "description": "Woot!"
+    }
+  }, 
+  {
+    "pk": 2, 
+    "model": "semanticeditor.cssclass", 
+    "fields": {
+      "category": 2, 
+      "templates": "cms_harness/example.html", 
+      "name": "redborder", 
+      "verbose_name": "Red border", 
+      "column_equiv": null, 
+      "allowed_elements": "h1 h2 h3 h4 h5 h6 p blockquote ul ol li newrow newcol", 
+      "description": ""
+    }
+  }
+]

File semanticeditor/models.py

         verbose_name_plural = "CSS classes"
         ordering = ('verbose_name',)
 
+
+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.
+    return filter(lambda c: template in c.templates, classes)

File semanticeditor/tests.py

 from django.test import TestCase
 from lxml import etree as ET
 
-from semanticeditor.api import extract_structure, PresentationInfo, format_html, extract_presentation, clean_html, preview_html
+from semanticeditor.api import extract_structure, PresentationInfo, format_html, extract_presentation, clean_html, preview_html, get_classes
 from semanticeditor.common import html_extract, parse, get_structure
 from semanticeditor.definitions import IncorrectHeadings, BadStructure, TooManyColumns, PresentationClass, NEWROW, NEWCOL, NEWINNERROW, NEWINNERCOL
+from semanticeditor.models import CssClass
 from semanticeditor.layout import LayoutDetails
 from semanticeditor.utils.etree import get_index, get_parent, eliminate_tag, indent
 
         # Some unicode
         self.assertEqualClean(u'<p>&nbsp;Frappé&nbsp;</p>',
                               u'<p> Frapp&#233; </p>')
+
+
+class TestRetrieveStyles(TestCase):
+    fixtures = ['test_classes.json']
+
+    def test_retrieve_styles_all(self):
+        classes = CssClass.objects.all()
+        classes_2 = get_classes('cms_harness/example.html')
+        self.assertEqual(len(classes), len(classes_2))

File semanticeditor/views.py

 from django.core.mail import mail_admins
 from django.conf import settings
 from django.utils.translation import ugettext as _
-from semanticeditor.api import extract_presentation, format_html, preview_html, AllUserErrors, COMMANDS, PresentationInfo, PresentationClass, clean_html
+from semanticeditor.api import extract_presentation, format_html, preview_html, AllUserErrors, COMMANDS, PresentationInfo, PresentationClass, clean_html, get_classes
 from semanticeditor.models import CssClass
 import sys
 try:
         # Need to look up page to find out what template to use
         p = Page.objects.get(pk=page_id)
         template = p.get_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.
-    classes = filter(lambda c: template in c.templates, classes)
+    classes = get_classes(template)
     retval = map(css_class_to_presentation_class,
                  classes)
     return success(map(PI_to_dict,retval))