dilrajsachdev
11/25/2013 - 9:41 AM

WYSIWYG field available.

WYSIWYG field available.

"""
Makes a WYSIWYG field available. This field is identical to the
TextField provided by django.db.models.fields.TextField except that
the class wysiwyg will be added to it's rendered textarea tag
"""
 
from django.db.models.fields import TextField
from django.forms.widgets import Textarea
from django.utils.translation  import ugettext_lazy as _
from django.conf import settings
 

class WYSIWYG(Textarea):
    """
    An extension to the text class that adds the class WYSIWYG 
    """
 
    def render(self, name, value, attrs=None):
        defaults = {'cols': '80'}
        if 'class' in attrs:
            attrs['class'] += " wysiwyg"
        else:
            attrs.update({'class': 'wysiwyg'})
        defaults.update(attrs)
        return super(WYSIWYG, self).render(name, value, defaults)
 
    class Media:
        js = (settings.MEDIA_URL_SECURE + 'js/tiny_mce/tiny_mce.js', 
              settings.MEDIA_URL_SECURE + 'js/wysiwyg_config.js',)
 

class WYSIWYGField(TextField):
    """
    A new Model Field type - it is based on the text input, but will
    automatically use a WYSIWYG editor (the WYSIWYG widget) for its
    default model form (eg. in the admin)
    """
    _description = _("WYSIWYG")
 
    def formfield(self, **kwargs):
        """
        Override the default widget with the WYSIWYG widget
        """
        kwargs.update({'widget': WYSIWYG})
        return super(WYSIWYGField, self).formfield(**kwargs)
 
"""
South integration for the WYSIWYGField
"""
try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], ["^wysiwyg\.wysiwyg\.WYSIWYGField"])
except ImportError:
    pass