# HG changeset patch # User sylvain.thenault@logilab.fr # Date 1236778755 -3600 # Node ID 185d630d1aef05fc81f103daa9eec3aeaa9fab7a # Parent 7d2e6b5fae3c689d53f32db4b7cceff4cf84c02a EditableFileField diff -r 7d2e6b5fae3c -r 185d630d1aef web/form.py --- a/web/form.py Wed Mar 11 14:38:44 2009 +0100 +++ b/web/form.py Wed Mar 11 14:39:15 2009 +0100 @@ -359,10 +359,6 @@ return super(FCKEditor, self).render(form, field) -#class EditableFile(Widget): -# # XXX -# pass - class Select(FieldWidget): def __init__(self, attrs=None, multiple=False): super(Select, self).__init__(attrs) @@ -520,7 +516,6 @@ def render(self, form, renderer): return self.get_widget(form).render(form, self) - def vocabulary(self, form): return self.choices @@ -644,7 +639,37 @@ + 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: + data = form.form_field_value(self, {}, load_bytes=True) + if data: + encoding = form.form_field_encoding(self) + try: + form.context[self]['value'] = unicode(data.getvalue(), encoding) + form.context[self]['rawvalue'] = form.context[self]['value'] + except UnicodeError: + pass + else: + if not self.required: + msg = form.req._( + 'You can either submit a new file using the browse button above' + ', or choose to remove already uploaded file by checking the ' + '"detach attached file" check-box, or edit file content online ' + 'with the widget below.') + else: + msg = form.req._( + 'You can either submit a new file using the browse button above' + ', or edit file content online with the widget below.') + wdgs.append(u'

%s

' % msg) + wdgs.append(TextArea(setdomid=False).render(form, self)) + # XXX restore form context? + return '\n'.join(wdgs) + + class IntField(Field): def __init__(self, min=None, max=None, **kwargs): super(IntField, self).__init__(**kwargs)