def api_metadata(request, item_id):
    cache_key = '{}:{}'.format(item_id, 'metadata')
    metadata = cache.get(cache_key)
    if metadata is None:
        try:
            tc = TerrainClient('anonymous', '*****@*****.**')
            metadata = tc.get_metadata(item_id)
            cache.set(cache_key, metadata, CACHE_EXPIRATION)
        except HTTPError as e:
            logger.exception('Failed to retrieve metadata',
                             extra={'id': item_id})
            return HttpResponseBadRequest('Failed to retrieve metadata',
                                          content_type='application/json')
    return JsonResponse(metadata)
Beispiel #2
0
def api_metadata(request, item_id, download=False):
    cache_key = '{}:{}'.format(item_id, 'metadata')
    result = cache.get(cache_key)

    if result is None:
        try:
            tc = TerrainClient('anonymous', '*****@*****.**')
            metadata = tc.get_metadata(item_id)
            avus = metadata['avus'] + metadata['irods-avus']
            contributors = []

            readable_meta = {}
            for item in avus:  #get readable labels
                attr = item.get('attr')
                label = data_dictionary.get(attr, attr)
                value = item.get('value')

                my_dict = {}
                my_dict['attr'] = attr
                my_dict['label'] = label
                my_dict['value'] = value

                if (label not in readable_meta and label != 'Contributor Type'
                        and label != 'Contributor'):
                    readable_meta[label] = my_dict
                elif label == 'Contributor Type' or label == 'Contributor':
                    contributors.append(my_dict)
                elif label not in readable_meta:
                    readable_meta[label] = my_dict
                elif readable_meta[label]['value']:
                    readable_meta[label]['value'] += ', {}'.format(value)
                else:
                    readable_meta[label]['value'] = value

            sorted_meta = []
            meta_copy = copy.copy(readable_meta)

            for item in metadata_order:
                if not isinstance(item, str):
                    key = item['key']
                    value = item['value']
                    if key in meta_copy and value in meta_copy:
                        if 'Additional Label' in item \
                            and meta_copy[key]['value'] \
                            and meta_copy[value]['value']:
                            label = item['Additional Label']
                            display_value = meta_copy[key][
                                'value'] + ': ' + meta_copy[value]['value']
                        else:
                            label = meta_copy[key]['value']
                            display_value = meta_copy[value]['value']
                        sorted_meta.append({
                            'key': label,
                            'value': display_value
                        })
                        meta_copy.pop(key, None)
                        meta_copy.pop(value, None)
                elif item == 'Contributor':
                    for c in contributors:
                        sorted_meta.append({
                            'key': c['label'],
                            'value': c['value']
                        })
                elif item in meta_copy:
                    key = meta_copy[item]['label']
                    value = meta_copy[item]['value']
                    sorted_meta.append({'key': key, 'value': value})
                    meta_copy.pop(key, None)

            for others in meta_copy:
                sorted_meta.append({
                    'key': readable_meta[others]['label'],
                    'value': readable_meta[others]['value']
                })

            result = {'sorted_meta': sorted_meta, 'metadata': readable_meta}
            cache.set(cache_key, result, CACHE_EXPIRATION)

        except HTTPError as e:
            logger.exception('Failed to retrieve metadata',
                             extra={'id': item_id})
            return HttpResponseBadRequest('Failed to retrieve metadata',
                                          content_type='application/json')
    return JsonResponse(result, safe=False)
def api_metadata(request, item_id, download=False):
    cache_key = '{}:{}'.format(item_id, 'metadata')
    result = cache.get(cache_key)

    if result is None:
        try:
            tc = TerrainClient('anonymous', '*****@*****.**')
            metadata = tc.get_metadata(item_id)
            avus = metadata['avus']+metadata['irods-avus']
            contributors = []

            readable_meta={}
            for item in avus: #get readable labels
                attr = item.get('attr')
                label = data_dictionary.get(attr, attr)
                value = item.get('value')

                my_dict = {}
                my_dict['attr'] = attr
                my_dict['label'] = label
                my_dict['value'] = value

                if (label not in readable_meta
                    and label != 'Contributor Type'
                    and label != 'Contributor'):
                    readable_meta[label] = my_dict
                elif label == 'Contributor Type' or label == 'Contributor':
                    contributors.append(my_dict)
                elif label not in readable_meta:
                    readable_meta[label] = my_dict
                elif readable_meta[label]['value']:
                    readable_meta[label]['value'] += ', {}'.format(value)
                else:
                    readable_meta[label]['value'] = value

            sorted_meta = []
            meta_copy = copy.copy(readable_meta)

            for item in metadata_order:
                if not isinstance(item, str):
                    key = item['key']
                    value = item['value']
                    if key in meta_copy and value in meta_copy:
                        if 'Additional Label' in item \
                            and meta_copy[key]['value'] \
                            and meta_copy[value]['value']:
                            label = item['Additional Label']
                            display_value = meta_copy[key]['value'] + ': ' + meta_copy[value]['value']
                        else:
                            label = meta_copy[key]['value']
                            display_value = meta_copy[value]['value']
                        sorted_meta.append({'key':label, 'value': display_value})
                        meta_copy.pop(key, None)
                        meta_copy.pop(value, None)
                elif item == 'Contributor':
                    for c in contributors:
                        sorted_meta.append({'key': c['label'], 'value': c['value']})
                elif item in meta_copy:
                    key = meta_copy[item]['label']
                    value = meta_copy[item]['value']
                    sorted_meta.append({'key':key, 'value':value})
                    meta_copy.pop(key, None)

            for others in meta_copy:
                sorted_meta.append({'key':readable_meta[others]['label'], 'value':readable_meta[others]['value']})

            result = {'sorted_meta': sorted_meta, 'metadata': readable_meta}
            cache.set(cache_key, result, CACHE_EXPIRATION)

        except HTTPError as e:
            logger.exception('Failed to retrieve metadata', extra={'id': item_id})
            return HttpResponseBadRequest('Failed to retrieve metadata',
                                          content_type='application/json')
    return JsonResponse(result, safe=False)