Plone技术资料

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 11331|回复: 0

plone.directives.form介绍(一、form实现)

[复制链接]
发表于 2015-1-4 20:01:46 | 显示全部楼层 |阅读模式
plone.directives.form介绍(一、form实现)
代码:
  1. import martian
  2. import five.grok

  3. import grokcore.view.interfaces
  4. import grokcore.view.util

  5. import zope.component
  6. import zope.interface

  7. import z3c.form.form
  8. import z3c.form.button

  9. import plone.autoform.form
  10. import plone.autoform.view

  11. import zope.i18nmessageid

  12. import zope.publisher.publish

  13. from Products.statusmessages.interfaces import IStatusMessage

  14. _ = zope.i18nmessageid.MessageFactory(u'plone.dexterity')

  15. # Form base classes

  16. class GrokkedForm(object):
  17.     """Mixin class for all grokked forms, which provides grok.View-like
  18.     semantics for template association, static resources, etc.
  19.    
  20.     Do not use directly.
  21.     """
  22.     martian.baseclass()
  23.    
  24.     # Emulate grokcore.view.View
  25.    
  26.     def __init__(self, context, request):
  27.         super(GrokkedForm, self).__init__(context, request)
  28.         
  29.         # Set the view __name__
  30.         self.__name__ = getattr(self, '__view_name__', None)
  31.         
  32.         # Set up the view.static resource directory reference
  33.         if getattr(self, 'module_info', None) is not None:
  34.             self.static = zope.component.queryAdapter(
  35.                 self.request,
  36.                 zope.interface.Interface,
  37.                 name=self.module_info.package_dotted_name
  38.                 )
  39.         else:
  40.             self.static = None
  41.    
  42.     def render(self):
  43.         # Render a grok-style templat if we have one
  44.         if (
  45.             getattr(self, 'template') and
  46.             grokcore.view.interfaces.ITemplate.providedBy(self.template)
  47.         ):
  48.             return self._render_template()
  49.         else:
  50.             return super(GrokkedForm, self).render()
  51.     render.base_method = True
  52.    
  53.     @property
  54.     def response(self):
  55.         return self.request.response
  56.    
  57.     def _render_template(self):
  58.         return self.template.render(self)

  59.     def default_namespace(self):
  60.         namespace = {}
  61.         namespace['context'] = self.context
  62.         namespace['request'] = self.request
  63.         namespace['static'] = self.static
  64.         namespace['view'] = self
  65.         return namespace

  66.     def namespace(self):
  67.         return {}
  68.    
  69.     def url(self, obj=None, name=None, data=None):
  70.         """Return string for the URL based on the obj and name. The data
  71.         argument is used to form a CGI query string.
  72.         """
  73.         if isinstance(obj, basestring):
  74.             if name is not None:
  75.                 raise TypeError(
  76.                     'url() takes either obj argument, obj, string arguments, '
  77.                     'or string argument')
  78.             name = obj
  79.             obj = None

  80.         if name is None and obj is None:
  81.             # create URL to view itself
  82.             obj = self
  83.         elif name is not None and obj is None:
  84.             # create URL to view on context
  85.             obj = self.context

  86.         if data is None:
  87.             data = {}
  88.         else:
  89.             if not isinstance(data, dict):
  90.                 raise TypeError('url() data argument must be a dict.')

  91.         return grokcore.view.util.url(self.request, obj, name, data=data)

  92.     def redirect(self, url):
  93.         return self.request.response.redirect(url)
  94.    
  95.     # BBB: makes the form have the most important properties that were
  96.     # exposed by the wrapper view
  97.    
  98.     @property
  99.     def form_instance(self):
  100.         return self
  101.    
  102.     @property
  103.     def form(self):
  104.         return self.__class__
  105.    
  106. # Page forms

  107. class Form(GrokkedForm, z3c.form.form.Form):
  108.     """A basic form.
  109.     """
  110.     martian.baseclass()

  111. class SchemaForm(plone.autoform.form.AutoExtensibleForm, Form):
  112.     """A basic extensible form
  113.     """
  114.     martian.baseclass()
  115.    
  116.     schema = None # Must be set by subclass

  117. # Add forms

  118. class AddForm(GrokkedForm, z3c.form.form.AddForm):
  119.     """A standard add form.
  120.     """
  121.     martian.baseclass()
  122.    
  123.     immediate_view = None
  124.    
  125.     def __init__(self, context, request):
  126.         super(AddForm, self).__init__(context, request)
  127.         self.request['disable_border'] = True
  128.    
  129.     def render(self):
  130.         if self._finishedAdd:
  131.             self.request.response.redirect(self.nextURL())
  132.             return ""
  133.         return super(AddForm, self).render()
  134.    
  135.     def nextURL(self):
  136.         if self.immediate_view is not None:
  137.             return self.immediate_view
  138.         else:
  139.             return self.context.absolute_url()
  140.    
  141.     # Buttons
  142.    
  143.     @z3c.form.button.buttonAndHandler(_('Save'), name='save')
  144.     def handleAdd(self, action):
  145.         data, errors = self.extractData()
  146.         if errors:
  147.             self.status = self.formErrorsMessage
  148.             return
  149.         obj = self.createAndAdd(data)
  150.         if obj is not None:
  151.             # mark only as finished if we get the new object
  152.             self._finishedAdd = True
  153.             IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info")
  154.    
  155.     @z3c.form.button.buttonAndHandler(_(u'Cancel'), name='cancel')
  156.     def handleCancel(self, action):
  157.         IStatusMessage(self.request).addStatusMessage(_(u"Add New Item operation cancelled"), "info")
  158.         self.request.response.redirect(self.nextURL())

  159.     def updateActions(self):
  160.         super(AddForm, self).updateActions()
  161.         self.actions["save"].addClass("context")
  162.         self.actions["cancel"].addClass("standalone")

  163. class SchemaAddForm(plone.autoform.form.AutoExtensibleForm, AddForm):
  164.     """An extensible add form.
  165.     """
  166.     martian.baseclass()
  167.    
  168.     schema = None # Must be set by subclass

  169. # Edit forms

  170. class EditForm(GrokkedForm, z3c.form.form.EditForm):
  171.     """A standard edit form
  172.     """
  173.     martian.baseclass()
  174.    
  175.     @z3c.form.button.buttonAndHandler(_(u'Save'), name='save')
  176.     def handleApply(self, action):
  177.         data, errors = self.extractData()
  178.         if errors:
  179.             self.status = self.formErrorsMessage
  180.             return
  181.         self.applyChanges(data)
  182.         IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info")
  183.         self.request.response.redirect(self.context.absolute_url())
  184.    
  185.     @z3c.form.button.buttonAndHandler(_(u'Cancel'), name='cancel')
  186.     def handleCancel(self, action):
  187.         IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info")
  188.         self.request.response.redirect(self.context.absolute_url())
  189.    
  190.     def updateActions(self):
  191.         super(EditForm, self).updateActions()
  192.         self.actions["save"].addClass("context")
  193.         self.actions["cancel"].addClass("standalone")

  194. class SchemaEditForm(plone.autoform.form.AutoExtensibleForm, EditForm):
  195.     """An extensible edit form
  196.     """
  197.     martian.baseclass()
  198.    
  199.     schema = None # Must be set by subclass

  200. # Display forms

  201. class DisplayForm(plone.autoform.view.WidgetsView, five.grok.View):
  202.     """A view that knows about field widgets, but otherwise has all the
  203.     goodness of a grok.View, including automatic templates.
  204.     """
  205.     martian.baseclass()
  206.    
  207.     def __init__(self, context, request):
  208.         plone.autoform.view.WidgetsView.__init__(self, context, request)
  209.         five.grok.View.__init__(self, context, request)   
  210.    
  211.     def render(self):
  212.         template = getattr(self, 'template', None)
  213.         if template is not None:
  214.             return self.template.render(self)
  215.         return super(DisplayForm, self).render()
  216.     render.base_method = False

  217. # Directives

  218. class wrap(martian.Directive):
  219.     """Directive used on a form class to determine if a form wrapper view
  220.     should be used.
  221.     """
  222.     scope = martian.CLASS
  223.     store = martian.ONCE
  224.    
  225.     def factory(self, flag=True):
  226.         return flag

  227. __all__ = ('Form', 'SchemaForm', 'AddForm', 'SchemaAddForm',
  228.             'EditForm', 'SchemaEditForm', 'DisplayForm', 'wrap',)
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|Plone技术资料 ( 湘ICP备14006519号-1 )

GMT+8, 2019-11-23 04:38 , Processed in 0.045034 second(s), 15 queries , Gzip On.

Powered by Plone! X3.4

© 2001-2019 Plone.org.

快速回复 返回顶部 返回列表