set contentType automatically when uploading a file
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1591 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
24fb20d78a
commit
e6afb6c328
5 changed files with 25 additions and 9 deletions
|
@ -158,7 +158,7 @@ function toggleFormFieldHelp(ob,state) {
|
||||||
<form metal:use-macro="views/resource_macros/delete_object" />
|
<form metal:use-macro="views/resource_macros/delete_object" />
|
||||||
|
|
||||||
<script type="text/javascript" metal:define-slot="trackChanges">
|
<script type="text/javascript" metal:define-slot="trackChanges">
|
||||||
zc_trackChanges(document.getElementById('zc.page.browser_form'));
|
//zc_trackChanges(document.getElementById('zc.page.browser_form'));
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript"
|
<script type="text/javascript"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#
|
#
|
||||||
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
|
# Copyright (c) 2007 Helmut Merz helmutm@cy55.de
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
@ -29,10 +29,10 @@ from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent
|
||||||
|
|
||||||
from zope.app.container.interfaces import INameChooser
|
from zope.app.container.interfaces import INameChooser
|
||||||
from zope.app.container.contained import NameChooser
|
from zope.app.container.contained import NameChooser
|
||||||
#from zope.app.content_types import guess_content_types
|
|
||||||
from zope.app.form.browser.textwidgets import FileWidget, TextAreaWidget
|
from zope.app.form.browser.textwidgets import FileWidget, TextAreaWidget
|
||||||
from zope.app.pagetemplate import ViewPageTemplateFile
|
from zope.app.pagetemplate import ViewPageTemplateFile
|
||||||
from zope.cachedescriptors.property import Lazy
|
from zope.cachedescriptors.property import Lazy
|
||||||
|
from zope.contenttype import guess_content_type
|
||||||
from zope.formlib.form import Form, EditForm, FormFields
|
from zope.formlib.form import Form, EditForm, FormFields
|
||||||
from zope.publisher.browser import FileUpload
|
from zope.publisher.browser import FileUpload
|
||||||
from zope.publisher.interfaces import BadRequest
|
from zope.publisher.interfaces import BadRequest
|
||||||
|
@ -56,13 +56,18 @@ from loops.util import _
|
||||||
class UploadWidget(FileWidget):
|
class UploadWidget(FileWidget):
|
||||||
|
|
||||||
def _toFieldValue(self, input):
|
def _toFieldValue(self, input):
|
||||||
|
# not used at the moment as the context object is updated
|
||||||
|
# via EditObject.updateFields()
|
||||||
fn = getattr(input, 'filename', '') # zope.publisher.browser.FileUpload
|
fn = getattr(input, 'filename', '') # zope.publisher.browser.FileUpload
|
||||||
self.request.form['filename'] = fn
|
self.request.form['filename'] = fn
|
||||||
if input:
|
if input:
|
||||||
self.request.form['_tempfilename'] = input.headers.get('_tempfilename')
|
self.request.form['_tempfilename'] = input.headers.get('_tempfilename')
|
||||||
# f = self.context
|
# f = self.context
|
||||||
# f.extfiledata = tempfilename # provide for rename
|
# f.extfiledata = tempfilename # provide for rename
|
||||||
# f.contentType = guess_content_types(fn)
|
if fn:
|
||||||
|
contentType = guess_content_type(fn)
|
||||||
|
if contentType:
|
||||||
|
request.form['form.contentType'] = contentType
|
||||||
return super(UploadWidget, self)._toFieldValue(input)
|
return super(UploadWidget, self)._toFieldValue(input)
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,7 +124,10 @@ class EditObjectForm(ObjectForm, EditForm):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def form_fields(self):
|
def form_fields(self):
|
||||||
return FormFields(self.typeInterface)
|
ff = FormFields(self.typeInterface)
|
||||||
|
# if self.typeInterface in (IFile, IExternalFile):
|
||||||
|
#ff['data'].custom_widget = UploadWidget
|
||||||
|
return ff
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def assignments(self):
|
def assignments(self):
|
||||||
|
@ -151,7 +159,9 @@ class CreateObjectForm(ObjectForm, Form):
|
||||||
else:
|
else:
|
||||||
ifc = INote
|
ifc = INote
|
||||||
self.typeInterface = ifc
|
self.typeInterface = ifc
|
||||||
return FormFields(ifc)
|
ff = FormFields(ifc)
|
||||||
|
#ff['data'].custom_widget = UploadWidget
|
||||||
|
return ff
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def assignments(self):
|
def assignments(self):
|
||||||
|
@ -188,6 +198,7 @@ class EditObject(FormController):
|
||||||
return self.view.loopsRoot
|
return self.view.loopsRoot
|
||||||
|
|
||||||
def updateFields(self, obj):
|
def updateFields(self, obj):
|
||||||
|
# TODO: replace with `applyChanges()`
|
||||||
form = self.request.form
|
form = self.request.form
|
||||||
ti = IType(obj).typeInterface
|
ti = IType(obj).typeInterface
|
||||||
if ti is not None:
|
if ti is not None:
|
||||||
|
@ -204,7 +215,12 @@ class EditObject(FormController):
|
||||||
self.collectConcepts(fn[len(self.conceptPrefix):], value)
|
self.collectConcepts(fn[len(self.conceptPrefix):], value)
|
||||||
else:
|
else:
|
||||||
if isinstance(value, FileUpload):
|
if isinstance(value, FileUpload):
|
||||||
|
filename = getattr(value, 'filename', '')
|
||||||
value = value.read()
|
value = value.read()
|
||||||
|
if filename:
|
||||||
|
contentType = guess_content_type(filename, value[:100])
|
||||||
|
if contentType:
|
||||||
|
self.request.form['form.contentType'] = contentType[0]
|
||||||
setattr(adapted, fn, value)
|
setattr(adapted, fn, value)
|
||||||
if self.old or self.selected:
|
if self.old or self.selected:
|
||||||
self.assignConcepts(obj)
|
self.assignConcepts(obj)
|
||||||
|
|
|
@ -149,7 +149,7 @@ function toggleFormFieldHelp(ob,state) {
|
||||||
<form metal:use-macro="views/resource_macros/delete_object" />
|
<form metal:use-macro="views/resource_macros/delete_object" />
|
||||||
|
|
||||||
<script type="text/javascript" metal:define-slot="trackChanges">
|
<script type="text/javascript" metal:define-slot="trackChanges">
|
||||||
zc_trackChanges(document.getElementById('zc.page.browser_form'));
|
//zc_trackChanges(document.getElementById('zc.page.browser_form'));
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript"
|
<script type="text/javascript"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#
|
#
|
||||||
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
|
# Copyright (c) 2007 Helmut Merz helmutm@cy55.de
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -288,7 +288,7 @@ class DocumentWriteFileAdapter(object):
|
||||||
# TODO: use typeInterface...
|
# TODO: use typeInterface...
|
||||||
ti = IType(self.context).typeInterface
|
ti = IType(self.context).typeInterface
|
||||||
context = ti is None and self.context or ti(self.context)
|
context = ti is None and self.context or ti(self.context)
|
||||||
if ITextDocument.providedBy(context):
|
if ITextDocument.providedBy(context) or IDocument.providedBy(context):
|
||||||
context.data = unicode(data.replace('\r', ''), 'UTF-8')
|
context.data = unicode(data.replace('\r', ''), 'UTF-8')
|
||||||
else:
|
else:
|
||||||
# TODO: make use of tmpfile when using external files
|
# TODO: make use of tmpfile when using external files
|
||||||
|
|
Loading…
Add table
Reference in a new issue