src/ztfy/baseskin/interfaces/form.py
changeset 0 747fc65e13e2
equal deleted inserted replaced
-1:000000000000 0:747fc65e13e2
       
     1 #
       
     2 # Copyright (c) 2008-2014 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = "restructuredtext"
       
    14 
       
    15 # import standard packages
       
    16 
       
    17 # import Zope3 interfaces
       
    18 from z3c.form.interfaces import INPUT_MODE, IWidget, ISubForm, ISubmitWidget
       
    19 from zope.component.interfaces import IObjectEvent
       
    20 from zope.lifecycleevent.interfaces import IObjectCreatedEvent, IObjectModifiedEvent
       
    21 from zope.viewlet.interfaces import IViewletManager, IViewlet
       
    22 
       
    23 # import local interfaces
       
    24 
       
    25 # import Zope3 packages
       
    26 from zope.interface import Interface, Attribute
       
    27 from zope.schema import Bool, TextLine, Choice, List, Dict, Object
       
    28 
       
    29 # import local packages
       
    30 
       
    31 from ztfy.baseskin import _
       
    32 
       
    33 
       
    34 #
       
    35 # Custom widgets interfaces
       
    36 #
       
    37 
       
    38 class IResetWidget(ISubmitWidget):
       
    39     """Reset button widget interface"""
       
    40 
       
    41 
       
    42 class ICloseWidget(ISubmitWidget):
       
    43     """Close button widget interface"""
       
    44 
       
    45 
       
    46 #
       
    47 # Custom forms interfaces
       
    48 #
       
    49 
       
    50 def checkSubmitButton(form):
       
    51     """Check form and widgets mode before displaying submit button"""
       
    52     if form.mode != INPUT_MODE:
       
    53         return False
       
    54     for widget in form.widgets.values():
       
    55         if widget.mode == INPUT_MODE:
       
    56             return True
       
    57     if IForm.providedBy(form):
       
    58         for subform in form.subforms:
       
    59             for widget in subform.widgets.values():
       
    60                 if widget.mode == INPUT_MODE:
       
    61                     return True
       
    62 
       
    63 
       
    64 class IWidgetsGroup(Interface):
       
    65     """Form widgets group interface"""
       
    66 
       
    67     id = TextLine(title=_("Group ID"),
       
    68                   required=False)
       
    69 
       
    70     css_class = TextLine(title=_("CSS class"),
       
    71                          required=False)
       
    72 
       
    73     legend = TextLine(title=_("Group legend"),
       
    74                       required=False)
       
    75 
       
    76     help = TextLine(title=_("Group help"),
       
    77                     required=False)
       
    78 
       
    79     widgets = List(title=_("Group's widgets list"),
       
    80                    value_type=Object(schema=IWidget))
       
    81 
       
    82     switch = Bool(title=_("Switchable group?"),
       
    83                   required=True,
       
    84                   default=False)
       
    85 
       
    86     checkbox_switch = Bool(title=_("Group switched via checkbox?"),
       
    87                            required=True,
       
    88                            default=False)
       
    89 
       
    90     checkbox_field = TextLine(title=_("Field name matching switch checkbox?"),
       
    91                               required=False)
       
    92 
       
    93     checkbox_widget = Object(schema=IWidget,
       
    94                              required=False)
       
    95 
       
    96     checkbox_on = Attribute(_("Checkbox on?"))
       
    97 
       
    98     hide_if_empty = Bool(title=_("Hide group if empty?"),
       
    99                          description=_("""If 'Yes', a switchable group containing only """
       
   100                                        """widgets with default values is hidden"""),
       
   101                          required=True,
       
   102                          default=False)
       
   103 
       
   104     visible = Attribute(_("Visible group?"))
       
   105 
       
   106     switchable = Attribute(_("Switchable group?"))
       
   107 
       
   108 
       
   109 class IBaseForm(Interface):
       
   110     """Marker interface for any form"""
       
   111 
       
   112 
       
   113 class IGroupsBasedForm(IBaseForm):
       
   114     """Groups based form"""
       
   115 
       
   116     groups = Attribute(_("Form groups"))
       
   117 
       
   118     def addGroup(self, group):
       
   119         """Add given group to form"""
       
   120 
       
   121 
       
   122 class IForm(IBaseForm):
       
   123     """Base form interface"""
       
   124 
       
   125     title = TextLine(title=_("Form title"))
       
   126 
       
   127     legend = TextLine(title=_("Form legend"),
       
   128                       required=False)
       
   129 
       
   130     subforms = List(title=_("Sub-forms"),
       
   131                     value_type=Object(schema=ISubForm),
       
   132                     required=False)
       
   133 
       
   134     subforms_legend = TextLine(title=_("Subforms legend"),
       
   135                                required=False)
       
   136 
       
   137     tabforms = List(title=_("Tab-forms"),
       
   138                     value_type=Object(schema=ISubForm),
       
   139                     required=False)
       
   140 
       
   141     autocomplete = Choice(title=_("Auto-complete"),
       
   142                           values=('on', 'off'),
       
   143                           default='on')
       
   144 
       
   145     label_css_class = TextLine(title=_("Labels CSS class"),
       
   146                                required=False,
       
   147                                default=u'control-label col-md-3')
       
   148 
       
   149     input_css_class = TextLine(title=_("Inputs CSS class"),
       
   150                                required=False,
       
   151                                default=u'col-md-9')
       
   152 
       
   153     display_hints_on_widgets = Bool(title=_("Display hints on input widgets?"),
       
   154                                     required=True,
       
   155                                     default=False)
       
   156 
       
   157     handle_upload = Bool(title=_("Handle uploads in form?"),
       
   158                          description=_("Set to true when form handle uploads to get progress bar"),
       
   159                          required=True,
       
   160                          default=False)
       
   161 
       
   162     callbacks = Dict(title=_("Widgets validation callbacks"),
       
   163                      key_type=TextLine(),
       
   164                      value_type=TextLine(),
       
   165                      required=False)
       
   166 
       
   167     def isDialog(self):
       
   168         """Check to know if current form is in a modal dialog"""
       
   169 
       
   170     def getForms(self):
       
   171         """Get full list of main form and subforms"""
       
   172 
       
   173     def createSubForms(self):
       
   174         """Initialize sub-forms"""
       
   175 
       
   176     def createTabForms(self):
       
   177         """Initialize tab-forms"""
       
   178 
       
   179     def getWidgetCallback(self, widget):
       
   180         """Get submit callback associated with a given widget"""
       
   181 
       
   182     def updateContent(self, object, data):
       
   183         """Update given object with form data"""
       
   184 
       
   185     def getSubmitOutput(self, writer, changes):
       
   186         """Get submit output"""
       
   187 
       
   188 
       
   189 class IAJAXForm(IForm):
       
   190     """AJAX form interface"""
       
   191 
       
   192     handler = TextLine(title=_("Form AJAX handler"),
       
   193                        description=_("Relative URL of AJAX handler"),
       
   194                        required=False)
       
   195 
       
   196     data_type = Choice(title=_("Form AJAX data type"),
       
   197                        description=_(""),
       
   198                        required=False,
       
   199                        values=('json', 'jsonp', 'text', 'html', 'xml', 'script'))
       
   200 
       
   201     form_options = Dict(title=_("Form AJAX data options"),
       
   202                         required=False)
       
   203 
       
   204     callback = TextLine(title=_("Submit callback"),
       
   205                         description=_("Name of a custom form submit callback"),
       
   206                         required=False)
       
   207 
       
   208     def getFormOptions(self):
       
   209         """Get custom AJAX POST data"""
       
   210 
       
   211     def getAjaxErrors(self):
       
   212         """Get errors associated with their respective widgets in a JSON dictionary"""
       
   213 
       
   214 
       
   215 class IInnerSubForm(ISubForm):
       
   216     """Inner subform marker interface"""
       
   217 
       
   218 
       
   219 class IInnerTabForm(ISubForm):
       
   220     """Inner tabform marker interface"""
       
   221 
       
   222     tabLabel = TextLine(title=_("Tab label"),
       
   223                         required=True)
       
   224 
       
   225 
       
   226 class IViewletsBasedForm(IForm):
       
   227     """Viewlets based form interface"""
       
   228 
       
   229     managers = List(title=_("Names list of viewlets managers included in this form"),
       
   230                     value_type=TextLine(),
       
   231                     required=True)
       
   232 
       
   233 
       
   234 class ISubFormViewlet(IViewlet):
       
   235     """Sub-form viewlet interface"""
       
   236 
       
   237     legend = Attribute(_("Sub-form legend"))
       
   238 
       
   239     switchable = Attribute(_("Can the subform be hidden ?"))
       
   240 
       
   241     visible = Attribute(_("Is the subform initially visible ?"))
       
   242 
       
   243     callbacks = Dict(title=_("Widgets callbacks"),
       
   244                      key_type=TextLine(),
       
   245                      value_type=TextLine())
       
   246 
       
   247     def getWidgetCallback(self, widget):
       
   248         """Get submit callback associated with a given widget"""
       
   249 
       
   250 
       
   251 class ICustomExtractSubForm(ISubForm):
       
   252     """SubForm interface with custom extract method"""
       
   253 
       
   254     def extract(self):
       
   255         """Extract data and errors from input request"""
       
   256 
       
   257 
       
   258 class ICustomUpdateSubForm(ISubForm):
       
   259     """SubForm interface with custom update method"""
       
   260 
       
   261     def updateContent(self, object, data):
       
   262         """Update custom content with given data"""
       
   263 
       
   264 
       
   265 #
       
   266 # Default form content providers
       
   267 #
       
   268 
       
   269 class IFormViewletsManager(IViewletManager):
       
   270     """Base forms viewlets manager interface"""
       
   271 
       
   272 
       
   273 class IFormPrefixViewletsManager(IFormViewletsManager):
       
   274     """Form prefix viewlets manager interface"""
       
   275 
       
   276 
       
   277 class IWidgetsPrefixViewletsManager(IFormViewletsManager):
       
   278     """Form widgets prefix viewlets manager interface"""
       
   279 
       
   280 
       
   281 class IWidgetsSuffixViewletsManager(IFormViewletsManager):
       
   282     """Form widgets suffix viewlets manager interface"""
       
   283 
       
   284 
       
   285 class IFormSuffixViewletsManager(IFormViewletsManager):
       
   286     """Form suffix viewlets manager interface"""
       
   287 
       
   288 
       
   289 #
       
   290 # Custom events interfaces
       
   291 #
       
   292 
       
   293 class IViewObjectEvent(IObjectEvent):
       
   294     """View object event interface"""
       
   295 
       
   296     view = Attribute(_("View in which event was fired"))
       
   297 
       
   298 
       
   299 class IFormObjectCreatedEvent(IObjectCreatedEvent, IViewObjectEvent):
       
   300     """Object added event notify by form after final object creation"""
       
   301 
       
   302 
       
   303 class IFormObjectModifiedEvent(IObjectModifiedEvent, IViewObjectEvent):
       
   304     """Form object modified event interface"""