app/soc/views/helpers/custom_widgets.py
changeset 271 01e90bb21b7e
parent 270 7dd6d8347b56
child 272 00cea07656c0
equal deleted inserted replaced
270:7dd6d8347b56 271:01e90bb21b7e
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Custom widgets used for form fields.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 import copy
       
    26 
       
    27 from django import newforms as forms
       
    28 from django.newforms import util
       
    29 from django.newforms import widgets
       
    30 from django.utils import html
       
    31 from django.utils import simplejson
       
    32 from django.utils import safestring
       
    33 
       
    34 
       
    35 class ReadOnlyInput(forms.widgets.Input):
       
    36   """Read only input widget.
       
    37   """
       
    38   input_type = 'text'
       
    39 
       
    40   def render(self, name, value, attrs=None):
       
    41     """Render ReadOnlyInput widget as HTML.
       
    42     """
       
    43     attrs['readonly'] = 'readonly'
       
    44     return super(ReadOnlyInput, self).render(name, value, attrs)
       
    45 
       
    46 
       
    47 class TinyMCE(forms.widgets.Textarea):
       
    48   """TinyMCE widget. 
       
    49   
       
    50   Requires to include tiny_mce_src.js in your template. Widget can be
       
    51   customized by overwriting or adding extra options to mce_settings
       
    52   dictionary
       
    53 
       
    54   You can set TinyMCE widget for particular form field using code below:
       
    55     class ExampleForm(forms_helpers.DbModelForm):
       
    56       content = forms.fields.CharField(widget=custom_widgets.TinyMCE())
       
    57   
       
    58   You can include tiny_mce_src.js in your template using:
       
    59     {% block scripts %}
       
    60   	  <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script>
       
    61     {% endblock %}
       
    62   """ 
       
    63   DEF_MCE_SETTINGS = { 'mode': "exact",
       
    64                        'theme': "simple",
       
    65                        'theme_advanced_toolbar_location': "top",
       
    66                        'theme_advanced_toolbar_align': "center"}
       
    67 
       
    68   mce_settings = DEF_MCE_SETTINGS.copy()
       
    69 
       
    70   TINY_MCE_HTML_FMT = u'''\
       
    71 <textarea %(attrs)s>%(value)s</textarea>
       
    72 <script type="text/javascript">
       
    73 tinyMCE.init(%(settings_json)s)
       
    74 </script>'''
       
    75   
       
    76   def render(self, name, value, attrs=None):
       
    77     """Render TinyMCE widget as HTML.
       
    78     """
       
    79     if value is None:
       
    80       value = ''
       
    81     value = util.smart_unicode(value)
       
    82     final_attrs = self.build_attrs(attrs, name=name)
       
    83     
       
    84     self.mce_settings['elements'] = "id_%s" % name
       
    85       
       
    86     # convert mce_settings from dict to JSON
       
    87     mce_json = simplejson.JSONEncoder().encode(self.mce_settings)
       
    88 
       
    89     return safestring.mark_safe(self.TINY_MCE_HTML_FMT % 
       
    90         {'attrs': widgets.flatatt(final_attrs),
       
    91          'value': html.escape(value), 
       
    92          'settings_json':  mce_json})