#!/usr/bin/env python
from django.forms.widgets import Widget
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
import fckeditor
from util.cache import CacheContainer as Cache
class FCKEditor(Widget):
def __init__(self, attrs=None):
if attrs is not None:
self.attrs = attrs.copy()
else:
self.attrs = {}
self.__widget = fckeditor.FCKeditor("FCKeditor1")
self.__widget.sAgent = Cache.user_agent or ""
super(FCKEditor, self).__init__(attrs)
self.setAtters(self.attrs)
def setAtters(self, attrs):
if attrs is None:
return
if 'basepath' in attrs:
self.__widget.BasePath = attrs['basepath']
if 'width' in attrs:
self.__widget.Width = attrs['width']
if 'height' in attrs:
self.__widget.Height = attrs['height']
if 'toolbar' in attrs:
self.__widget.ToolbarSet = attrs['toolbar']
def render(self, name, value, attrs=None):
if value is None: value = ''
value = force_unicode(value)
self.__widget.InstanceName = name
self.__widget.Value = value
self.setAtters(attrs)
final_attrs = self.build_attrs(attrs, name=name)
return mark_safe(self.__widget.CreateHtml())
|