def get_link_type_count(self, link_type): """ :param link_type: :return: """ # on the fly links or identification links are only 1 if (link_type != 'TOC' and link_type in list( self.on_the_fly.keys())) or link_type in list( self.identification.keys()): return 1 # query db results = get_records(bibcode=self.bibcode, link_type=link_type) if (results is None): return 0 if link_type == 'DATA': count = 0 for result in results: count += result['itemCount'] return count if link_type in ['ESOURCE', 'ASSOCIATED']: count = 0 for result in results: count += len(result['url']) return count count = 0 for result in results: # get the maximum of count += max(1, result['itemCount'] if 'itemCount' in result else 0) return count
def test_link_all_error_bibcode(self): """ call get_records to fetch all the records for a none existing bibcode :return: """ results = get_records(bibcode='errorbibcode') self.assertEqual(results, None)
def test_link_data_error_bibcode(self): """ return 404 for not finding any records :return: """ results = get_records(bibcode='errorbibcode', link_type='DATA') response = LinkRequest(bibcode='').request_link_type_data(results) self.assertEqual(response._status_code, 404)
def test_link_associated_error_bibcode(self): """ return 404 for not finding any records :return: """ results = get_records(bibcode='errorbibcode', link_type='ASSOCIATED') response = LinkRequest( bibcode='').request_link_type_associated(results) self.assertEqual(response._status_code, 404)
def test_error_with_sub_type(self): """ call get_records to fetch the records for a none existing bibcode, link_type, and link_subtype :return: """ results = get_records(bibcode='errorbibcode', link_type='errorlinktype', link_sub_type='errorlinksubtype') self.assertEqual(results, None)
def request_link_type_on_the_fly(self): """ for these link types, we only need to format the deterministic link and return it :return: """ # as of 3/27/2019 we should have bibcodes with TOC in db, so check to see if for this bibcode # there is an entry, and return accordingly if self.link_type == 'TOC': results = get_records(bibcode=self.bibcode, link_type=self.link_type) if (results is None): return self.__return_response( {'error': 'did not find any records'}, 404) url = self.on_the_fly[self.link_type].format(baseurl=self.baseurl, bibcode=self.bibcode) return self.request_link_type_deterministic_single_url_toJSON(url)
def verify_url(self, url): """ verify that url is in db :param bibcode: :param url: :return: """ parsed_url = urllib.parse.urlparse(urllib.parse.unquote(url)) if (self.__verify_replaced_url_not_in_db(parsed_url.netloc)): return JsonResponse({'link': 'verified'}, 200) results = get_records(bibcode=self.bibcode) for result in results: for a_url in result['url']: parsed_url_db = urllib.parse.urlparse(a_url) if parsed_url.netloc == parsed_url_db.netloc: return JsonResponse({'link': 'verified'}, 200) return JsonResponse({'link': 'not found'}, 200)
def process_request(self): """ process the request :return: json code of the result or error """ current_app.logger.info( 'received request with bibcode=%s, link_type=%s and link_sub_type=%s' % (self.bibcode, self.link_type if self.link_type is not None else '*', self.link_sub_type if self.link_sub_type is not None else '*')) if (len(self.bibcode) == 0): return JsonResponse({'error': 'no bibcode received'}, 400) # return all the link types if self.link_type is None: return self.request_link_type_all() # for these link types, we only need to format the deterministic link and return it if (self.link_type in list(self.on_the_fly.keys())): # as of 3/27/2019 we should have bibcodes with TOC in db, so check to see if for this bibcode # there is an entry, and return accordingly if self.link_type == 'TOC': results = get_records(bibcode=self.bibcode, link_type=self.link_type) if (results is None): return JsonResponse({'error': 'did not find any records'}, 404) return self.request_link_type_on_the_fly() # the rest of the link types query the db # for the following link types we have a single url, and shall be wrapped in a JSON code as well if (self.link_type == 'INSPIRE') or (self.link_type == 'LIBRARYCATALOG') or (self.link_type == 'PRESENTATION'): return self.request_link_type_single_url( get_records(bibcode=self.bibcode, link_type=self.link_type)) # for the following link type the return value is specific to the type if (self.link_type == 'ASSOCIATED'): return self.request_link_type_associated( get_records(bibcode=self.bibcode, link_type=self.link_type)) # for BBB we have defined more specifically the source of full text resources and # have divided them into 7 sub types, defined in Solr field esource if (self.link_type == 'ESOURCE'): return self.request_link_type_esource( get_records(bibcode=self.bibcode, link_type=self.link_type, link_sub_type=self.link_sub_type)) # for BBB we have defined more specifically the type of data and # have divided them into 30+ sub types, defined in Solr field data if (self.link_type == 'DATA'): return self.request_link_type_data( get_records(bibcode=self.bibcode, link_type=self.link_type, link_sub_type=self.link_sub_type)) # for these link types, we only need to format the deterministic link, attach the id to it, and return it if (self.link_type in list(self.identification.keys())): return self.request_link_type_identification() # we did not recognize the link_type, so return an error return JsonResponse( { 'error': 'unrecognizable link type:`{link_type}`'.format( link_type=self.link_type) }, 400)