MarkupField

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# for suggestions please fork & improve @ http://gist.github.com/67724

from django.db import models
from django.conf import settings
from django.utils.html import linebreaks, urlize
from django.utils.safestring import mark_safe

PLAIN_TEXT, HTML, MARKDOWN, REST, TEXTILE = range(5)
MARKUP_TYPES = (
    (PLAIN_TEXT, 'plain'),
    (HTML, 'html'),
)

try:
    import markdown
    MARKUP_TYPES += ( (MARKDOWN, 'markdown'), )
except ImportError:
    pass

try:
    from docutils.core import publish_parts
    MARKUP_TYPES += ( (REST, 'restructuredtext'), )
except ImportError:
    pass

try:
    import textile
    MARKUP_TYPES += ( (TEXTILE, 'textile'), )
except ImportError:
    pass

_rendered_field_name = lambda name: '%s_rendered' % name
_markup_type_field_name = lambda name: '%s_markup_type' % name

class MarkupField(models.TextField):
    def __init__(self, *args, **kwargs):
        super(MarkupField, self).__init__(*args, **kwargs)

    def contribute_to_class(self, cls, name):
        markup_type_field = models.PositiveIntegerField(choices=MARKUP_TYPES, default=PLAIN_TEXT)
        rendered_field = models.TextField(editable=False)
        cls.add_to_class(_markup_type_field_name(name), markup_type_field)
        cls.add_to_class(_rendered_field_name(name), rendered_field)
        super(MarkupField, self).contribute_to_class(cls, name)

        def as_html(self):
            return mark_safe(getattr(self, _rendered_field_name(name)))
        cls.add_to_class('%s_as_html' % name, property(as_html))

    def pre_save(self, model_instance, add):
        markup_type = getattr(model_instance, _markup_type_field_name(self.attname))
        markup = getattr(model_instance, self.attname)

        if markup_type == MARKDOWN:
            rendered = markdown.markdown(markup)
        elif markup_type == REST:
            docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
            parts = publish_parts(source=markup, writer_name="html4css1", settings_overrides=docutils_settings)
            rendered = parts["fragment"]
        elif markup_type == TEXTILE:
            rendered = textile.textile(markup, encoding='utf-8', output='utf-8')
        elif markup_type == PLAIN_TEXT:
            rendered = urlize(linebreaks(markup))
        else:
            rendered = markup

        setattr(model_instance, _rendered_field_name(self.attname), rendered)

        return markup

More like this

  1. MarkupTextField by myles 5 years, 5 months ago
  2. Markup Selection in Admin by jonathan 6 years, 8 months ago
  3. Generic markup converter by ubernostrum 7 years, 1 month ago
  4. render_markup filter, specify the markup filter as a string by exogen 7 years ago
  5. HttpMethodsMiddleware by hawkeye 7 years ago

Comments

simonluijk (on February 21, 2009):

Very nice snippet, thank you. I will be using it for sure. One thing I noticed. Is the below required? I think it can just be taken out.

def __init__(self, *args, **kwargs):
    super(MarkupField, self).__init__(*args, **kwargs)

#

pjanderson (on February 23, 2009):

I wasn't able to successfully run syncdb command (when using django-trunk) when I have defined more than one MarkupField in my models.

#

(Forgotten your password?)