doc/book/devweb/edition/examples.rst
changeset 12879 7347715bf0ee
parent 10491 c67bcee93248
equal deleted inserted replaced
12878:ec05a333f02c 12879:7347715bf0ee
    54 Here is the source code:
    54 Here is the source code:
    55 
    55 
    56 .. sourcecode:: python
    56 .. sourcecode:: python
    57 
    57 
    58     def sender_value(form, field):
    58     def sender_value(form, field):
    59 	return '%s <%s>' % (form._cw.user.dc_title(), form._cw.user.get_email())
    59         return '%s <%s>' % (form._cw.user.dc_title(), form._cw.user.get_email())
    60 
    60 
    61     def recipient_choices(form, field):
    61     def recipient_choices(form, field):
    62 	return [(e.get_email(), e.eid)
    62         return [(e.get_email(), e.eid)
    63                  for e in form.cw_rset.entities()
    63                  for e in form.cw_rset.entities()
    64 		 if e.get_email()]
    64                  if e.get_email()]
    65 
    65 
    66     def recipient_value(form, field):
    66     def recipient_value(form, field):
    67 	return [e.eid for e in form.cw_rset.entities()
    67         return [e.eid for e in form.cw_rset.entities()
    68                 if e.get_email()]
    68                 if e.get_email()]
    69 
    69 
    70     class MassMailingForm(forms.FieldsForm):
    70     class MassMailingForm(forms.FieldsForm):
    71 	__regid__ = 'massmailing'
    71         __regid__ = 'massmailing'
    72 
    72 
    73 	needs_js = ('cubicweb.widgets.js',)
    73         needs_js = ('cubicweb.widgets.js',)
    74 	domid = 'sendmail'
    74         domid = 'sendmail'
    75 	action = 'sendmail'
    75         action = 'sendmail'
    76 
    76 
    77 	sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}),
    77         sender = ff.StringField(widget=TextInput({'disabled': 'disabled'}),
    78 				label=_('From:'),
    78                                 label=_('From:'),
    79 				value=sender_value)
    79                                 value=sender_value)
    80 
    80 
    81 	recipient = ff.StringField(widget=CheckBox(),
    81         recipient = ff.StringField(widget=CheckBox(),
    82 	                           label=_('Recipients:'),
    82                                    label=_('Recipients:'),
    83 				   choices=recipient_choices,
    83                                    choices=recipient_choices,
    84 				   value=recipients_value)
    84                                    value=recipients_value)
    85 
    85 
    86 	subject = ff.StringField(label=_('Subject:'), max_length=256)
    86         subject = ff.StringField(label=_('Subject:'), max_length=256)
    87 
    87 
    88 	mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
    88         mailbody = ff.StringField(widget=AjaxWidget(wdgtype='TemplateTextField',
    89 						    inputid='mailbody'))
    89                                                     inputid='mailbody'))
    90 
    90 
    91 	form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
    91         form_buttons = [ImgButton('sendbutton', "javascript: $('#sendmail').submit()",
    92 				  _('send email'), 'SEND_EMAIL_ICON'),
    92                                   _('send email'), 'SEND_EMAIL_ICON'),
    93 			ImgButton('cancelbutton', "javascript: history.back()",
    93                         ImgButton('cancelbutton', "javascript: history.back()",
    94 				  stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')]
    94                                   stdmsgs.BUTTON_CANCEL, 'CANCEL_EMAIL_ICON')]
    95 
    95 
    96 Let's detail what's going on up there. Our form will hold four fields:
    96 Let's detail what's going on up there. Our form will hold four fields:
    97 
    97 
    98 * a sender field, which is disabled and will simply contains the user's name and
    98 * a sender field, which is disabled and will simply contains the user's name and
    99   email
    99   email
   123 To see this form, we still have to wrap it in a view. This is pretty simple:
   123 To see this form, we still have to wrap it in a view. This is pretty simple:
   124 
   124 
   125 .. sourcecode:: python
   125 .. sourcecode:: python
   126 
   126 
   127     class MassMailingFormView(form.FormViewMixIn, EntityView):
   127     class MassMailingFormView(form.FormViewMixIn, EntityView):
   128 	__regid__ = 'massmailing'
   128         __regid__ = 'massmailing'
   129 	__select__ = is_instance(IEmailable) & authenticated_user()
   129         __select__ = is_instance(IEmailable) & authenticated_user()
   130 
   130 
   131 	def call(self):
   131         def call(self):
   132 	    form = self._cw.vreg['forms'].select('massmailing', self._cw,
   132             form = self._cw.vreg['forms'].select('massmailing', self._cw,
   133 	                                         rset=self.cw_rset)
   133                                                  rset=self.cw_rset)
   134 	    form.render(w=self.w)
   134             form.render(w=self.w)
   135 
   135 
   136 As you see, we simply define a view with proper selector so it only apply to a
   136 As you see, we simply define a view with proper selector so it only apply to a
   137 result set containing :class:`IEmailable` entities, and so that only users in the
   137 result set containing :class:`IEmailable` entities, and so that only users in the
   138 managers or users group can use it. Then in the `call()` method for this view we
   138 managers or users group can use it. Then in the `call()` method for this view we
   139 simply select the above form and call its `.render()` method with our output
   139 simply select the above form and call its `.render()` method with our output