Esempio n. 1
0
File: views.py Progetto: GaloC/gplib
 def transform_section(section):
     section = sanitize_lucene(section.strip())
     if section:
         section = '~ AND '.join(section.split(' '))
         section = section.strip(" AND ") + "~"
         return '%s:(%s)' % (field, section)
     else:
         return ''
Esempio n. 2
0
 def transform_section(section):
     section = sanitize_lucene(section.strip())
     if section:
         section = '~ AND '.join(section.split(' '))
         section = section.strip(" AND ") + "~"
         return '%s:(%s)' % (field, section)
     else:
         return ''
Esempio n. 3
0
def reference_complete(request):
    """
    Returns data for reference complete
    """

    query = request.GET.get("term")
    qfilter = sanitize_lucene(query)
    search_config = Config.get_or_create("search")
    # autoridades
    query = make_query(search_config, qfilter, True,
                       "05a721a33096563ec44d8da885fa1a30")

    show_config = {}

    result = []

    # Result Display Configuration
    for item in search_config.values.values():
        item_type = item['type']

        fields = OrderedDict()
        for field in item['fields']:
            if field['show']:
                more = field.get('more', False)
                fields[field['field']] = (field['name'], more)

        show_config[item_type] = fields

    try:
        docs = search('search/by_field',
                      q=query,
                      include_fields="022_a,020_a,urn,_id",
                      limit=133742)
        docs = list(docs)
    except RequestFailed:
        print "Fail!"
        print "QQ", query

    # group uniq docs by urn
    uniq_docs = {}

    for doc in docs:
        try:
            urn = doc['fields']['urn']
        except KeyError:
            print "Item should have urn"
            continue

        # TODO: check if should be a list
        if type(urn) is list:
            urn = urn[0]

        uniq_docs.setdefault(urn, {'count': 0, 'id': None})
        uniq_docs[urn]['id'] = doc['id']
        uniq_docs[urn]['count'] += 1

    db = get_db('couchflow')
    keys = [doc['id'] for doc in uniq_docs.values()]

    def _get_field(field, doc):
        subfield = ""
        if '_' in field:
            field, subfield = field.split('_')

        #field_value = doc.fields_properties.get(field, None)
        try:
            field_value = doc["fields_properties"][field]
        except KeyError:
            field_value = ""

        if field_value and len(field_value['list']):
            field_value = field_value['list'][0]
            if subfield:
                field_value = field_value['subfields'].get(subfield, "")

            if field_value and 'exec_value' in field_value and field_value[
                    'exec_value']:
                exec_val = field_value['exec_value'][0]
                if not isinstance(exec_val, basestring):
                    if exec_val == None:
                        exec_val = ""
                    exec_val = str(exec_val)
                return exec_val
        return ""

    for doc in db.view('_all_docs', keys=keys, include_docs=True):
        #doc = WFItem.wrap(doc['doc'])
        doc = doc["doc"]

        show_item_config = show_config.get(doc['item_type'], None)
        if not show_item_config:
            print 'Unknown', doc['item_type']
            continue

        field = _get_field('700_a', doc)
        result.append({'label': field})
        #data = ''
        #for field, (name, more) in show_item_config.iteritems():
        #    row_value = '%s: %s' % (name, _get_field(field, doc))

        #    row_value = row_value.replace('/', '').replace(',', '')
        #    data += '<div>%s</div>' %  row_value

        #doc_urn = get_urn(doc)
        #if not doc_urn:
        #    print "Invalid Item, need a urn", doc["_id"]
        #    continue

        #result.append(data)

    return HttpResponse(simplejson.dumps(result),\
                    mimetype='application/javascript')
Esempio n. 4
0
def make_advanced_query(request, newsearch_fields):
    query = []
    query_greenstone = ''

    item_type = sanitize_lucene(request.POST.get('item_type'))
    if item_type:
        query.append("item_type:%s" % item_type)

    searchfields_by_type = {}
    for field_id, field_name, field_item_type, field_type in newsearch_fields:
        searchfields_by_type.setdefault(field_item_type, [])
        searchfields_by_type[field_item_type].append(
            (field_id, field_name, field_type))

    if '_ft' in searchfields_by_type:
        ft_field_list = searchfields_by_type.pop('_ft')
        for key in searchfields_by_type:
            searchfields_by_type[key] += ft_field_list

    def transform_field_query(field, value):
        matches = list(re.finditer("(AND|OR|NOT)", value))

        def transform_section(section):
            section = sanitize_lucene(section.strip())
            if section:
                section = '~ AND '.join(section.split(' '))
                section = section.strip(" AND ") + "~"
                return '%s:(%s)' % (field, section)
            else:
                return ''

        output = []
        last_end = 0

        for match in matches:
            output.append(transform_section(value[last_end:match.start()]))
            output.append(match.group(1))
            last_end = match.end()

        output.append(transform_section(value[last_end:]))

        return ' '.join(output)

    fields_query = []
    for field_item_type, field_list in searchfields_by_type.iteritems():
        if item_type and field_item_type != item_type:
            continue

        field_list.sort(
            key=lambda x: request.POST.get("order_%s" % x[0], VERY_BIG_NUMBER))

        this_type_query = []
        first = True
        for field_id, field_name, field_type in field_list:
            value = request.POST.get(field_id, None)
            op = request.POST.get("operator_%s" % field_id, 'AND')

            if field_type == 'date':
                value = tuple([
                    sanitize_lucene(request.POST.get(field_id + x, None), '-')
                    for x in ("_from", "_to")
                ])

            if value:
                if field_type == 'date':
                    q = '%s<date>:[%s TO %s]' % ((field_id, ) + value)
                elif field_type == '_ft':
                    q = GREENSTONE_NEWSEARCH_PLACEHOLDER
                    query_greenstone = value
                else:
                    q = transform_field_query(field_id, value)

                if not first:
                    if op not in ('AND', 'OR', 'NOT'):
                        op = 'AND'
                    if op == 'NOT':
                        op = 'AND NOT'
                else:
                    if op != 'NOT':
                        op = ''

                this_type_query.append("%s (%s)" % (op, q))
                first = False

        if field_item_type is None:
            fields_query.append("(%s)" % " ".join(this_type_query))
        else:
            if this_type_query:
                fields_query.append(
                    "(item_type:%s AND (%s))" %
                    (field_item_type, " ".join(this_type_query)))

    query.append(' OR '.join(fields_query))

    full_query = ' AND '.join(query)
    #print full_query

    sort = ''
    if request.POST.get("sort", ''):
        sort = ('\\' if (request.POST.get("ascdesc", "asc") == 'desc') else
                '/') + request.POST.get("sort")
    #print sort
    return "%s||%s||%s" % (full_query, sort, query_greenstone)
Esempio n. 5
0
def data(request):
    """
    Returns data for datatables
    """
    user = request.user

    secho = int(request.POST.get('sEcho', 0)) + 1
    return_dict = {
        "aaData": [],
        "iTotalRecords": 0,
        "iTotalDisplayRecords": 0,
        "sEcho": secho
    }
    query = None
    sort = None
    sort_reverse = False
    qfilter = ''
    basic_greenstone = True
    search_config = Config.get_or_create("search")

    if 'raw_query' in request.POST:
        query, sort_raw, query_greenstone = \
            request.POST['raw_query'].split("||", 2)
        if sort_raw:
            sort = sort_raw[1:]
            sort_reverse = (sort_raw[0] == '\\')

        # make_advanced_query() doesn't handle greenstone filtering.
        # Instead, it leaves a placeholder that is replaced here.
        # This is to leave all the querying to the second request
        # (this one), and only lucene query building to the first.

        query = query.replace(
            GREENSTONE_NEWSEARCH_PLACEHOLDER, "(%s)" % (' OR '.join([
                "urn:%s" % x['nodeID']
                for x in greenstone_query("", "", query_greenstone)
            ])))
        basic_greenstone = False

    elif 'filter' in request.POST:
        qfilter = sanitize_lucene(request.POST['filter'])
        qfilter = request.POST['filter']
        reference = request.POST.get('reference', 0)
        filter_item_type = request.POST.get('item_type', None)

        query = make_query(search_config, qfilter, reference, filter_item_type)
    elif 'filtered' in request.POST:
        qfilter = sanitize_lucene(request.POST['filtered'])
        reference = request.POST.get('reference', 0)
        filter_item_type = request.POST.get('item_type', None)

        query = make_query(search_config, qfilter, reference, filter_item_type)

    if not query:
        print "search failed: query = %s" % query
        return HttpResponse(simplejson.dumps(return_dict),\
                    mimetype='application/javascript')

    show_config = {}

    # Result Display Configuration
    for item in search_config.values.values():
        item_type = item['type']

        fields = OrderedDict()
        for field in item['fields']:
            if field['show']:
                more = field.get('more', False)
                existence = field.get('exist', False)
                fields[field['field']] = (field['name'], more, existence)

        show_config[item_type] = fields

    try:
        docs = search('search/by_field',
                      q=query,
                      include_fields="022_a,020_a,urn,_id,existence",
                      limit=133742)
        docs = list(docs)
    except RequestFailed:
        print "search failed: request failed"
        return HttpResponse(simplejson.dumps(return_dict),\
                    mimetype='application/javascript')

    db = get_db('couchflow')

    # group uniq docs by urn
    uniq_docs = {}

    if basic_greenstone:
        greenstone_urns = [
            x['nodeID'] for x in greenstone_query("", "", qfilter)
        ]
        greenstone_docs = db.view("couchflow/by_urn", keys=greenstone_urns)

        for doc in greenstone_docs:
            urn = doc['key']
            uniq_docs.setdefault(urn, {
                'count': 0,
                'id': None,
                "existences": []
            })
            uniq_docs[urn]['id'] = doc['id']
            #uniq_docs[urn]['count'] += 1
            uniq_docs[urn]['greenstone'] = True

    for doc in docs:
        try:
            urn = doc['fields']['urn']
        except KeyError:
            urn = None

        if urn is None or urn == 'undefined':
            print "Item should have urn", doc['id']
            continue

        # TODO: check if should be a list
        if type(urn) is list:
            urn = urn[0]

        uniq_docs.setdefault(urn, {'count': 0, 'id': None, "existences": []})
        if doc['fields']['existence'] != "false":
            uniq_docs[urn]['existences'].append(doc['id'])
            uniq_docs[urn]['count'] += 1
        else:
            uniq_docs[urn]['id'] = doc['id']

    columns = []

    start = int(request.POST['iDisplayStart'])
    length = int(request.POST['iDisplayLength'])

    #sort_col = int(request.POST['iSortCol_0'])
    #sort_dir = request.POST['sSortDir_0']

    count = len([u for u in uniq_docs.values() if u["id"]])

    sorted_uniq_docs = uniq_docs.values()
    if basic_greenstone:
        sorted_uniq_docs.sort(key=lambda x: 'greenstone' not in x)

    keys = [doc['id'] for doc in sorted_uniq_docs[start:start + length]]
    keys_exist = [doc['existences']\
            for doc in sorted_uniq_docs[start:start+length]]
    keys_exist = [item for sublist in keys_exist for item in sublist]

    def _get_field(field, doc):
        subfield = ""
        if '_' in field:
            field, subfield = field.split('_')

        #field_value = doc.fields_properties.get(field, None)
        try:
            field_value = doc["fields_properties"][field]
        except KeyError:
            field_value = ""

        if field_value and len(field_value['list']):
            field_value = field_value['list'][0]
            if subfield:
                field_value = field_value['subfields'].get(subfield, "")

            if field_value and 'exec_value' in field_value and field_value[
                    'exec_value']:
                exec_val = field_value['exec_value'][0]
                if not isinstance(exec_val, basestring):
                    if exec_val == None:
                        exec_val = ""
                    exec_val = str(exec_val)
                return exec_val
        return ""

    # get existences
    existences = {}
    for doc in db.view('_all_docs', keys=keys_exist, include_docs=True):
        existences[doc["doc"]["_id"]] = doc["doc"]

    for doc in db.view('_all_docs', keys=keys, include_docs=True):
        #doc = WFItem.wrap(doc['doc'])
        if not "doc" in doc:
            continue
        doc = doc["doc"]

        show_item_config = show_config.get(doc['item_type'], None)
        if not show_item_config:
            print 'Search config missing for', doc['item_type']
            continue

        try:
            img_name = doc['fields_properties']['5000']['list'][0][
                'exec_value'][0]
        except Exception, error:
            print 'Image not found', error
            img_name = 'none.png'

        img_path = "/couchflow/get_attach/couchflow/%s/%s" % (doc['_id'],
                                                              img_name)
        row = [doc['_id'], '<img style="width:80px" src="%s"/>' % img_path]

        data = ''
        for field, (name, more, existence) in show_item_config.iteritems():
            if existence: continue
            field_value = _get_field(field, doc)
            if not field_value:
                continue
            row_value = '%s: %s' % (name, field_value)

            row_value = row_value.replace('/', '').replace(',', '')
            more_class = ' class="search_more"' if more else ''
            data += '<div%s>%s</div>' % (more_class, row_value)

        doc_urn = get_urn(doc)
        if not doc_urn:
            print "Invalid Item, need a urn", doc["_id"]
            continue

        if not doc['reference']:
            data += 'Disponibles: %s<br>' % uniq_docs[doc_urn]['count']

        if uniq_docs[doc_urn]["existences"]:
            data += "<br><h3 class='search_more'>Ejemplares</h3>"
        # Add Existences
        for e in uniq_docs[doc_urn]["existences"]:
            if existences.get(e, False):
                data += "<div id='%s' class='existence search_more'>" % e
                for field, (name, more, exist_conf) in\
                        show_item_config.iteritems():
                    if exist_conf:
                        field_value = _get_field(field, existences[e])
                        if not field_value:
                            field_value = ""

                        row_value = '%s: %s' % (name, field_value)
                        row_value = row_value.replace('/', '').replace(',', '')
                        more_class = ' class="search_more"' if more else ''
                        data += '<div%s>%s</div>' % (more_class, row_value)
                data += "</div>"

        row.append(data)
        row.append('')

        sort_value = None
        if sort:
            sort_value = _get_field(sort, doc)

        columns.append((sort_value, row))
Esempio n. 6
0
File: views.py Progetto: GaloC/gplib
def reference_complete(request):
    """
    Returns data for reference complete
    """

    query = request.GET.get("term")
    qfilter = sanitize_lucene(query)
    search_config = Config.get_or_create("search")
    # autoridades
    query = make_query(search_config, qfilter, True, "05a721a33096563ec44d8da885fa1a30")

    show_config = {}

    result = []

    # Result Display Configuration
    for item in search_config.values.values():
        item_type = item['type']

        fields = OrderedDict()
        for field in item['fields']:
            if field['show']:
                more = field.get('more', False)
                fields[field['field']] = (field['name'], more)

        show_config[item_type] = fields

    try:
        docs = search('search/by_field', q=query,
                include_fields="022_a,020_a,urn,_id",
                                  limit=133742)
        docs = list(docs)
    except RequestFailed:
        print "Fail!"
        print "QQ", query

    # group uniq docs by urn
    uniq_docs = {}

    for doc in docs:
        try:
            urn = doc['fields']['urn']
        except KeyError:
            print "Item should have urn"
            continue

        # TODO: check if should be a list
        if type(urn) is list:
            urn = urn[0]

        uniq_docs.setdefault(urn, {'count':0, 'id': None})
        uniq_docs[urn]['id'] = doc['id']
        uniq_docs[urn]['count'] += 1

    db = get_db('couchflow')
    keys = [doc['id'] for doc in uniq_docs.values()]

    def _get_field(field, doc):
        subfield = ""
        if '_' in field:
            field, subfield = field.split('_')

        #field_value = doc.fields_properties.get(field, None)
        try:
            field_value = doc["fields_properties"][field]
        except KeyError:
            field_value = ""

        if field_value and len(field_value['list']):
            field_value = field_value['list'][0]
            if subfield:
                field_value = field_value['subfields'].get(subfield, "")

            if field_value and 'exec_value' in field_value and field_value['exec_value']:
                exec_val = field_value['exec_value'][0]
                if not isinstance(exec_val, basestring):
                    if exec_val == None:
                        exec_val = ""
                    exec_val = str(exec_val)
                return exec_val
        return ""

    for doc in db.view('_all_docs', keys=keys, include_docs=True):
        #doc = WFItem.wrap(doc['doc'])
        doc = doc["doc"]

        show_item_config = show_config.get(doc['item_type'], None)
        if not show_item_config:
            print 'Unknown', doc['item_type']
            continue

        field = _get_field('700_a', doc)
        result.append({'label': field})
        #data = ''
        #for field, (name, more) in show_item_config.iteritems():
        #    row_value = '%s: %s' % (name, _get_field(field, doc))

        #    row_value = row_value.replace('/', '').replace(',', '')
        #    data += '<div>%s</div>' %  row_value

        #doc_urn = get_urn(doc)
        #if not doc_urn:
        #    print "Invalid Item, need a urn", doc["_id"]
        #    continue

        #result.append(data)

    return HttpResponse(simplejson.dumps(result),\
                    mimetype='application/javascript')
Esempio n. 7
0
File: views.py Progetto: GaloC/gplib
def make_advanced_query(request, newsearch_fields):
    query = []
    query_greenstone = ''

    item_type = sanitize_lucene(request.POST.get('item_type'))
    if item_type:
        query.append("item_type:%s" % item_type)

    searchfields_by_type = {}
    for field_id, field_name, field_item_type, field_type in newsearch_fields:
        searchfields_by_type.setdefault(field_item_type, [])
        searchfields_by_type[field_item_type].append((field_id, field_name,
            field_type))

    if '_ft' in searchfields_by_type:
        ft_field_list = searchfields_by_type.pop('_ft')
        for key in searchfields_by_type:
            searchfields_by_type[key] += ft_field_list

    def transform_field_query(field, value):
        matches = list(re.finditer("(AND|OR|NOT)", value))

        def transform_section(section):
            section = sanitize_lucene(section.strip())
            if section:
                section = '~ AND '.join(section.split(' '))
                section = section.strip(" AND ") + "~"
                return '%s:(%s)' % (field, section)
            else:
                return ''

        output = []
        last_end = 0

        for match in matches:
            output.append(transform_section(value[last_end:match.start()]))
            output.append(match.group(1))
            last_end = match.end()

        output.append(transform_section(value[last_end:]))

        return ' '.join(output)

    fields_query = []
    for field_item_type, field_list in searchfields_by_type.iteritems():
        if item_type and field_item_type != item_type:
            continue

        field_list.sort(key=lambda x: request.POST.get("order_%s" % x[0],
            VERY_BIG_NUMBER))

        this_type_query = []
        first = True
        for field_id, field_name, field_type in field_list:
            value = request.POST.get(field_id, None)
            op = request.POST.get("operator_%s" % field_id, 'AND')

            if field_type == 'date':
                value = tuple([sanitize_lucene(request.POST.get(
                    field_id + x, None), '-') for x in ("_from", "_to")])

            if value:
                if field_type == 'date':
                    q = '%s<date>:[%s TO %s]' % ((field_id,) + value)
                elif field_type == '_ft':
                    q = GREENSTONE_NEWSEARCH_PLACEHOLDER
                    query_greenstone = value
                else:
                    q = transform_field_query(field_id, value)

                if not first:
                    if op not in ('AND', 'OR', 'NOT'):
                        op = 'AND'
                    if op == 'NOT':
                        op = 'AND NOT'
                else:
                    if op != 'NOT':
                        op = ''

                this_type_query.append("%s (%s)" % (op, q))
                first = False

        if field_item_type is None:
            fields_query.append("(%s)" % " ".join(this_type_query))
        else:
            if this_type_query:
                fields_query.append("(item_type:%s AND (%s))" % (
                    field_item_type, " ".join(this_type_query)))

    query.append(' OR '.join(fields_query))

    full_query = ' AND '.join(query)
    #print full_query

    sort = ''
    if request.POST.get("sort", ''):
        sort = ('\\' if (request.POST.get("ascdesc", "asc") == 'desc')
            else '/') + request.POST.get("sort")
    #print sort
    return "%s||%s||%s" % (full_query, sort, query_greenstone)
Esempio n. 8
0
File: views.py Progetto: GaloC/gplib
def data(request):
    """
    Returns data for datatables
    """
    user = request.user

    secho = int(request.POST.get('sEcho', 0)) + 1
    return_dict = {"aaData": [], "iTotalRecords":0,
            "iTotalDisplayRecords":0, "sEcho": secho}
    query = None
    sort = None
    sort_reverse = False
    qfilter = ''
    basic_greenstone = True
    search_config = Config.get_or_create("search")

    if 'raw_query' in request.POST:
        query, sort_raw, query_greenstone = \
            request.POST['raw_query'].split("||", 2)
        if sort_raw:
            sort = sort_raw[1:]
            sort_reverse = (sort_raw[0] == '\\')

        # make_advanced_query() doesn't handle greenstone filtering.
        # Instead, it leaves a placeholder that is replaced here.
        # This is to leave all the querying to the second request
        # (this one), and only lucene query building to the first.

        query = query.replace(GREENSTONE_NEWSEARCH_PLACEHOLDER,
            "(%s)" % (' OR '.join(["urn:%s" % x['nodeID'] for x in
            greenstone_query("", "", query_greenstone)])))
        basic_greenstone = False

    elif 'filter' in request.POST:
        qfilter = sanitize_lucene(request.POST['filter'])
        qfilter = request.POST['filter']
        reference = request.POST.get('reference', 0)
        filter_item_type = request.POST.get('item_type', None)

        query = make_query(search_config, qfilter, reference, filter_item_type)
    elif 'filtered' in request.POST:
        qfilter = sanitize_lucene(request.POST['filtered'])
        reference = request.POST.get('reference', 0)
        filter_item_type = request.POST.get('item_type', None)

        query = make_query(search_config, qfilter, reference, filter_item_type)

    if not query:
        print "search failed: query = %s" % query
        return HttpResponse(simplejson.dumps(return_dict),\
                    mimetype='application/javascript')

    show_config = {}

    # Result Display Configuration
    for item in search_config.values.values():
        item_type = item['type']

        fields = OrderedDict()
        for field in item['fields']:
            if field['show']:
                more = field.get('more', False)
                existence = field.get('exist', False)
                fields[field['field']] = (field['name'], more, existence)

        show_config[item_type] = fields

    try:
        docs = search('search/by_field', q=query,
                include_fields="022_a,020_a,urn,_id,existence",
                                  limit=133742)
        docs = list(docs)
    except RequestFailed:
        print "search failed: request failed"
        return HttpResponse(simplejson.dumps(return_dict),\
                    mimetype='application/javascript')

    db = get_db('couchflow')

    # group uniq docs by urn
    uniq_docs = {}

    if basic_greenstone:
        greenstone_urns = [x['nodeID'] for x in greenstone_query("", "", qfilter)]
        greenstone_docs = db.view("couchflow/by_urn", keys=greenstone_urns)

        for doc in greenstone_docs:
            urn = doc['key']
            uniq_docs.setdefault(urn, {'count':0, 'id': None, "existences": []})
            uniq_docs[urn]['id'] = doc['id']
            #uniq_docs[urn]['count'] += 1
            uniq_docs[urn]['greenstone'] = True

    for doc in docs:
        try:
            urn = doc['fields']['urn']
        except KeyError:
            urn = None

        if urn is None or urn == 'undefined':
            print "Item should have urn", doc['id']
            continue

        # TODO: check if should be a list
        if type(urn) is list:
            urn = urn[0]

        uniq_docs.setdefault(urn, {'count':0, 'id': None, "existences": []})
        if doc['fields']['existence'] != "false":
            uniq_docs[urn]['existences'].append(doc['id'])
            uniq_docs[urn]['count'] += 1
        else:
            uniq_docs[urn]['id'] = doc['id']

    columns = []

    start = int(request.POST['iDisplayStart'])
    length = int(request.POST['iDisplayLength'])

    #sort_col = int(request.POST['iSortCol_0'])
    #sort_dir = request.POST['sSortDir_0']

    count = len([u for u in uniq_docs.values() if u["id"]])

    sorted_uniq_docs = uniq_docs.values()
    if basic_greenstone:
        sorted_uniq_docs.sort(key=lambda x: 'greenstone' not in x)

    keys = [doc['id'] for doc in sorted_uniq_docs[start:start+length]]
    keys_exist = [doc['existences']\
            for doc in sorted_uniq_docs[start:start+length]]
    keys_exist = [item for sublist in keys_exist for item in sublist]

    def _get_field(field, doc):
        subfield = ""
        if '_' in field:
            field, subfield = field.split('_')

        #field_value = doc.fields_properties.get(field, None)
        try:
            field_value = doc["fields_properties"][field]
        except KeyError:
            field_value = ""

        if field_value and len(field_value['list']):
            field_value = field_value['list'][0]
            if subfield:
                field_value = field_value['subfields'].get(subfield, "")

            if field_value and 'exec_value' in field_value and field_value['exec_value']:
                exec_val = field_value['exec_value'][0]
                if not isinstance(exec_val, basestring):
                    if exec_val == None:
                        exec_val = ""
                    exec_val = str(exec_val)
                return exec_val
        return ""

    # get existences
    existences = {}
    for doc in db.view('_all_docs', keys=keys_exist, include_docs=True):
        existences[doc["doc"]["_id"]] = doc["doc"]

    for doc in db.view('_all_docs', keys=keys, include_docs=True):
        #doc = WFItem.wrap(doc['doc'])
        if not "doc" in doc:
            continue
        doc = doc["doc"]

        show_item_config = show_config.get(doc['item_type'], None)
        if not show_item_config:
            print 'Search config missing for', doc['item_type']
            continue

        try:
            img_name = doc['fields_properties']['5000']['list'][0]['exec_value'][0]
        except Exception, error:
            print 'Image not found', error
            img_name = 'none.png'

        img_path = "/couchflow/get_attach/couchflow/%s/%s" % (doc['_id'], img_name)
        row = [doc['_id'], '<img style="width:80px" src="%s"/>' % img_path]

        data = ''
        for field, (name, more, existence) in show_item_config.iteritems():
            if existence: continue
            field_value = _get_field(field, doc)
            if not field_value:
                continue
            row_value = '%s: %s' % (name, field_value)

            row_value = row_value.replace('/', '').replace(',', '')
            more_class = ' class="search_more"' if more else ''
            data += '<div%s>%s</div>' % (more_class, row_value)

        doc_urn = get_urn(doc)
        if not doc_urn:
            print "Invalid Item, need a urn", doc["_id"]
            continue

        if not doc['reference']:
            data += 'Disponibles: %s<br>' % uniq_docs[doc_urn]['count']

        if uniq_docs[doc_urn]["existences"]:
            data += "<br><h3 class='search_more'>Ejemplares</h3>"
        # Add Existences
        for e in uniq_docs[doc_urn]["existences"]:
            if existences.get(e, False):
                data += "<div id='%s' class='existence search_more'>" % e
                for field, (name, more, exist_conf) in\
                        show_item_config.iteritems():
                    if exist_conf:
                        field_value = _get_field(field, existences[e])
                        if not field_value:
                            field_value = ""

                        row_value = '%s: %s' % (name, field_value)
                        row_value = row_value.replace('/', '').replace(',', '')
                        more_class = ' class="search_more"' if more else ''
                        data += '<div%s>%s</div>' % (more_class, row_value)
                data += "</div>"

        row.append(data)
        row.append('')

        sort_value = None
        if sort:
            sort_value = _get_field(sort, doc)

        columns.append((sort_value, row))