def input_name(self, form, suffix=None): """Return the 'qualified name' for this field, e.g. something suitable to use as HTML input name. You can specify a suffix that will be included in the name when widget needs several inputs. """ # caching is necessary else we get some pb on entity creation : # entity.eid is modified from creation mark (eg 'X') to its actual eid # (eg 123), and then `field.input_name()` won't return the right key # anymore if not cached (first call to input_name done *before* eventual # eid affectation). # # note that you should NOT use @cached else it will create a memory leak # on persistent fields (eg created once for all on a form class) because # of the 'form' appobject argument: the cache will keep growing as new # form are created... try: return form.formvalues[(self, 'input_name', suffix)] except KeyError: name = self.role_name() if suffix is not None: name += suffix if self.eidparam: name = eid_param(name, form.edited_entity.eid) form.formvalues[(self, 'input_name', suffix)] = name return name
def _render_fields(self, fields, w, form): if form.parent_form is not None: entity = form.edited_entity values = form.form_previous_values qeid = eid_param('eid', entity.eid) cbsetstate = "setCheckboxesState('eid', %s, 'checked')" % \ xml_escape(json_dumps(entity.eid)) w(u'<tr class="%s">' % (entity.cw_row % 2 and u'even' or u'odd')) # XXX turn this into a widget used on the eid field w(u'<td>%s</td>' % checkbox('eid', entity.eid, checked=qeid in values)) for field in fields: error = form.field_error(field) if error: w(u'<td class="error">') self.render_error(w, error) else: w(u'<td>') if isinstance(field.widget, (fwdgs.Select, fwdgs.CheckBox, fwdgs.Radio)): field.widget.attrs['onchange'] = cbsetstate elif isinstance(field.widget, fwdgs.Input): field.widget.attrs['onkeypress'] = cbsetstate # XXX else w(u'<div>%s</div>' % field.render(form, self)) w(u'</td>\n') w(u'</tr>') else: self._main_display_fields = fields
def keep_entity(self, form, entity): if not entity.has_eid(): return True # are we regenerating form because of a validation error? if form.form_previous_values: cdvalues = self._cw.list_form_param( eid_param(self.rtype, self.peid), form.form_previous_values) if str(entity.eid) not in cdvalues: return False return True
def init_form(self, form, entity): """customize your form before rendering here""" super(CopyFormView, self).init_form(form, entity) if entity.eid == self.newentity.eid: form.add_hidden(eid_param('__cloned_eid', entity.eid), self.copying.eid) for rschema, role in form.editable_attributes(): if not rschema.final: # ensure relation cache is filed rset = self.copying.related(rschema, role) self.newentity.cw_set_relation_cache(rschema, role, rset)
def fake_form(formid, field_dict=None, entity_field_dicts=()): """Build _cw.form dictionnary to fake posting of some standard cubicweb form * `formid`, the form id, usually form's __regid__ * `field_dict`, dictionary of name:value for fields that are not tied to an entity * `entity_field_dicts`, list of (entity, dictionary) where dictionary contains name:value for fields that are not tied to the given entity """ assert field_dict or entity_field_dicts, \ 'field_dict and entity_field_dicts arguments must not be both unspecified' if field_dict is None: field_dict = {} form = {'__form_id': formid} fields = [] for field, value in field_dict.items(): fields.append(field) form[field] = value def _add_entity_field(entity, field, value): entity_fields.append(field) form[eid_param(field, entity.eid)] = value for entity, field_dict in entity_field_dicts: if '__maineid' not in form: form['__maineid'] = entity.eid entity_fields = [] form.setdefault('eid', []).append(entity.eid) _add_entity_field(entity, '__type', entity.cw_etype) for field, value in field_dict.items(): _add_entity_field(entity, field, value) if entity_fields: form[eid_param('_cw_entity_fields', entity.eid)] = ','.join(entity_fields) if fields: form['_cw_fields'] = ','.join(sorted(fields)) return form
def _add_entity_field(entity, field, value): entity_fields.append(field) form[eid_param(field, entity.eid)] = value