Ménage. jslint ok.
[Plinn.git] / skins / generic / add_uploaded_content.py
1 ##parameters=
2 #assumes that jupload send files one by one.
3 from Products.CMFCore.exceptions import BadRequest
4 from Products.CMFCore.utils import getToolByName
5 from Products.Plinn.utils import makeValidId
6 form = context.REQUEST.form
7 ctr = getToolByName(context, 'content_type_registry')
8
9 file = [form[name] for name in form.keys() if name.startswith('File')][0]
10 filename = file.filename
11 utf8filename = filename.split('%25')[0]
12 if utf8filename != filename :
13 for p in filename.split('%25')[1:] :
14 utf8filename += chr(int(p[0:2], 16)) + p[2:]
15 filename = utf8filename
16
17 allow_dup = form.get('overwrite', False)
18 id = makeValidId(context, filename, allow_dup=allow_dup)
19 mt = form['mimetype[]']
20
21 # adapted from plone jupload
22 pt = ctr.findTypeName(id.lower(), mt, file)
23 try:
24 id = context.invokeFactory( type_name=pt,
25 id=id,
26 file='',
27 content_type=mt)
28 o = getattr(context, id)
29 o.manage_upload(file)
30 except BadRequest:
31 if allow_dup :
32 o = getattr(context, id)
33 if o.meta_type == 'Photo' :
34 o.manage_upload(file)
35 else :
36 o.edit(file=file)
37 else :
38 raise
39 except TypeError:
40 # looks like the constructor does no support a file argument
41 # (probably trying to create a cmf document)
42 context.invokeFactory( type_name=pt,
43 id=id,
44 text_format=mt.split('/')[1],
45 text=file.read())
46
47 return 'SUCCESS'