Changeset 279

Show
Ignore:
Timestamp:
02/21/06 13:31:38
Author:
mylan
Message:

Clean, debug and test newMediaObject function of MetaWeblogAPI

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • SimpleBlog/branches/plone-2.0.5-Blogging-APIs/MetaWeblogAPI.py

    r278 r279  
    3131import re 
    3232import xmlrpclib 
     33import random 
    3334 
    3435authMethods = [ 
     
    3940    'metaWeblog/getRecentPosts', 
    4041    'metaWeblog/getUsersBlogs', 
    41     'metaWeblog/getCategories' 
     42    'metaWeblog/getCategories', 
     43    'metaWeblog/newMediaObject' 
    4244    ] 
    4345 
     
    266268        print 'metaWeblog/newMediaObject' 
    267269         
    268         sm = getSecurityManager() 
    269         m_tool = getToolByName(self, 'portal_membership') 
    270         user = m_tool.getMemberById(username) 
    271         uf = getToolByName(self, 'acl_users') 
    272         if not hasattr(user, 'aq_base'): 
    273             user = user.__of__(uf) 
    274         newSecurityManager(None, user) 
    275  
    276         sbtool = getToolByName(self, 'simpleblog_tool') 
    277  
     270        sbtool = getToolByName(self, 'simpleblog_tool') 
    278271        blog = sbtool.getByUID(blogid) 
    279272         
    280         media_name = struct.get('name', struct.get('name')) 
    281         mime_type = struct.get('type', struct.get('type')) 
    282         data = struct.get('bits', struct.get('bits')) 
    283  
    284         if not 'images' in blog.objectIds(): 
    285             blog.invokeFactory('BlogFolder', id = 'images', title='Container for images') 
    286             images = getattr(blog, 'images') 
    287         else: 
    288             images = blog.images 
    289  
     273        media_name = struct.get('name', None) 
     274        mime_type = struct.get('type', None) 
     275        data = struct.get('bits', '') 
     276 
     277        if not media_name or media_name.startswith('.'): 
     278            return "No 'name' of media object supply or starts with '.'" 
     279        if not mime_type: 
     280            return "No 'type' of media object supply" 
    290281        if not (mime_type.startswith('image') or mime_type.startswith('application')): 
    291282            return "%s - not supported mime tipe." % mime_type 
    292283 
    293         id = sbtool.idFromTitle(media_name) 
    294284        try: 
    295             blog.images.invokeFactory('Image', id=id, title=media_name) #, file=data) 
    296             image = getattr(blog.images, id) 
    297             url = image.absolute_url() 
    298             return url 
     285            if not 'images' in blog.objectIds(): 
     286                blog.invokeFactory('BlogFolder', id = 'images', title='Container for images') 
     287                images = getattr(blog, 'images') 
     288            else: 
     289                images = blog.images 
     290 
     291            id = re.sub('[^A-Za-z0-9_.]', '', re.sub(' ', '_', media_name)).lower() 
     292            while id in images.objectIds(): 
     293                index = id.rfind('.') 
     294                if index > -1: 
     295                    front = id[:index] 
     296                    ext = id[index:] 
     297                else: 
     298                    front = id 
     299                    ext = '' 
     300                id = front + str(random.randint(1,100)) + ext 
     301 
     302            images.invokeFactory('Image', id=id, title=media_name, file=str(data))  
     303            image = getattr(images, id) 
     304 
     305            return image.absolute_url() 
    299306        except Exception, e: 
    300307            return str(e)