def handle_page_resource(sn, method, page_id, verbose, argv): ''' some ways to play with page resource note the possible styles handling each request. - LIST: Page.list(sn) - GET: Page(sn, id=page_id)).get() - POST: Page(sn, title=title, source=source, tags="example_py").save() - PUT: sn.save_page(id=page_id, title=title, source=source) ''' ## GET page 123 if method == 'GET' and page_id: page = Page(sn, id=page_id).get(verbose=verbose) pprint.pprint(page.resource) print "page '%(title)s'(#%(identifier)d), last updated at %(date_modified)s" % page.resource ## GET pages elif method == 'GET' and not page_id: pages = Page.list(sn, verbose=verbose) first_p = filter(lambda x: x.relation_is_part_of is None, pages)[0] last_p = max(pages, key=lambda x: x.date_modified) print "has", len(pages), 'pages,', print "from '%s'(#%d) to '%s'(#%d)" % (first_p.title, first_p.identifier, last_p.title, last_p.identifier) print "i'm not gonna show ALL the pages for ya" ## POST page elif method == "POST": title, source = None, None if len(argv) >= 3: title = argv[2] if len(argv) >= 4: source = argv[3] if title: page = Page(sn, title=title, source=source, tags="example_py") page.save(verbose=verbose) pprint.pprint(page.resource) print "created page '%(title)s'(#%(identifier)d) at %(date_created)s" % page.resource print "content:", page.source ## PUT page 123 elif method == "PUT": title, source = None, None if len(argv) >= 4: title = argv[3] if len(argv) >= 5: source = argv[4] if not page_id: sys.exit("I need to know which page you want to edit.") if title: page = sn.save_page(id=page_id, title=title, source=source, verbose=verbose) pprint.pprint(page.resource) print "updated page '%(title)s'(#%(identifier)d) at %(date_created)s" % page.resource print "content:", page.source ## DELETE page elif method == "DELETE": print "c'mon now, this is just a tutorial program. :p"
def _test_object_calls(self, auth): if test_page_object: # LIST page: get list of pages of default note _starting("test Page.list()..") pages = Page.list(auth, verbose=global_verbose) _printout("%d pages" % len(pages)) # LIST page with options last_modified = sorted(pages, \ cmp=lambda x,y: cmp(x.date_modified, y.date_modified))[-1] most_recent = Page.list(auth, sort="date_modified", order="desc", count=1, verbose=global_verbose)[0] for attr in ["identifier", "title", "source", "date_modified"]: last_modified_attr = getattr(last_modified, attr) most_recent_attr = getattr(most_recent, attr) assert_that(last_modified_attr, is_(equal_to(most_recent_attr))) _okay() # GET page: get most recently modified page _starting("test page.get() READ ..") page = Page(auth, id=last_modified.id).get(verbose=global_verbose) assert_that(page.title, is_(equal_to(last_modified.title))) _okay() # you need this to test other resources if True: # POST page: create a page _starting("test page.save() CREATE ..") page = Page(auth, title = "POST test for %s" % test_id, source = "hola!", tags = global_tag ).save(verbose=global_verbose) new_pages = Page.list(auth, verbose=global_verbose) if test_page_object: assert_that(len(pages) +1, is_(equal_to(len(new_pages)))) _okay() if test_page_object: # PUT page: edit the page _starting("test page.save() UPDATE ..") page.source = "modified" page.save(verbose=global_verbose) refetch = Page(auth, id=page.id).get(verbose=global_verbose) assert_that(refetch.source, contains_string("modified")) _okay() # test other resources if test_attachment_object: self._test_attachment_object(page) if test_comment_object: self._test_comment_object(page) if test_collaboration_object: self._test_collaboration_object(page) if test_lock_object: self._test_lock_object(page) if test_revision_object: self._test_revision_object(page) # you need this to test other resources if True: # DELETE page: delete page _starting("test page.delete() DELETE ..") page.delete(verbose=global_verbose) should_raise(SpringnoteError.Response, lambda: Page(auth, id=page.id).get(verbose=global_verbose) ) _okay()
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