def _test_attachment_object(self, page):
        # LIST attachment - count 0
        attaches = Attachment.list(page, verbose=global_verbose)
        assert_that(len(attaches), is_(0))

        # POST attachment
        _starting("test Attachment.upload() CREATE ..")
        data = open(__file__, 'rb') # i shall sacrifice myself for testing!
        attach = Attachment(page, file=data)
        assert_that(attach.date_created, is_(None))
        attach.upload(verbose=global_verbose)
        data.close()
        assert_that(attach.date_created, is_not(None))
        prev_attach_rsrc = attach.resource
        # LIST attachment - count 1
        attaches = Attachment.list(page, verbose=global_verbose)
        assert_that(len(attaches), is_(1))
        assert_that(attaches[0].id, is_(attach.id))
        _okay()

        # PUT attachment
        _starting("test Attachment.upload() UPDATE ..")
        attach.file = File('tmp2', 'CONTENT!')
        attach.upload(verbose=global_verbose)
        assert_that(attach.id          , is_(prev_attach_rsrc["identifier"]))
        assert_that(attach.title       , is_not(prev_attach_rsrc["title"]))
        assert_that(attach.description , is_not(prev_attach_rsrc["description"]))
        assert_that(attach.resource["date_created"], is_(prev_attach_rsrc["date_created"]))
        _okay()
        
        # DELETE attachment
        _starting("test Attachment.delete() ..")
        attach.delete(verbose=global_verbose)
        # LIST attachment - count 0
        attaches = Attachment.list(page, verbose=global_verbose)
        assert_that(len(attaches), is_(0))
        _okay()
        _starting("test Attachment.list() ..")
        _okay()
예제 #2
0
def handle_attachment_resource(sn, method, page_id, verbose, resource_id, argv):
    ''' shows a few ways handling attahcment resources, with page_id and resource_id.

    note the possible styles handling each request.
     - LIST:   Page(sn, id=page_id).list_attachments()
     - GET:    Attachment(Page(sn, id=page_id), id=resource_id).get().download()
     - POST:   Attachment(Page(sn, id=page_id), filename=filename, file=file_obj).upload()
     - PUT:    Attachment(Page(sn, id=page_id), id=resource_id, file=file_obj).upload()
     - DELETE: Page(sn, id=page_id).delete_attachment(resource_id)
    '''
    ## DOWNLOAD attchment
    if method == 'GET' and resource_id:
        page   = Page(sn, id=page_id)   # just initializing, no need to fetch
        attach = Attachment(page, id=resource_id)
        attach.get(verbose=verbose)         # this retrieves metadata, such as filename
        attach.download(verbose=verbose)    # this downloads actual file content
        pprint.pprint(attach.resource)
        print 'saving to', attach.title, '..'

        if os.path.exists(filename):
            print 'filename already exists! abort saving file'
        else:
            file = open(attach.title, 'rw')
            file.write(attach.raw)
            file.close()

    ## LIST attchments
    if method == 'GET' and not resource_id:
        page = Page(sn, id=page_id).get() # fetch page, just curious
        attachments = page.list_attachments(verbose=verbose)
        print 'found', len(attachments), "files under '%(title)s'(#%(identifier)d)" % page.resource
        for attach in attachments:
            print ' - %(title), %(description) bytes' % attach.resource
            
    ## POST atatchment
    elif method == "POST":
        filename = resource_id # resource_id slot has filename to upload
        file_obj = open(filename, 'rb')

        # DON'T HAVE TO give a page object, relation_is_part_of will do.
        page   = Page(sn, id=page_id)   # no fetch, just holding page_id
        attach = Attachment(page, filename=filename, file=file_obj)
        attach.upload(verbose=verbose)
        pprint.pprint(attach.resource)
        print "created '%(title)s' (#%(identifier)d), %(description)d bytes under %(relation_is_part_of)d" % attach.resource
        file_obj.close()

    ## PUT attachment
    elif method == "PUT":
        filename = argv[5]  # you need the resource_id
        file_obj = open(filename, 'rb')

        page   = Page(sn, id=page_id)   # no fetch, just holding page_id
        attach = Attachment(page, id=resource_id, file=file_obj) # knows filename
        attach.upload(verbose=verbose)
        print "updated '%(title)s' (#%(identifier)d), %(description)d bytes under %(relation_is_part_of)d" % attach.resource
        file_obj.close()

    ## DELETE attachment
    elif method == "DELETE":
        page = Page(sn, id=page_id)
        attach = page.delete_attachment(resource_id, verbose=verbose)
        print "deleted file '%(title)s'" % attach.resource