def list_notifications(self, since, page=None, page_size=None, repository_id=None): # check that the since date is valid, and get it into the right format if not hasattr(since, "strftime"): since = dates.parse(since) since = since.strftime("%Y-%m-%dT%H:%M:%SZ") # make the url params into an object params = {"since": since} if page is not None: try: params["page"] = str(page) except: raise JPERException( "Unable to convert page argument to string") if page_size is not None: try: params["pageSize"] = str(page_size) except: raise JPERException( "Unable to convert page_size argument to string") # get the url, which may contain the repository id if it is not None url = self._url("routed", id=repository_id, params=params) # 2016-06-20 TD : switch SSL verification off verify = False # get the response object resp = http.get(url, verify=verify) # check for errors or problems with the response if resp is None: raise JPERConnectionException( "Unable to communicate with the JPER API") if resp.status_code == 401: raise JPERAuthException( "Could not authenticate with JPER with your API key") if resp.status_code == 400: raise JPERException(resp.json().get("error")) if resp.status_code != 200: raise JPERException( "Received unexpected status code from {y}: {x} ".format( x=resp.status_code, y=url)) # create the notification list object j = resp.json() return models.NotificationList(j)
def list_notifications(self, since, page=None, page_size=None, repository_id=None): # check that the since date is valid, and get it into the right format if not hasattr(since, "strftime"): since = dates.parse(since) since = since.strftime("%Y-%m-%dT%H:%M:%SZ") # make the url params into an object params = {"since" : since} if page is not None: try: params["page"] = str(page) except: raise JPERException("Unable to convert page argument to string") if page_size is not None: try: params["pageSize"] = str(page_size) except: raise JPERException("Unable to convert page_size argument to string") # get the url, which may contain the repository id if it is not None url = self._url("routed", id=repository_id, params=params) # 2016-06-20 TD : switch SSL verification off verify = False # get the response object resp = http.get(url, verify=verify) # check for errors or problems with the response if resp is None: raise JPERConnectionException("Unable to communicate with the JPER API") if resp.status_code == 401: raise JPERAuthException("Could not authenticate with JPER with your API key") if resp.status_code == 400: raise JPERException(resp.json().get("error")) if resp.status_code != 200: raise JPERException("Received unexpected status code from {y}: {x} ".format(x=resp.status_code, y=url)) # create the notification list object j = resp.json() return models.NotificationList(j)
def test_19_lantern_active_jobs(self): # Check we can list active Lantern jobs lj1 = LanternJob() lj1.job_id = "123456789" lj1.account = "abcdefg" lj1.status = "complete" lj1.save() lj2 = LanternJob() lj2.job_id = "123456789" lj2.account = "abcdefg" lj2.status = "active" lj2.save() time.sleep(2) lj3 = LanternJob() lj3.job_id = "987654321" lj3.account = "abcdefg" lj3.status = "active" lj3.save(blocking=True) dao = LanternJob() # list all the active jobs, without worrying about cutting them off by date gen = dao.list_active() jobs = [job for job in gen] assert len(jobs) == 2 # now check that we can do a date cut-off, by setting the before date to just after # the oldest one lu = lj2.last_updated ds = dates.parse(lu) ds = ds + timedelta(seconds=1) gen = dao.list_active(checked_before=ds) jobs = [job for job in gen] assert len(jobs) == 1
def stampify(val): return dates.parse(val, format=in_format)
def _xwalk(cls, result): """ Crosswalk the Lantern result to an Enhancement object :param result: :return: """ journal_record = result.get("journal", {}) # publication date publication_date = None if journal_record is not None: publication_date = journal_record.get("dateOfPublication") try: dates.parse(publication_date) except: publication_date = None # Version is_aam = result.get("is_aam", False) version = None if is_aam is not None and is_aam is not False: version = "AAM" # authors authors = result.get("author", []) eauthors = [] if authors is not None: for author in authors: ea = {} if author.get("fullName") is not None: ea["name"] = author.get("fullName") if author.get("affiliation") is not None: ea["affiliation"] = [{"name" : author.get("affiliation")}] if len(ea.keys()) > 0: eauthors.append(ea) # publisher publisher = result.get("publisher") # DOI doi = result.get("doi") # pmcid pmcid = result.get("pmcid") # pmid pmid = result.get("pmid") journal = None issn = None essn = None oa_type = "unknown" if journal_record is not None: # journal title journal = journal_record.get("title") # issn issn = journal_record.get("issn") # essn essn = journal_record.get("eissn") # oa type in_doaj = journal_record.get("in_doaj") if in_doaj is not None: if in_doaj is True: oa_type = "oa" else: oa_type = "hybrid" # self_archiving policies archiving = result.get("archiving") archive_preprint = False archive_postprint = False archive_pdf = False if archiving is not None: archive_preprint = archiving.get("preprint", False) archive_postprint = archiving.get("postprint", False) archive_pdf = archiving.get("pdf", False) # embargo policies embargo = result.get("embargo") embargo_preprint = False embargo_postprint = False embargo_pdf = False if embargo is not None: embargo_preprint = result.get("embargo", {}).get("preprint", False) if not isinstance(embargo_preprint, bool): try: embargo_preprint = int(embargo_preprint) except: pass embargo_postprint = result.get("embargo", {}).get("postprint", False) if not isinstance(embargo_postprint, bool): try: embargo_postprint = int(embargo_postprint) except: pass embargo_pdf = result.get("embargo", {}).get("pdf", False) if not isinstance(embargo_pdf, bool): try: embargo_pdf = int(embargo_pdf) except: pass # grant funding projects = [] grants = result.get("grants", []) if grants is not None: for g in grants: project = {} grant_number = g.get("grantId") if grant_number is not None: project["grant_number"] = grant_number agency = g.get("agency") if agency is not None: project["funder_name"] = agency if len(project.keys()) > 0: projects.append(project) # licence licence_type = None free_to_read = False licence = result.get("licence") if licence != "non-standard-licence": if licence == "free-to-read": free_to_read = True elif licence != "unknown": licence_type = licence # provenance prov = result.get("provenance", []) if prov is not None: prov = "Record enhanced via Lantern (not all data necessarily used), which provided provenance information: " + "; ".join(prov) # repository repository = result.get("repositories", []) rrecords = [] if repository is not None: for r in repository: obj = {} if isinstance(r, basestring): obj["repo_name"] = r obj["metadata"] = "True" obj["fulltext"] = "Unknown" obj["machine_readable_fulltext"] = "Unknown" elif isinstance(r, dict): obj["metadata"] = "True" obj["fulltext"] = "Unknown" obj["machine_readable_fulltext"] = "Unknown" if "name" in r: obj["repo_name"] = r["name"] if "url" in r: obj["repo_url"] = r["url"] if "fulltexts" in r and len(r["fulltexts"]) > 0: obj["record_url"] = " | ".join(r["fulltexts"]) if len(obj.keys()) > 0: rrecords.append(obj) # article title title = result.get("title") # assign the values to the object do = dataobj.DataObj() if publication_date is not None: do._set_single("rioxxterms:publication_date", publication_date) if version is not None: do._set_single("rioxxterms:version", version) if len(eauthors) > 0: do._set_single("rioxxterms:author", eauthors) if publisher is not None: do._set_single("dcterms:publisher.name", publisher) if doi is not None: do._add_to_list("dc:identifier", {"type" : "doi", "id" : doi}) if pmcid is not None: do._add_to_list("dc:identifier", {"type" : "pmcid", "id" : pmcid}) if pmid is not None: do._add_to_list("dc:identifier", {"type" : "pmid", "id" : pmid}) if journal is not None: do._set_single("dc:source.name", journal) if issn is not None: do._add_to_list("dc:source.identifier", {"type" : "issn", "id" : issn}) if issn is not None: do._add_to_list("dc:source.identifier", {"type" : "e-issn", "id" : essn}) if oa_type is not None: do._set_single("dc:source.oa_type", oa_type) if isinstance(archive_preprint, basestring): do._set_single("dc:source.self_archiving.preprint.policy", archive_preprint) if isinstance(archive_postprint, basestring): do._set_single("dc:source.self_archiving.postprint.policy", archive_postprint) if isinstance(archive_pdf, basestring): do._set_single("dc:source.self_archiving.publisher.policy", archive_pdf) if isinstance(embargo_preprint, int) and not isinstance(embargo_preprint, bool): do._set_single("dc:source.self_archiving.preprint.embargo", embargo_preprint) if isinstance(embargo_postprint, int) and not isinstance(embargo_postprint, bool): do._set_single("dc:source.self_archiving.postprint.embargo", embargo_postprint) if isinstance(embargo_pdf, int) and not isinstance(embargo_pdf, bool): do._set_single("dc:source.self_archiving.publisher.embargo", embargo_pdf) if len(projects) > 0: do._set_single("rioxxterms:project", projects) if licence_type is not None: do._add_to_list("ali:license_ref", {"type" : licence_type}) if free_to_read is True: do._set_single("ali:free_to_read.free_to_read", free_to_read) do._add_to_list("jm:provenance", prov) if len(rrecords) > 0: do._set_single("jm:repository", rrecords) if title is not None: do._set_single("dc:title", title) # build and return an Enhancement object around the data e = Enhancement({"record" : do.data}) return e