Commits

Anonymous committed eb788c8

make MarkupField compatible with South FakeORM

Comments (0)

Files changed (4)

markitup/fields.py

             obj.__dict__[self.field.name] = value
 
 class MarkupField(models.TextField):
+    def __init__(self, *args, **kwargs):
+        # for South FakeORM compatibility: the frozen version of a
+        # MarkupField can't try to add a _rendered field, because the
+        # _rendered field itself is frozen as well. See introspection
+        # rules below.
+        self.add_rendered_field = not kwargs.pop('no_rendered_field', False)
+        super(MarkupField, self).__init__(*args, **kwargs)
+    
     def contribute_to_class(self, cls, name):
-        rendered_field = models.TextField(editable=False)
-        rendered_field.creation_counter = self.creation_counter+1
-        cls.add_to_class(_rendered_field_name(name), rendered_field)
+        if self.add_rendered_field:
+            rendered_field = models.TextField(editable=False)
+            rendered_field.creation_counter = self.creation_counter+1
+            cls.add_to_class(_rendered_field_name(name), rendered_field)
         super(MarkupField, self).contribute_to_class(cls, name)
         setattr(cls, self.name, MarkupDescriptor(self))
 
 # allow South to handle MarkupField smoothly
 try:
     from south.modelsinspector import add_introspection_rules
-    add_introspection_rules(patterns=['markitup\.fields\.'])
+    # For a normal MarkupField, the add_rendered_field attribute is
+    # always True, which means no_rendered_field arg will always be
+    # True in a frozen MarkupField, which is what we want.
+    add_introspection_rules(rules=[((MarkupField,),
+                                    [],
+                                    {'no_rendered_field': ('add_rendered_field',
+                                                           {})})],
+                            patterns=['markitup\.fields\.'])
 except ImportError:
     pass
 
 
     def __unicode__(self):
         return self.title
+
+class NoRendered(models.Model):
+    """
+    Test that the no_rendered_field keyword arg works. This arg should
+    never be used except by the South model-freezing.
+
+    """
+    body = MarkupField(no_rendered_field=True)

tests/test_settings.py

 INSTALLED_APPS = ('markitup', 'tests')
 DATABASE_ENGINE = 'sqlite3'
 
+try:
+    import south
+    INSTALLED_APPS += ('south',)
+except ImportError:
+    pass
+
 MEDIA_URL = '/media/'
 MEDIA_ROOT = join(dirname(MIU_TEST_ROOT), 'markitup', 'media')
 
 from __future__ import with_statement
 import re
 import warnings
+
 from django.template import Template, Context, get_library
 from django.test import TestCase, Client
 from django.conf import settings as django_settings
-from markitup import settings
-from markitup.widgets import MarkItUpWidget, MarkupTextarea, AdminMarkItUpWidget
 from django.templatetags.markitup_tags import _get_markitup_context
 from django.core import serializers
 from django.forms.models import modelform_factory
+from django.db.models.fields import FieldDoesNotExist
+
 from django.contrib import admin
+
+from markitup import settings
 from markitup.fields import MarkupField
+from markitup.widgets import MarkItUpWidget, MarkupTextarea, AdminMarkItUpWidget
 
 from models import Post
 
     def test_all_media(self):
         out = """<link href="%(prefix)s/markitup/skins/simple/style.css" type="text/css" media="screen" rel="stylesheet" />
 <link href="%(prefix)s/markitup/sets/default/style.css" type="text/css" media="screen" rel="stylesheet" />
-<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
 <script type="text/javascript" src="%(prefix)s/markitup/jquery.markitup.js"></script>
 <script type="text/javascript" src="%(prefix)s/markitup/sets/default/set.js"></script>""" % {'prefix': self.prefix}
         self.assertEquals(self._get_media(), out)
 class WidgetAlternateMediaUrlTests(AlternateMediaUrlTests,
                                    WidgetMediaUrlTests):
     pass
+
+if 'south' in django_settings.INSTALLED_APPS:
+    class SouthFreezingTests(TestCase):
+        def test_introspector_adds_no_rendered_field(self):
+            from south.modelsinspector import introspector
+            mf = Post._meta.get_field('body')
+            args, kwargs = introspector(mf)
+            self.assertEquals(kwargs['no_rendered_field'], 'True')
+        
+        def test_no_rendered_field_works(self):
+            from models import NoRendered
+            self.assertRaises(FieldDoesNotExist,
+                              NoRendered._meta.get_field,
+                              '_body_rendered')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.