src/source/howto-paragraph.rst
branchdoc-dc
changeset 77 ae0e104800aa
parent 76 ca63b1a69cbb
child 78 88a65b19d07d
equal deleted inserted replaced
76:ca63b1a69cbb 77:ae0e104800aa
    31                        description=_("Use 'browse' button to select contact picture"),
    31                        description=_("Use 'browse' button to select contact picture"),
    32                        required=False)
    32                        required=False)
    33 
    33 
    34     phone = TextLine(title=_("Phone Number"),
    34     phone = TextLine(title=_("Phone Number"),
    35                      description=_("Name of the contact", required=False))
    35                      description=_("Name of the contact", required=False))
    36 
       
    37     address = Text(title=_("Address"), required=False)
       
    38 
       
    39     contact_email = MailAddressField(title=_("Email address"),
       
    40                                      description=_("Contact email address"),
       
    41                                      required=False)
       
    42 
    36 
    43     renderer = Choice(title=_("Contact template"),
    37     renderer = Choice(title=_("Contact template"),
    44                       description=_("Presentation template used for this contact"),
    38                       description=_("Presentation template used for this contact"),
    45                       vocabulary=CONTACT_PHONE_PARAGRAPH_RENDERERS,
    39                       vocabulary=CONTACT_PHONE_PARAGRAPH_RENDERERS,
    46                       default='default')
    40                       default='default')
    62 
    56 
    63 
    57 
    64         icon_class = 'fa-phone'
    58         icon_class = 'fa-phone'
    65         icon_hint = _("Phone number card")
    59         icon_hint = _("Phone number card")
    66 
    60 
    67         address = FieldProperty(IContactParagraph['address'])
    61         name = FieldProperty(IContactPhoneParagraph['name'])
    68         contact_email = FieldProperty(IContactParagraph['contact_email'])
    62         _photo = FileProperty(IContactPhoneParagraph['photo'])
       
    63 
    69         renderer = FieldProperty(IContactParagraph['renderer'])
    64         renderer = FieldProperty(IContactParagraph['renderer'])
    70 
    65 
    71         @property
    66         @property
    72         def photo(self):
    67         def photo(self):
    73             return self._photo
    68             return self._photo
    77             self._photo = value
    72             self._photo = value
    78             if IImage.providedBy(self._photo):
    73             if IImage.providedBy(self._photo):
    79                 alsoProvides(self._photo, IResponsiveImage)
    74                 alsoProvides(self._photo, IResponsiveImage)
    80 
    75 
    81 
    76 
    82 3) renderer Vocabulary
    77 3) renderers Vocabulary
    83 ----------------------
    78 ----------------------
    84 
    79 
    85 The list of rendered available for a paragraph is build automatically and is based on adapters that provide this interface
    80 The list of rendered available for a paragraph is build automatically and is based on adapters that provide this interface
    86 
    81 
    87 .. code-block:: python
    82 .. code-block:: python
    88 
    83 
    89     @vocabulary_config(name=CONTACT_PARAGRAPH_RENDERERS)
    84     @vocabulary_config(name=CONTACT_PARAGRAPH_RENDERERS)
    90     class ContactParagraphRendererVocabulary(RenderersVocabulary):
    85     class ContactParagraphRendererVocabulary(RenderersVocabulary):
    91         """Contact paragraph renderers vocabulary"""
    86         """Contact Phone paragraph renderers vocabulary"""
    92 
    87 
    93         content_interface = IContactParagraph
    88         content_interface = IContactPhoneParagraph
    94 
    89 
    95 
    90 
    96 .. seealso::
    91 .. seealso::
    97 
    92 
    98     :ref:`rendererhowto`
    93     :ref:`rendererhowto`
   101 How integrate a paragraph in the ZMI?
    96 How integrate a paragraph in the ZMI?
   102 =====================================
    97 =====================================
   103 
    98 
   104 To display and manage the new paragraph in the ZMI, you should create this associated forms
    99 To display and manage the new paragraph in the ZMI, you should create this associated forms
   105 
   100 
   106 1) paragraph factory
   101 1) Paragraph factory
   107 --------------------
   102 --------------------
   108 
   103 
   109 To create a new element inside the zodb, we need a container to this element. PyAMS provide
   104 To create a new element inside the zodb, we need a container to this element. PyAMS provide
   110 `IParagraphContainerTarget`, it's the default container for all paragraphs. We could use this container interface
   105 `IParagraphContainerTarget`, it's the default container for all paragraphs. We could use this container interface
   111 as context to create a new paragraph.
   106 as context to create a new paragraph.
   114 
   109 
   115 - *HTML*
   110 - *HTML*
   116 
   111 
   117 .. code-block:: python
   112 .. code-block:: python
   118 
   113 
   119     @pagelet_config(name='add-contact-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
   114     @pagelet_config(name='add-contact-phone-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
   120                     permission=MANAGE_CONTENT_PERMISSION)
   115                     permission=MANAGE_CONTENT_PERMISSION)
   121     class ContactParagraphAddForm(AdminDialogAddForm):
   116     class ContactPhoneParagraphAddForm(AdminDialogAddForm):
   122         """Contact paragraph add form"""
   117         """Contact phone paragraph add form"""
   123 
   118 
   124         legend = _("Add new contact card")
   119         legend = _("Add new contact phone card")
   125         dialog_class = 'modal-large'
   120         dialog_class = 'modal-large'
   126         icon_css_class = 'fa fa-fw fa-id-card-o'
   121         icon_css_class = 'fa fa-fw fa-phone'
   127 
   122 
   128         fields = field.Fields(IContactParagraph).omit('__parent__', '__name__', 'visible')
   123         fields = field.Fields(IContactPhoneParagraph).omit('__parent__', '__name__', 'visible')
   129         ajax_handler = 'add-contact-paragraph.json'
   124         ajax_handler = 'add-contact-phone-paragraph.json' #(*)
   130         edit_permission = MANAGE_CONTENT_PERMISSION
   125         edit_permission = MANAGE_CONTENT_PERMISSION
   131 
   126 
   132         def updateWidgets(self, prefix=None):
   127          def create(self, data):
   133             super(ContactParagraphAddForm, self).updateWidgets(prefix)
   128             return ContactPhoneParagraph()
   134             if 'address' in self.widgets:
       
   135                 self.widgets['address'].widget_css_class = 'textarea'
       
   136 
       
   137         def create(self, data):
       
   138             return ContactParagraph()
       
   139 
   129 
   140         def add(self, object):
   130         def add(self, object):
   141             IParagraphContainer(self.context).append(object)
   131             IParagraphContainer(self.context).append(object)
   142 
   132 
   143 
   133 
   144 - *JSON*
   134 - *JSON*
   145 
   135 
   146 .. code-block:: python
   136 .. code-block:: python
   147 
   137 
   148     @view_config(name='add-contact-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
   138     #(*)
       
   139     @view_config(name='add-contact-phone-1paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
   149                  permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
   140                  permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
   150     class ContactParagraphAJAXAddForm(BaseParagraphAJAXAddForm, ContactParagraphAddForm):
   141     class ContactPhoneParagraphAJAXAddForm(BaseParagraphAJAXAddForm, ContactPhoneParagraphAddForm):
   151         """Contact paragraph add form, JSON renderer"""
   142         """Contact phone paragraph add form, JSON renderer"""
   152 
   143 
   153 
   144 
   154 2) Append the paragraph addform button in the menu
   145 2) Append the paragraph addform button in the menu
   155 --------------------------------------------------
   146 --------------------------------------------------
   156 
   147 
   157 We have created a new form and we want add a quick access button to create a new paragraph
   148 We have created a new form and we want add a quick access button to create a new paragraph
   158 
   149 
   159 .. code-block:: python
   150 .. code-block:: python
   160 
   151 
   161     @viewlet_config(name='add-contact-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
   152     @viewlet_config(name='add-contact-phone-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
   162                     layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=600)
   153                     layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=600)
   163     class ContactParagraphAddMenu(BaseParagraphAddMenu):
   154     class ContactPhoneParagraphAddMenu(BaseParagraphAddMenu):
   164         """Contact paragraph add menu"""
   155         """Contact paragraph add menu"""
   165 
   156 
   166         label = _("Contact card...")
   157         label = _("Contact card...")
   167         label_css_class = 'fa fa-fw fa-id-card-o'
   158         label_css_class = 'fa fa-fw fa-id-card-o'
   168         url = 'add-contact-paragraph.html'
   159         url = 'add-contact-paragraph.html'
   169         paragraph_type = CONTACT_PARAGRAPH_TYPE
   160         paragraph_type = CONTACT_PARAGRAPH_TYPE
   170 
   161 
   171 
   162 
   172 3)Edit form
   163 3) Create Edit form
   173 -----------
   164 -------------------
   174 
   165 
   175 - *HTML*
   166 - *HTML*
   176 
   167 
   177 .. code-block:: python
   168 .. code-block:: python
   178 
   169 
   179     @pagelet_config(name='properties.html', context=IContactParagraph, layer=IPyAMSLayer,
   170     @pagelet_config(name='properties.html', context=IContactParagraph, layer=IPyAMSLayer,
   180                     permission=MANAGE_CONTENT_PERMISSION)
   171                     permission=MANAGE_CONTENT_PERMISSION)
   181     class ContactParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
   172     class ContactPhoneParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
   182         """Contact paragraph properties edit form"""
   173         """Contact phone paragraph properties edit form"""
   183 
   174 
   184         prefix = 'contact_properties.'
   175         prefix = 'contact_properties.'
   185 
   176 
   186         legend = _("Edit contact card properties")
   177         legend = _("Edit contact card properties")
   187         icon_css_class = 'fa fa-fw fa-id-card-o'
   178         icon_css_class = 'fa fa-fw fa-id-card-o'
   190         fields['renderer'].widgetFactory = RendererFieldWidget
   181         fields['renderer'].widgetFactory = RendererFieldWidget
   191 
   182 
   192         ajax_handler = 'properties.json'
   183         ajax_handler = 'properties.json'
   193         edit_permission = MANAGE_CONTENT_PERMISSION
   184         edit_permission = MANAGE_CONTENT_PERMISSION
   194 
   185 
   195         def updateWidgets(self, prefix=None):
       
   196             super(ContactParagraphPropertiesEditForm, self).updateWidgets(prefix)
       
   197             if 'address' in self.widgets:
       
   198                 self.widgets['address'].widget_css_class = 'textarea'
       
   199 
   186 
   200 - *JSON*
   187 - *JSON*
   201 
   188 
   202 .. code-block:: python
   189 .. code-block:: python
   203 
   190 
   204     @view_config(name='properties.json', context=IContactParagraph, request_type=IPyAMSLayer,
   191     @view_config(name='properties.json', context=IContactPhoneParagraph, request_type=IPyAMSLayer,
   205                  permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
   192                  permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
   206     class ContactParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, ContactParagraphPropertiesEditForm):
   193     class ContactPhoneParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, ContactParagraphPropertiesEditForm):
   207         """Contact paragraph properties edit form, JSON renderer"""
   194         """Contact phone paragraph properties edit form, JSON renderer"""
   208 
   195 
   209 
   196 
   210     @adapter_config(context=(IContactParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
   197 4) Create ZMI Edit form inner tab
       
   198 ---------------------------------
       
   199 
       
   200 .. code-block:: python
       
   201     @adapter_config(context=(IContactPhoneParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
   211     @implementer(IInnerForm)
   202     @implementer(IInnerForm)
   212     class ContactParagraphInnerEditForm(ContactParagraphPropertiesEditForm):
   203     class ContactPhoneParagraphInnerEditForm(ContactPhoneParagraphPropertiesEditForm):
   213         """Contact paragraph inner edit form"""
   204         """Contact paragraph inner edit form"""
   214 
   205 
   215         legend = None
   206         legend = None
   216         ajax_handler = 'inner-properties.json'
   207         ajax_handler = 'inner-properties.json'
   217 
   208 
   221                 return button.Buttons(IParagraphEditFormButtons)
   212                 return button.Buttons(IParagraphEditFormButtons)
   222             else:
   213             else:
   223                 return button.Buttons()
   214                 return button.Buttons()
   224 
   215 
   225 
   216 
   226     @view_config(name='inner-properties.json', context=IContactParagraph, request_type=IPyAMSLayer,
   217 
       
   218 .. code-block:: python
       
   219 
       
   220     @view_config(name='inner-properties.json', context=IContactPhoneParagraph, request_type=IPyAMSLayer,
   227                  permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
   221                  permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
   228     class ContactParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, ContactParagraphInnerEditForm):
   222     class ContactPhoneParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, ContactParagraphInnerEditForm):
   229         """Contact paragraph inner edit form, JSON renderer"""
   223         """Contact paragraph inner edit form, JSON renderer"""
   230 
   224 
   231         def get_ajax_output(self, changes):
   225         def get_ajax_output(self, changes):
   232             output = super(ContactParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
   226             output = super(ContactParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
   233             updated = changes.get(IBaseParagraph, ())
   227             updated = changes.get(IBaseParagraph, ())