# HG changeset patch # User sylvain.thenault@logilab.fr # Date 1240423288 -7200 # Node ID ea75dfe32317926dd481c2f5c5ac954fef39c480 # Parent 97774eb6443b783a906d273732209921545660c0 delete-trailing-whitespaces diff -r 97774eb6443b -r ea75dfe32317 web/form.py --- a/web/form.py Wed Apr 22 19:44:15 2009 +0200 +++ b/web/form.py Wed Apr 22 20:01:28 2009 +0200 @@ -29,7 +29,7 @@ controller = 'edit' http_cache_manager = NoHTTPCacheManager add_to_breadcrumbs = False - + def __init__(self, req, rset, **kwargs): super(FormViewMixIn, self).__init__(req, rset, **kwargs) # get validation session data which may have been previously set. @@ -51,7 +51,7 @@ break else: errex.eid = foreid - + def html_headers(self): """return a list of html headers (eg something to be inserted between and of the returned page @@ -59,7 +59,7 @@ by default forms are neither indexed nor followed """ return [NOINDEX, NOFOLLOW] - + def linkable(self): """override since forms are usually linked by an action, so we don't want them to be listed by appli.possible_views @@ -67,7 +67,7 @@ return False -# XXX should disappear +# XXX should disappear class FormMixIn(object): """abstract form mix-in XXX: you should inherit from this FIRST (obscure pb with super call) @@ -81,13 +81,13 @@ self.varmaker = varmaker # XXX deprecated with new form system. Should disappear - + domid = 'entityForm' category = 'form' controller = 'edit' http_cache_manager = NoHTTPCacheManager add_to_breadcrumbs = False - + def __init__(self, req, rset, **kwargs): super(FormMixIn, self).__init__(req, rset, **kwargs) # get validation session data which may have been previously set. @@ -108,8 +108,8 @@ errex.eid = var break else: - errex.eid = foreid - + errex.eid = foreid + def html_headers(self): """return a list of html headers (eg something to be inserted between and of the returned page @@ -117,7 +117,7 @@ by default forms are neither indexed nor followed """ return [NOINDEX, NOFOLLOW] - + def linkable(self): """override since forms are usually linked by an action, so we don't want them to be listed by appli.possible_views @@ -140,7 +140,7 @@ **kwargs): label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() return self.button(label, name=name, type=type, **kwargs) - + def button_apply(self, label=None, type='button', **kwargs): label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() return self.action_button(label, __action='apply', type=type, **kwargs) @@ -148,11 +148,11 @@ def button_delete(self, label=None, type='button', **kwargs): label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() return self.action_button(label, __action='delete', type=type, **kwargs) - + def button_cancel(self, label=None, type='button', **kwargs): label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() return self.action_button(label, __action='cancel', type=type, **kwargs) - + def button_reset(self, label=None, type='reset', name='__action_cancel', **kwargs): label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() @@ -174,7 +174,7 @@ if inlined_entity.get_widget(irschema, x).need_multipart: return True return False - + def error_message(self): """return formatted error message @@ -189,7 +189,7 @@ if not field in displayed) if errors: if len(errors) > 1: - templstr = '
  • %s
  • \n' + templstr = '
  • %s
  • \n' else: templstr = ' %s\n' for field, err in errors: @@ -223,19 +223,19 @@ classdict['_fields_'] = allfields return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) - + class FieldNotFound(Exception): """raised by field_by_name when a field with the given name has not been found """ - + class FieldsForm(FormMixIn, AppRsetObject): __metaclass__ = metafieldsform __registry__ = 'forms' __select__ = yes() - + is_subform = False - + # attributes overrideable through __init__ internal_fields = ('__errorurl',) + NAV_FORM_PARAMETERS needs_js = ('cubicweb.ajax.js', 'cubicweb.edition.js',) @@ -251,7 +251,7 @@ set_error_url = True copy_nav_params = False form_buttons = None # form buttons (button widgets instances) - + def __init__(self, req, rset=None, row=None, col=None, submitmsg=None, **kwargs): super(FieldsForm, self).__init__(req, rset, row=row, col=col) @@ -285,7 +285,7 @@ if field.name == name and field.role == role: return field raise FieldNotFound(name) - + @iclassmethod def remove_field(cls_or_self, field): """remove a field from form class or instance""" @@ -294,7 +294,7 @@ else: fields = cls_or_self.fields fields.remove(field) - + @iclassmethod def append_field(cls_or_self, field): """append a field to form class or instance""" @@ -303,11 +303,11 @@ else: fields = cls_or_self.fields fields.append(field) - + @property def form_needs_multipart(self): """true if the form needs enctype=multipart/form-data""" - return any(field.needs_multipart for field in self.fields) + return any(field.needs_multipart for field in self.fields) def form_add_hidden(self, name, value=None, **kwargs): """add an hidden field to the form""" @@ -319,7 +319,7 @@ field.widget.setdomid = True self.append_field(field) return field - + def add_media(self): """adds media (CSS & JS) required by this widget""" if self.needs_js: @@ -361,7 +361,7 @@ def form_field_display_value(self, field, rendervalues, load_bytes=False): """return field's *string* value to use for display - + looks in 1. previously submitted form values if any (eg on validation error) 2. req.form @@ -382,7 +382,7 @@ value = self.form_field_value(field, load_bytes) if callable(value): value = value(self) - if value != INTERNAL_FIELD_VALUE: + if value != INTERNAL_FIELD_VALUE: value = field.format_value(self.req, value) return value @@ -392,7 +392,7 @@ if callable(value): value = value(self) return value - + def form_field_error(self, field): """return validation error for widget's field, if any""" errex = self.req.data.get('formerrors') @@ -404,11 +404,11 @@ def form_field_format(self, field): """return MIME type used for the given (text or bytes) field""" return self.req.property_value('ui.default-text-format') - + def form_field_encoding(self, field): """return encoding used for the given (text) field""" return self.req.encoding - + def form_field_name(self, field): """return qualified name for the given field""" return field.name @@ -416,7 +416,7 @@ def form_field_id(self, field): """return dom id for the given field""" return field.id - + def form_field_vocabulary(self, field, limit=None): """return vocabulary for the given field. Should be overriden in specific forms using fields which requires some vocabulary @@ -428,13 +428,13 @@ """ return field.name in errex.errors - + class EntityFieldsForm(FieldsForm): __select__ = (match_kwargs('entity') | (one_line_rset & non_final_entity())) - + internal_fields = FieldsForm.internal_fields + ('__type', 'eid') domid = 'entityForm' - + def __init__(self, *args, **kwargs): self.edited_entity = kwargs.pop('entity', None) msg = kwargs.pop('submitmsg', None) @@ -449,7 +449,7 @@ self.form_add_hidden('__linkto', linkto) msg = '%s %s' % (msg, self.req._('and linked')) self.form_add_hidden('__message', msg) - + def _errex_match_field(self, errex, field): """return true if the field has some error in given validation exception """ @@ -484,7 +484,7 @@ value = super(EntityFieldsForm, self).form_field_value(field, load_bytes) return value - + def form_build_context(self, values=None): """overriden to add edit[s|o] hidden fields and to ensure schema fields have eidparam set to True @@ -503,7 +503,7 @@ field.eidparam = True self.fields.append(HiddenInitialValueField(field)) return super(EntityFieldsForm, self).form_build_context(values) - + def form_field_value(self, field, load_bytes=False): """return field's *typed* value @@ -551,7 +551,7 @@ else: value = self._form_field_default_value(field, load_bytes) return value - + def form_field_format(self, field): """return MIME type used for the given (text or bytes) field""" entity = self.edited_entity @@ -566,8 +566,8 @@ if field.eidparam and entity.e_schema.has_metadata(field.name, 'encoding') and ( entity.has_eid() or '%s_encoding' % field.name in entity): return self.edited_entity.attr_metadata(field.name, 'encoding') - return super(EntityFieldsForm, self).form_field_encoding(field) - + return super(EntityFieldsForm, self).form_field_encoding(field) + def form_field_name(self, field): """return qualified name for the given field""" if field.eidparam: @@ -579,7 +579,7 @@ if field.eidparam: return eid_param(field.id, self.edited_entity.eid) return field.id - + def form_field_vocabulary(self, field, limit=None): """return vocabulary for the given field""" role, rtype = field.role, field.name @@ -667,7 +667,7 @@ class CompositeForm(FieldsForm): """form composed for sub-forms""" - + def __init__(self, *args, **kwargs): super(CompositeForm, self).__init__(*args, **kwargs) self.forms = [] diff -r 97774eb6443b -r ea75dfe32317 web/formfields.py --- a/web/formfields.py Wed Apr 22 19:44:15 2009 +0200 +++ b/web/formfields.py Wed Apr 22 20:01:28 2009 +0200 @@ -17,7 +17,7 @@ from cubicweb.web import INTERNAL_FIELD_VALUE from cubicweb.web.formwidgets import ( HiddenInput, TextInput, FileInput, PasswordInput, TextArea, FCKEditor, - Radio, Select, DateTimePicker) + Radio, Select, DateTimePicker) class Field(object): """field class is introduced to control what's displayed in forms. It makes @@ -28,7 +28,7 @@ ---------- all the attributes described below have sensible default value which may be overriden by value given to field's constructor. - + :name: name of the field (basestring), should be unique in a form. :id: @@ -60,7 +60,7 @@ :role: when the field is linked to an entity attribute or relation, tells the role of the entity in the relation (eg 'subject' or 'object') - + """ # default widget associated to this class of fields. May be overriden per # instance @@ -70,7 +70,7 @@ # class attribute used for ordering of fields in a form __creation_rank = 0 - def __init__(self, name=None, id=None, label=None, help=None, + def __init__(self, name=None, id=None, label=None, help=None, widget=None, required=False, initial=None, choices=None, sort=True, internationalizable=False, eidparam=False, role='subject'): @@ -92,7 +92,7 @@ # ordering number for this field instance self.creation_rank = Field.__creation_rank Field.__creation_rank += 1 - + def __unicode__(self): return u'<%s name=%r label=%r id=%r initial=%r @%x>' % ( self.__class__.__name__, self.name, self.label, @@ -109,17 +109,17 @@ self.id = name if not self.label: self.label = name - + def is_visible(self): """return true if the field is not an hidden field""" return not isinstance(self.widget, HiddenInput) - + def actual_fields(self, form): """return actual fields composing this field in case of a compound field, usually simply return self """ yield self - + def format_value(self, req, value): """return value suitable for display where value may be a list or tuple of values @@ -127,7 +127,7 @@ if isinstance(value, (list, tuple)): return [self.format_single_value(req, val) for val in value] return self.format_single_value(req, value) - + def format_single_value(self, req, value): """return value suitable for display""" if value is None or value is False: @@ -139,11 +139,11 @@ def get_widget(self, form): """return the widget instance associated to this field""" return self.widget - + def example_format(self, req): """return a sample string describing what can be given as input for this field - """ + """ return u'' def render(self, form, renderer): @@ -154,7 +154,7 @@ def vocabulary(self, form): """return vocabulary for this field. This method will be called by - widgets which desire it.""" + widgets which desire it.""" if self.choices is not None: if callable(self.choices): vocab = self.choices(req=form.req) @@ -169,13 +169,13 @@ if self.sort: vocab = sorted(vocab) return vocab - + def form_init(self, form): """method called before by form_build_context to trigger potential field initialization requiring the form instance """ pass - + class StringField(Field): def __init__(self, max_length=None, **kwargs): super(StringField, self).__init__(**kwargs) @@ -227,13 +227,13 @@ choices=choices) req.data[self] = field return field - + def actual_fields(self, form): yield self format_field = self.get_format_field(form) if format_field: yield format_field - + def use_fckeditor(self, form): """return True if fckeditor should be used to edit entity's attribute named `attr`, according to user preferences @@ -254,12 +254,12 @@ class FileField(StringField): widget = FileInput needs_multipart = True - + def __init__(self, format_field=None, encoding_field=None, **kwargs): super(FileField, self).__init__(**kwargs) self.format_field = format_field self.encoding_field = encoding_field - + def actual_fields(self, form): yield self if self.format_field: @@ -281,7 +281,7 @@ wdgs.append(self.render_subfield(form, self.format_field, renderer)) if self.encoding_field: wdgs.append(self.render_subfield(form, self.encoding_field, renderer)) - wdgs.append(u'') + wdgs.append(u'') if not self.required and form.context[self]['value']: # trick to be able to delete an uploaded file wdgs.append(u'
    ') @@ -296,10 +296,10 @@ + renderer.render_help(form, field) + u'
    ') - + class EditableFileField(FileField): editable_formats = ('text/plain', 'text/html', 'text/rest') - + def render(self, form, renderer): wdgs = [super(EditableFileField, self).render(form, renderer)] if form.form_field_format(self) in self.editable_formats: @@ -326,7 +326,7 @@ # XXX restore form context? return '\n'.join(wdgs) - + class IntField(Field): def __init__(self, min=None, max=None, **kwargs): super(IntField, self).__init__(**kwargs) @@ -335,14 +335,14 @@ class BooleanField(Field): widget = Radio - + def vocabulary(self, form): if self.choices: return self.choices return [(form.req._('yes'), '1'), (form.req._('no'), '')] -class FloatField(IntField): +class FloatField(IntField): def format_single_value(self, req, value): formatstr = req.property_value('ui.float-format') if value is None: @@ -356,7 +356,7 @@ class DateField(StringField): format_prop = 'ui.date-format' widget = DateTimePicker - + def format_single_value(self, req, value): return value and ustrftime(value, req.property_value(self.format_prop)) or u'' @@ -378,8 +378,8 @@ super(HiddenInitialValueField, self).__init__( name=name, widget=HiddenInput, eidparam=True) self.visible_field = visible_field - - + + class RelationField(Field): def __init__(self, **kwargs): super(RelationField, self).__init__(**kwargs) @@ -387,7 +387,7 @@ @staticmethod def fromcardinality(card, **kwargs): return RelationField(widget=Select(multiple=card in '*+'), **kwargs) - + def vocabulary(self, form): entity = form.edited_entity req = entity.req @@ -407,7 +407,7 @@ else: relatedvocab = [] return res + form.form_field_vocabulary(self) + relatedvocab - + def format_single_value(self, req, value): return value