Beispiel #1
0
def build_q_list(param):
    q_list = []
    if 'q' in param:
        q_param = param['q'].strip()
    else:
        q_param = None
    use_dismax = False
    if q_param:
        if q_param == '*:*':
            q_list.append(q_param)
        elif 'NOT ' in q_param:  # this is a hack
            q_list.append(q_param.strip())
        elif re_fields.search(q_param):
            q_list.extend(i['op'] if 'op' in i else '%s:(%s)' %
                          (i['field'], i['value'])
                          for i in parse_query_fields(q_param))
        else:
            isbn = read_isbn(q_param)
            if isbn:
                q_list.append('isbn:(%s)' % isbn)
            else:
                q_list.append(q_param.strip().replace(':', '\:'))
                use_dismax = True
    else:
        if 'author' in param:
            v = param['author'].strip()
            m = re_author_key.search(v)
            if m:
                q_list.append("author_key:(%s)" % m.group(1))
            else:
                v = re_to_esc.sub(lambda m: '\\' + m.group(), v)
                # Somehow v can be empty at this point,
                #   passing the following with empty strings causes a severe error in SOLR
                if v:
                    q_list.append(
                        "(author_name:(%(name)s) OR author_alternative_name:(%(name)s))"
                        % {'name': v})

        check_params = [
            'title', 'publisher', 'oclc', 'lccn', 'contribtor', 'subject',
            'place', 'person', 'time'
        ]
        q_list += [
            '%s:(%s)' % (k, param[k]) for k in check_params if k in param
        ]
        if param.get('isbn'):
            q_list.append('isbn:(%s)' %
                          (read_isbn(param['isbn']) or param['isbn']))
    return (q_list, use_dismax)
Beispiel #2
0
def parse_query_fields(q):
    found = [(m.start(), m.end()) for m in re_fields.finditer(q)]
    first = q[:found[0][0]].strip() if found else q.strip()
    if first:
        yield {'field': 'text', 'value': first.replace(':', '\:')}
    for field_num in range(len(found)):
        op_found = None
        f = found[field_num]
        field_name = q[f[0]:f[1] - 1].lower()
        if field_name in field_name_map:
            field_name = field_name_map[field_name]
        if field_num == len(found) - 1:
            v = q[f[1]:].strip()
        else:
            v = q[f[1]:found[field_num + 1][0]].strip()
            m = re_op.search(v)
            if m:
                v = v[:-len(m.group(0))]
                op_found = m.group(1)
        if field_name == 'isbn':
            isbn = read_isbn(v)
            if isbn:
                v = isbn
        yield {'field': field_name, 'value': v.replace(':', '\:')}
        if op_found:
            yield {'op': op_found}
Beispiel #3
0
def parse_query_fields(q):
    found = [(m.start(), m.end()) for m in re_fields.finditer(q)]
    first = q[:found[0][0]].strip() if found else q.strip()
    if first:
        yield {'field': 'text', 'value': first.replace(':', '\:')}
    for field_num in range(len(found)):
        op_found = None
        f = found[field_num]
        field_name = q[f[0]:f[1]-1].lower()
        if field_name in field_name_map:
            field_name = field_name_map[field_name]
        if field_num == len(found)-1:
            v = q[f[1]:].strip()
        else:
            v = q[f[1]:found[field_num+1][0]].strip()
            m = re_op.search(v)
            if m:
                v = v[:-len(m.group(0))]
                op_found = m.group(1)
        if field_name == 'isbn':
            isbn = read_isbn(v)
            if isbn:
                v = isbn
        yield {'field': field_name, 'value': v.replace(':', '\:')}
        if op_found:
            yield {'op': op_found }
Beispiel #4
0
def build_q_list(param):
    q_list = []
    if 'q' in param:
        q_param = param['q'].strip()
    else:
        q_param = None
    use_dismax = False
    if q_param:
        if q_param == '*:*':
            q_list.append(q_param)
        elif 'NOT ' in q_param: # this is a hack
            q_list.append(q_param.strip())
        elif re_fields.search(q_param):
            q_list.extend(i['op'] if 'op' in i else '%s:(%s)' % (i['field'], i['value']) for i in parse_query_fields(q_param))
        else:
            isbn = read_isbn(q_param)
            if isbn:
                q_list.append('isbn:(%s)' % isbn)
            else:
                q_list.append(q_param.strip().replace(':', '\:'))
                use_dismax = True
    else:
        if 'author' in param:
            v = param['author'].strip()
            m = re_author_key.search(v)
            if m: # FIXME: 'OL123A OR OL234A'
                q_list.append('author_key:(' + m.group(1) + ')')
            else:
                v = re_to_esc.sub(lambda m:'\\' + m.group(), v)
                q_list.append('(author_name:(' + v + ') OR author_alternative_name:(' + v + '))')

        check_params = ['title', 'publisher', 'isbn', 'oclc', 'lccn', 'contribtor', 'subject', 'place', 'person', 'time']
        q_list += ['%s:(%s)' % (k, param[k]) for k in check_params if k in param]
    return (q_list, use_dismax)
Beispiel #5
0
def build_q_list(param):
    q_list = []
    if 'q' in param:
        q_param = param['q'].strip()
    else:
        q_param = None
    use_dismax = False
    if q_param:
        if q_param == '*:*':
            q_list.append(q_param)
        elif 'NOT ' in q_param:  # this is a hack
            q_list.append(q_param.strip())
        elif re_fields.search(q_param):
            q_list.extend(i['op'] if 'op' in i else '%s:(%s)' %
                          (i['field'], i['value'])
                          for i in parse_query_fields(q_param))
        else:
            isbn = read_isbn(q_param)
            if isbn:
                q_list.append('isbn:(%s)' % isbn)
            else:
                q_list.append(q_param.strip().replace(':', '\:'))
                use_dismax = True
    else:
        if 'author' in param:
            v = param['author'].strip()
            m = re_author_key.search(v)
            if m:  # FIXME: 'OL123A OR OL234A'
                q_list.append('author_key:(' + m.group(1) + ')')
            else:
                v = re_to_esc.sub(lambda m: '\\' + m.group(), v)
                q_list.append('(author_name:(' + v +
                              ') OR author_alternative_name:(' + v + '))')

        check_params = [
            'title', 'publisher', 'oclc', 'lccn', 'contribtor', 'subject',
            'place', 'person', 'time'
        ]
        q_list += [
            '%s:(%s)' % (k, param[k]) for k in check_params if k in param
        ]
        if param.get('isbn'):
            q_list.append('isbn:(%s)' %
                          (read_isbn(param['isbn']) or param['isbn']))
    return (q_list, use_dismax)
Beispiel #6
0
def test_isbn():
    assert read_isbn('x') is None
    assert read_isbn('1841151866') == '1841151866'
    assert read_isbn('184115186x') == '184115186x'
    assert read_isbn('184115186X') == '184115186X'
    assert read_isbn('184-115-1866') == '1841151866'
    assert read_isbn('9781841151861') == '9781841151861'
    assert read_isbn('978-1841151861') == '9781841151861'
Beispiel #7
0
def test_isbn():
    assert read_isbn("x") is None
    assert read_isbn("1841151866") == "1841151866"
    assert read_isbn("184115186x") == "184115186x"
    assert read_isbn("184115186X") == "184115186X"
    assert read_isbn("184-115-1866") == "1841151866"
    assert read_isbn("9781841151861") == "9781841151861"
    assert read_isbn("978-1841151861") == "9781841151861"
Beispiel #8
0
def test_isbn():
    assert read_isbn('x') is None
    assert read_isbn('1841151866') == '1841151866'
    assert read_isbn('184115186x') == '184115186x'
    assert read_isbn('184115186X') == '184115186X'
    assert read_isbn('184-115-1866') == '1841151866'
    assert read_isbn('9781841151861') == '9781841151861'
    assert read_isbn('978-1841151861') == '9781841151861'
Beispiel #9
0
 def isbn_redirect(self, isbn_param):
     isbn = read_isbn(isbn_param)
     if not isbn:
         return
     editions = []
     for f in 'isbn_10', 'isbn_13':
         q = {'type': '/type/edition', f: isbn}
         editions += web.ctx.site.things(q)
     if len(editions) == 1:
         raise web.seeother(editions[0])
Beispiel #10
0
 def isbn_redirect(self, isbn_param):
     isbn = read_isbn(isbn_param)
     if not isbn:
         return
     editions = []
     for f in 'isbn_10', 'isbn_13':
         q = {'type': '/type/edition', f: isbn}
         editions += web.ctx.site.things(q)
     if len(editions) == 1:
         raise web.seeother(editions[0])
Beispiel #11
0
def build_q_list(param):
    q_list = []
    if 'q' in param:
        q_param = param['q'].strip()
    else:
        q_param = None
    use_dismax = False
    if q_param:
        if q_param == '*:*':
            q_list.append(q_param)
        elif 'NOT ' in q_param: # this is a hack
            q_list.append(q_param.strip())
        elif re_fields.search(q_param):
            q_list.extend(i['op'] if 'op' in i else '%s:(%s)' % (i['field'], i['value']) for i in parse_query_fields(q_param))
        else:
            isbn = read_isbn(q_param)
            if isbn:
                q_list.append('isbn:(%s)' % isbn)
            else:
                q_list.append(q_param.strip().replace(':', '\:'))
                use_dismax = True
    else:
        if 'author' in param:
            v = param['author'].strip()
            m = re_author_key.search(v)
            if m:
                q_list.append("author_key:(%s)" % m.group(1))
            else:
                v = re_to_esc.sub(lambda m:'\\' + m.group(), v)
                # Somehow v can be empty at this point,
                #   passing the following with empty strings causes a severe error in SOLR
                if v:
                    q_list.append("(author_name:(%(name)s) OR author_alternative_name:(%(name)s))" % {'name': v})

        check_params = ['title', 'publisher', 'oclc', 'lccn', 'contribtor', 'subject', 'place', 'person', 'time']
        q_list += ['%s:(%s)' % (k, param[k]) for k in check_params if k in param]
        if param.get('isbn'):
            q_list.append('isbn:(%s)' % (read_isbn(param['isbn']) or param['isbn']))
    return (q_list, use_dismax)