Exemple #1
0
def get_record_info(**args):
    # Did we get bibcodes?
    if args.get('bibcodes', []):
        IDmap = get_identifiers(args['bibcodes'])
        IDs = [x[1] for x in IDmap]
        bibs = [x[0] for x in IDmap]
        bibs_ref = [x[0] for x in IDmap if x[2]]
        missing = [b for b in args['bibcodes'] if b not in bibs]
        return bibs, bibs_ref, IDs, missing
    # If we received a query, retrieve the associated bibcodes from Solr
    elif args.get('query', None):
        bibcodes = []
        q = args.get('query')
        
        if isinstance(q, basestring):
            params = {'q': q}
        elif isinstance(q, dict):
            params = dict(q) # clone it
        else:
            return {"Error": "Invalid value of the 'query' parameter: %s" % q,
                    "Status Code": 403}

        # override some values of the solr query
        params['fl'] = 'bibcode'
        params['wt'] = 'json'
        params['rows'] = 10000
        params['rows'] = max(min(int(params.get('rows', 10)), current_app.config['METRICS_MAX_SUBMITTED']), 1)

        headers = {'X-Forwarded-Authorization':
                   request.headers.get('Authorization')}
        response = client().get(
            current_app.config.get('METRICS_SOLRQUERY_URL'),
            params=params, headers=headers)
        if response.status_code != 200:
            return {"Error": "Unable to get results!",
                    "Error Info": response.text,
                    "Status Code": response.status_code}
        resp = response.json()
        biblist = [d['bibcode']
                   for d in resp['response']['docs'] if 'bibcode' in d]
        IDmap = get_identifiers(biblist)
        IDs = [x[1] for x in IDmap]
        bibs = [x[0] for x in IDmap]
        bibs_ref = [x[0] for x in IDmap if x[2]]
        missing = [b for b in biblist if b not in bibs]
        return bibs, bibs_ref, IDs, missing
    else:
        return {"Error": "Unable to get results!",
                "Error Info": "Unsupported metrics request",
                "Status Code": 200}
Exemple #2
0
def get_record_info(**args):
    # Did we get bibcodes?
    if args.get('bibcodes', []):
        IDmap = get_identifiers(args['bibcodes'])
        IDs = [x[1] for x in IDmap]
        bibs = [x[0] for x in IDmap]
        bibs_ref = [x[0] for x in IDmap if x[2]]
        missing = [b for b in args['bibcodes'] if b not in bibs]
        return bibs, bibs_ref, IDs, missing
    # If we received a query, retrieve the associated bibcodes from Solr
    elif args.get('query', None):
        bibcodes = []
        q = args.get('query')
        fl = "bibcode"
        headers = {
            'X-Forwarded-Authorization': request.headers.get('Authorization')
        }
        params = {
            'wt': 'json',
            'q': q,
            'fl': fl,
            'rows': current_app.config['METRICS_MAX_HITS']
        }
        response = client().get(
            current_app.config.get('METRICS_SOLRQUERY_URL'),
            params=params,
            headers=headers)
        if response.status_code != 200:
            return {
                "Error": "Unable to get results!",
                "Error Info": response.text,
                "Status Code": response.status_code
            }
        resp = response.json()
        biblist = [
            d['bibcode'] for d in resp['response']['docs'] if 'bibcode' in d
        ]
        IDmap = get_identifiers(biblist)
        IDs = [x[1] for x in IDmap]
        bibs = [x[0] for x in IDmap]
        bibs_ref = [x[0] for x in IDmap if x[2]]
        missing = [b for b in biblist if b not in bibs]
        return bibs, bibs_ref, IDs, missing
    else:
        return {
            "Error": "Unable to get results!",
            "Error Info": "Unsupported metrics request",
            "Status Code": 200
        }
 def test_get_identifiers(self):
     '''Test getting the identifiers for a set of bibcodes'''
     from models import get_identifiers
     data = get_identifiers(testset)
     # We are expecting data to be a dictionary with bibcodes as keys
     # and integers as values (don't really care what the actual values are)
     self.assertEqual(isinstance(data, list), True)
     self.assertTrue(False not in [len(x) == 3 for x in data])
     self.assertTrue(False not in [isinstance(x[0], unicode) and
                     len(x[0]) == 19 for x in data])
     self.assertTrue(False not in [isinstance(x[1], int) for x in data])
     self.assertTrue(False not in [isinstance(x[2], bool) for x in data])
 def test_get_identifiers(self):
     '''Test getting the identifiers for a set of bibcodes'''
     from models import get_identifiers
     data = get_identifiers(testset)
     # We are expecting data to be a dictionary with bibcodes as keys
     # and integers as values (don't really care what the actual values are)
     self.assertEqual(isinstance(data, list), True)
     self.assertTrue(False not in [len(x) == 3 for x in data])
     self.assertTrue(
         False not in
         [isinstance(x[0], unicode) and len(x[0]) == 19 for x in data])
     self.assertTrue(False not in [isinstance(x[1], int) for x in data])
     self.assertTrue(False not in [isinstance(x[2], bool) for x in data])
def get_record_info(**args):
    # Did we get bibcodes?
    if args.get('bibcodes', []):
        IDmap = get_identifiers(args['bibcodes'])
        IDs = [x[1] for x in IDmap]
        bibs = [x[0] for x in IDmap]
        bibs_ref = [x[0] for x in IDmap if x[2]]
        missing = [b for b in args['bibcodes'] if b not in bibs]
        return bibs, bibs_ref, IDs, missing
    # If we received a query, retrieve the associated bibcodes from Solr
    elif args.get('query', None):
        bibcodes = []
        q = args.get('query')
        fl = "bibcode"
        headers = {'X-Forwarded-Authorization':
                   request.headers.get('Authorization')}
        params = {'wt': 'json', 'q': q, 'fl': fl,
                  'rows': current_app.config['METRICS_MAX_HITS']}
        response = client().get(
            current_app.config.get('METRICS_SOLRQUERY_URL'),
            params=params, headers=headers)
        if response.status_code != 200:
            return {"Error": "Unable to get results!",
                    "Error Info": response.text,
                    "Status Code": response.status_code}
        resp = response.json()
        biblist = [d['bibcode']
                   for d in resp['response']['docs'] if 'bibcode' in d]
        IDmap = get_identifiers(biblist)
        IDs = [x[1] for x in IDmap]
        bibs = [x[0] for x in IDmap]
        bibs_ref = [x[0] for x in IDmap if x[2]]
        missing = [b for b in biblist if b not in bibs]
        return bibs, bibs_ref, IDs, missing
    else:
        return {"Error": "Unable to get results!",
                "Error Info": "Unsupported metrics request",
                "Status Code": 200}