Back to index

plone3  3.1.7
kupuUploadImage.py
Go to the documentation of this file.
00001 ## Script (Python) "kupuUploadImage"
00002 ##bind container=container
00003 ##bind context=context
00004 ##bind namespace=
00005 ##bind script=script
00006 ##bind subpath=traverse_subpath
00007 ##parameters=node_prop_title, node_prop_desc, node_prop_image
00008 
00009 from Products.CMFCore.utils import getToolByName
00010 from Products.PythonScripts.standard import html_quote, newline_to_br
00011 request = context.REQUEST
00012 RESPONSE =  request.RESPONSE
00013 
00014 TEMPLATE = """
00015 <html>
00016 <head></head>
00017 <body onload="window.parent.drawertool.current_drawer.%s('%s');">%s
00018 </body>
00019 </html>
00020 """
00021 
00022 def Error(fmt, *args):
00023     msg = fmt % args
00024     script = TEMPLATE % ('cancelUpload', msg.replace("'", "\\'"), newline_to_br(html_quote(printed)))
00025     return script
00026 
00027 def cleanupFilename(name):
00028     """Generate a unique id which doesn't match
00029     the system generated ids.
00030     The reason being that setImage will blow up if we have a system
00031     generated id and the id it generates from the filename is already in use.
00032     """
00033     id = ''
00034     name = name.replace('\\', '/') # Fixup Windows filenames
00035     name = name.split('/')[-1] # Throw away any path part.
00036     for c in name:
00037         if c.isalnum() or c in '._':
00038             id += c
00039 
00040     # Race condition here, but not a lot we can do about that
00041     if context.check_id(id) is None and getattr(context,id,None) is None:
00042         return id
00043 
00044     # Now make the id unique
00045     parts = id.split('.')
00046     if len(parts)==1: parts.append('')
00047     count = 1
00048     while 1:
00049         if count==1:
00050             sc = ''
00051         else:
00052             sc = str(count)
00053         id = "copy%s_of_%s" % (sc, id)
00054         if context.check_id(id) is None and getattr(context,id,None) is None:
00055             return id
00056         count += 1
00057 
00058 kupu_tool = getToolByName(context, 'kupu_library_tool')
00059 ctr_tool = getToolByName(context, 'content_type_registry')
00060 
00061 id = request['node_prop_image'].filename
00062 linkbyuid = kupu_tool.getLinkbyuid();
00063 base = context.absolute_url()
00064 
00065 # MTR would also do content-based classification, alas, we don't want it as a dependency here
00066 # content_type= getToolByName(context,'mimetypes_registry').classify(node_prop_image)
00067 
00068 content_type = request['node_prop_image'].headers["Content-Type"]
00069 typename = ctr_tool.findTypeName(id, content_type, "")
00070 
00071 # Permission checks based on code by Danny Bloemendaal
00072 
00073 # 1) check if we are allowed to create an Image in folder 
00074 if not typename in [t.id for t in context.getAllowedTypes()]: 
00075    return Error("Creation of '%s' content is not allowed in %s", typename, context.title_or_id())
00076 
00077 # 2) check if the current user has permissions to add stuff 
00078 if not context.portal_membership.checkPermission('Add portal content',context): 
00079     return Error("You do not have permission to add content in %s", context.getId())
00080 
00081 # Get an unused filename without path
00082 id = cleanupFilename(id)
00083 
00084 newid = context.invokeFactory(type_name=typename, id=id,
00085     title=node_prop_title,
00086     description=node_prop_desc,
00087     )
00088 
00089 if newid is None or newid == '':
00090    newid = id 
00091 
00092 obj = getattr(context,newid, None)
00093 obj.setImage(node_prop_image)
00094     
00095 if not obj:
00096    return Error("Could not create %s with %s as id and %s as title!", typename,newid, node_prop_title)
00097 
00098 obj.reindexObject() 
00099 if linkbyuid and hasattr(obj, 'UID'):
00100     url = base+'/resolveuid/%s' % obj.UID()
00101 else:
00102     url = obj.absolute_url()
00103 
00104 print "Uploaded image"
00105 # print "content_type", content_type
00106 # print "typename", typename
00107 # print "RESPONSE=", RESPONSE
00108 return TEMPLATE % ('finishUpload', url, newline_to_br(html_quote(printed)))
00109 
00110