def publish_module(self, module_url, credentials, logger, new=True): """ Publishes the module at [module_url] with [credentials] using HTTP requests. Returns the published module ID. Raises a CustomError on http request failure. """ username, password = credentials.split(':') data1 = {"message": "created module", "form.button.publish": "Publish", "form.submitted": "1"} response1 = http.http_post_request("%smodule_publish_description" % module_url, auth=(username, password), data=data1) if not http.verify(response1, logger): raise CCTError("publish module for %s request 1 failed: %s %s" % (module_url, response1.status_code, response1.reason)) if new: data2 = {"message": "created module", "publish": "Yes, Publish"} response2 = http.http_post_request("%spublishContent" % module_url, auth=(username, password), data=data2) if not http.verify(response2, logger): raise CCTError("publish module for %s request 2 failed: %s %s" % (module_url, response1.status_code, response1.reason)) # extract module ID url = response2.url.encode('UTF-8') end_id = regex.search('/content_published', url).start() beg = url.rfind('/', 0, end_id) + 1 return url[beg:end_id], url else: return module_url[module_url.rfind('/', 0, -1) + 1:-1], module_url
def add_subcollections(self, titles, server, credentials, collection, logger): logger.info("Adding subcollections to collection %s: %s" % (collection.title, titles)) auth = tuple(credentials.split(':')) base = "%s/Members/%s/%s/" % (server, auth[0], collection.get_parents_url()) data4 = {"form.submitted": "1", "titles": "\n".join(titles), "submit": "Add new subcollections"} subcollection = '@@collection-composer-collection-subcollection' response = http.http_post_request(base + subcollection, auth=auth, data=data4) if not http.verify(response, logger): raise CCTError("Creation of subcollection(s) %s request failed: %s %s" % (titles, response.status_code, response.reason)) text = response.text[len("close:["):-1] text = text.split("},{") subcollections = [] for subcollection_response in text: subcollection_id_start = regex.search(r'nodeid\':\'', subcollection_response).end() subcollection_id = subcollection_response[subcollection_id_start: subcollection_response.find("'", subcollection_id_start)] subcollection_title_start = regex.search(r'text\':\s*\'', subcollection_response).end() subcollection_title = subcollection_response[subcollection_title_start: subcollection_response.find("'", subcollection_title_start)] subcollection = Collection(subcollection_title, subcollection_id) subcollection.parent = collection collection.add_member(subcollection) subcollections.append(subcollection) return subcollections
def publish_collection(self, server, credentials, collection, logger): logger.info("Publishing collection %s" % collection.title) auth = tuple(credentials.split(':')) publish_message = "Initial publish" data1 = {"message": publish_message, "form.button.publish": "Publish", "form.submitted": "1"} response1 = http.http_post_request("%s/Members/%s/%s/collection_publish" % (server, auth[0], collection.id), auth=auth, data=data1) if not http.verify(response1, logger): raise CCTError("Publishing collection %s request 1 failed: %s %s" % (collection.title, response1.status_code, response1.reason)) data2 = {"message": publish_message, "publish": "Yes, Publish"} response2 = http.http_post_request("%s/Members/%s/%s/publishContent" % (server, auth[0], collection.id), auth=auth, data=data2) if not http.verify(response2, logger): raise CCTError("Publishing collection %s request 2 failed: %s %s" % (collection.title, response2.status_code, response2.reason))
def create_collection(self, credentials, title, server, logger): logger.info("Creating collection %s" % title) auth = tuple(credentials.split(":")) data0 = {"type_name": "Collection", "workspace_factories:method": "Create New Item"} response0 = http.http_post_request("%s/Members/%s" % (server, auth[0]), auth=auth, data=data0) if not http.verify(response0, logger): raise CCTError( "Creation of collection %s request 2 failed: %s %s" % (title, response0.status_code, response0.reason) ) cc_license = self.get_license(response0, logger) data1 = { "agree": "on", "form.button.next": "Next >>", "license": cc_license, "type_name": "Collection", "form.submitted": "1", } data2 = { "title": title, "master_language": "en", "language": "en", "collectionType": "", "keywords:lines": "", "abstract": "", "license": cc_license, "form.button.next": "Next >>", "form.submitted": "1", } response1 = http.http_post_request(response0.url, auth=auth, data=data1) if not http.verify(response1, None): raise CCTError( "Creation of collection %s request 2 failed: %s %s" % (title, response1.status_code, response1.reason) ) url = response1.url base = url[: url.rfind("/") + 1] response2 = http.http_post_request("%s/content_title" % base, auth=auth, data=data2) if not http.verify(response2, None): raise CCTError( "Creation of collection %s request 3 failed: %s %s" % (title, response2.status_code, response2.reason) ) start = base[:-1].rfind("/") + 1 return Collection(title, str(base[start:-1]))
def create_module(self, title, credentials, workspace_url, logger): """ Creates a module with [title] in [workspace_url] with [credentials]. Returns the url of the created module. Raises CustomError on failed http requests. """ username, password = credentials.split(":") auth = username, password data1 = {"type_name": "Module", "workspace_factories:method": "Create New Item"} response1 = http.http_post_request(workspace_url, auth=auth, data=data1) if not http.verify(response1, logger): raise CCTError( "create module for %s request 1 failed: %s %s" % (title, response1.status_code, response1.reason) ) cc_license = self.get_license(response1, logger) data2 = {"agree": "on", "form.button.next": "Next >>", "license": cc_license, "form.submitted": "1"} data3 = { "title": title, "master_language": "en", "language": "en", "license": cc_license, "form.button.next": "Next >>", "form.submitted": "1", } response2 = http.http_post_request(response1.url.encode("UTF-8"), auth=auth, data=data2) if not http.verify(response2, logger): raise CCTError( "create module for %s request 2 failed: %s %s" % (title, response2.status_code, response2.reason) ) r2url = response2.url.encode("UTF-8") create_url = r2url[: regex.search("cc_license", r2url).start()] response3 = http.http_post_request("%scontent_title" % create_url, auth=auth, data=data3) if not http.verify(response3, logger): raise CCTError( "create module for %s request 3 failed: %s %s" % (title, response3.status_code, response3.reason) ) return create_url
def create_module(self, title, credentials, workspace_url, logger): """ Creates a module with [title] in [workspace_url] with [credentials]. Returns the url of the created module. Raises CustomError on failed http requests. """ username, password = credentials.split(':') auth = username, password data1 = {"type_name": "Module", "workspace_factories:method": "Create New Item"} response1 = http.http_post_request(workspace_url, auth=auth, data=data1) if not http.verify(response1, logger): raise CCTError("create module for %s request 1 failed: %s %s" % (title, response1.status_code, response1.reason)) cc_license = self.get_license(response1, logger) data2 = {"agree": "on", "form.button.next": "Next >>", "license": cc_license, "form.submitted": "1"} data3 = {"title": title, "master_language": "en", "language": "en", "license": cc_license, "form.button.next": "Next >>", "form.submitted": "1"} response2 = http.http_post_request(response1.url.encode('UTF-8'), auth=auth, data=data2) if not http.verify(response2, logger): raise CCTError("create module for %s request 2 failed: %s %s" % (title, response2.status_code, response2.reason)) r2url = response2.url.encode('UTF-8') create_url = r2url[:regex.search('cc_license', r2url).start()] response3 = http.http_post_request("%scontent_title" % create_url, auth=auth, data=data3) if not http.verify(response3, logger): raise CCTError("create module for %s request 3 failed: %s %s" % (title, response3.status_code, response3.reason)) return create_url
def create_collection(self, credentials, title, server, logger): logger.info("Creating collection %s" % title) auth = tuple(credentials.split(':')) data0 = {"type_name": "Collection", "workspace_factories:method": "Create New Item"} response0 = http.http_post_request("%s/Members/%s" % (server, auth[0]), auth=auth, data=data0) if not http.verify(response0, logger): raise CCTError("Creation of collection %s request 2 failed: %s %s" % (title, response0.status_code, response0.reason)) cc_license = self.get_license(response0, logger) data1 = {"agree": "on", "form.button.next": "Next >>", "license": cc_license, "type_name": "Collection", "form.submitted": "1"} data2 = {"title": title, "master_language": "en", "language": "en", "collectionType": "", "keywords:lines": "", "abstract": "", "license": cc_license, "form.button.next": "Next >>", "form.submitted": "1"} response1 = http.http_post_request(response0.url, auth=auth, data=data1) if not http.verify(response1, None): raise CCTError("Creation of collection %s request 2 failed: %s %s" % (title, response1.status_code, response1.reason)) url = response1.url base = url[:url.rfind('/')+1] response2 = http.http_post_request("%s/content_title" % base, auth=auth, data=data2) if not http.verify(response2, None): raise CCTError("Creation of collection %s request 3 failed: %s %s" % (title, response2.status_code, response2.reason)) start = base[:-1].rfind('/')+1 return Collection(title, str(base[start:-1]))
def add_modules_to_collection(self, modules, server, credentials, collection, logger, failures): modules_str = "" for module in modules: modules_str += "%s " % module.destination_id logger.info("Adding modules to collection %s: %s" % (collection.title, modules_str)) auth = tuple(credentials.split(':')) data = {"form.submitted": "1", "form.action": "submit"} collection_url = collection.get_parents_url() for module in modules: if not module.valid: continue data["ids:list"] = module.destination_id response = http.http_post_request("%s/Members/%s/%s/@@collection-composer-collection-module" % (server, auth[0], collection_url), auth=auth, data=data) if not http.verify(response, logger): logger.error("Module %s failed to be added to collection %s" % (module.title, collection.title)) module.valid = False failures.append((module.full_title(), " adding to collection")) continue
def create_workgroup(self, workgroup, server, credentials, logger): """ Creates a workgroup with [title] on [server] with [credentials] using HTTP requests. Returns: None Modifies: The workgroup provided is updated with the new found information: id and url """ username, password = credentials.split(':') data = {"title": workgroup.title, "form.button.Reference": "Create", "form.submitted": "1"} response = http.http_post_request("%s/create_workgroup" % server, auth=(username, password), data=data) if not http.verify(response, logger): raise CCTError("%s %s" % (response.status_code, response.reason)) # extract workgroup ID url = response.url.encode('UTF-8') id_start = regex.search('GroupWorkspaces/', url).end() id_end = url.find('/', id_start) workgroup.id = url[id_start:id_end] workgroup.url = url[:id_end]