From c7157d5001d9a1f15bdc866492badccc10e4af62 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 30 Jun 2016 16:18:07 +0200 Subject: [PATCH] only decode ids for found positions merge document.view.get_positions into item.utils.get_positions add flag to decode id before looking up in ids Followup to 09ebbc9cc6ccf03d3b24c842042ac575749b3abc --- pandora/archive/views.py | 2 +- pandora/changelog/views.py | 4 ++-- pandora/document/views.py | 30 ++---------------------------- pandora/entity/views.py | 4 ++-- pandora/event/views.py | 4 ++-- pandora/item/utils.py | 7 +++++-- pandora/item/views.py | 2 +- pandora/log/views.py | 4 ++-- pandora/person/views.py | 4 ++-- pandora/place/views.py | 4 ++-- pandora/sequence/views.py | 2 +- pandora/title/views.py | 4 ++-- 12 files changed, 24 insertions(+), 47 deletions(-) diff --git a/pandora/archive/views.py b/pandora/archive/views.py index cb138ad3e..834f2fdb8 100644 --- a/pandora/archive/views.py +++ b/pandora/archive/views.py @@ -640,7 +640,7 @@ def findMedia(request, data): qs = _order_query(qs, query['sort']) response['data']['positions'] = {} - ids = [j['oshash'] for j in qs.values('oshash')] + ids = list(qs.values_list('oshash', flat=True)) response['data']['positions'] = utils.get_positions(ids, query['positions']) elif 'keys' in query: diff --git a/pandora/changelog/views.py b/pandora/changelog/views.py index 6695d8a88..226a7c3ec 100644 --- a/pandora/changelog/views.py +++ b/pandora/changelog/views.py @@ -75,8 +75,8 @@ def findChangeLogs(request, data): if qs.count() > 0: response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: - ids = [ox.toAZ(i.id) for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() return render_to_json_response(response) diff --git a/pandora/document/views.py b/pandora/document/views.py index ab739f62e..dc5a60ded 100644 --- a/pandora/document/views.py +++ b/pandora/document/views.py @@ -165,33 +165,6 @@ def parse_query(data, user): return query -def get_positions(qs, query_positions): - ''' - qs: a QuerySet - query_positions: a list of AZ ids - - TODO: merge this with item.utils.get_positions. The win is to fetch - only the integer IDs and convert the (smaller) set of query_positions to - ints, rather than fetch all keys for everything in qs (expected to be many - orders of magnitude larger), ignore most of it, and convert those ids to - strings. - - Returns: - { - i: index of i in qs - for i in query_positions - } - ''' - ids = list(qs.values_list('id', flat=True)) - ret = {} - for i in query_positions: - try: - ret[i] = ids.index(ox.fromAZ(i)) - except: - pass - return ret - - def findDocuments(request, data): ''' Finds documents for a given query @@ -225,7 +198,8 @@ def findDocuments(request, data): #FIXME: actually implement position requests response['data']['position'] = 0 elif 'positions' in data: - response['data']['positions'] = get_positions(qs, query['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: r = qs.aggregate( Sum('size') diff --git a/pandora/entity/views.py b/pandora/entity/views.py index c732d704f..432cdfad5 100644 --- a/pandora/entity/views.py +++ b/pandora/entity/views.py @@ -234,8 +234,8 @@ def findEntities(request, data): #FIXME: actually implement position requests response['data']['position'] = 0 elif 'positions' in data: - ids = [i.get_id() for i in qs] - response['data']['positions'] = utils.get_positions(ids, query['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() return render_to_json_response(response) diff --git a/pandora/event/views.py b/pandora/event/views.py index 4513196a9..4f8b7ec75 100644 --- a/pandora/event/views.py +++ b/pandora/event/views.py @@ -206,8 +206,8 @@ def findEvents(request, data): if qs.count() > 0: response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: - ids = [i.get_id() for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() diff --git a/pandora/item/utils.py b/pandora/item/utils.py index ba2721045..0d352fbe7 100644 --- a/pandora/item/utils.py +++ b/pandora/item/utils.py @@ -61,7 +61,7 @@ def sort_title(title): title = re.sub(u'[\'!¿¡,\.;\-"\:\*\[\]]', '', title) return title.strip() -def get_positions(ids, pos): +def get_positions(ids, pos, decode_id=False): ''' >>> get_positions([1,2,3,4], [2,4]) {2: 1, 4: 3} @@ -69,7 +69,10 @@ def get_positions(ids, pos): positions = {} for i in pos: try: - positions[i] = ids.index(i) + if decode_id: + positions[i] = ids.index(ox.fromAZ(i)) + else: + positions[i] = ids.index(i) except: pass return positions diff --git a/pandora/item/views.py b/pandora/item/views.py index d79b00772..0cc687f15 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -204,7 +204,7 @@ def find(request, data): response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in query: qs = _order_query(query['qs'], query['sort']) - ids = [j['public_id'] for j in qs.values('public_id')] + ids = list(qs.values_list('public_id', flat=True)) response['data']['positions'] = utils.get_positions(ids, query['positions']) elif 'keys' in query: response['data']['items'] = [] diff --git a/pandora/log/views.py b/pandora/log/views.py index 48592d948..e96e25d45 100644 --- a/pandora/log/views.py +++ b/pandora/log/views.py @@ -123,8 +123,8 @@ def findErrorLogs(request, data): if qs.count() > 0: response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: - ids = [ox.toAZ(i.id) for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() return render_to_json_response(response) diff --git a/pandora/person/views.py b/pandora/person/views.py index 044468e69..75962b8e3 100644 --- a/pandora/person/views.py +++ b/pandora/person/views.py @@ -133,8 +133,8 @@ def findNames(request, data): if qs.count() > 0: response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: - ids = [i.get_id() for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() return render_to_json_response(response) diff --git a/pandora/place/views.py b/pandora/place/views.py index 5070173a6..9c220e755 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -254,8 +254,8 @@ def findPlaces(request, data): if qs.count() > 0: response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: - ids = [i.get_id() for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() response['data']['area'] = qs.aggregate( diff --git a/pandora/sequence/views.py b/pandora/sequence/views.py index c873e1394..fffa9e95c 100644 --- a/pandora/sequence/views.py +++ b/pandora/sequence/views.py @@ -85,7 +85,7 @@ def findSequences(request, data): response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: qs = order_query(qs, query['sort']) - ids = [i['public_id'] for i in qs.values('public_id')] + ids = list(qs.values_list('public_id', flat=True)) response['data']['positions'] = utils.get_positions(ids, data['positions']) else: response['data']['items'] = qs.count() diff --git a/pandora/title/views.py b/pandora/title/views.py index 66d7c4e19..fc08e3ada 100644 --- a/pandora/title/views.py +++ b/pandora/title/views.py @@ -132,8 +132,8 @@ def findTitles(request, data): if qs.count() > 0: response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0] elif 'positions' in data: - ids = [i.get_id() for i in qs] - response['data']['positions'] = utils.get_positions(ids, data['positions']) + ids = list(qs.values_list('id', flat=True)) + response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True) else: response['data']['items'] = qs.count() return render_to_json_response(response)