Skip to content

Commit

Permalink
serialization: fix again caching behavior
Browse files Browse the repository at this point in the history
* Forces the reset of the cache if a serializer use this behavior.
* Refactoring document facet filtering/building.

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Aug 25, 2022
1 parent 09fd796 commit 2ded21d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
58 changes: 22 additions & 36 deletions rero_ils/modules/documents/serializers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,46 +162,32 @@ def _get_location_pids(lib_pid):

for org in aggr_org:
# filter libraries by organisation
records = LibrariesSearch() \
.get_libraries_by_organisation_pid(
org['key'], ['pid', 'name'])
org_library = [{record.pid: record.name}
for record in records]
org_library_pids = list(set().union(*(lib.keys()
for lib in org_library)))

org['library']['buckets'] = [library for library
in org['library']['buckets']
if library['key'] in
org_library_pids]

for library in org['library']['buckets']:
for lib in org_library:
if library['key'] in lib:
library['name'] = lib[library['key']]
break
# Keep only libraries for the current selected organisation.
query = LibrariesSearch() \
.filter('term', organisation__pid=org['key'])\
.source(['pid', 'name'])
org_libraries = {hit.pid: hit.name for hit in query.scan()}
org['library']['buckets'] = list(filter(
lambda l: l['key'] in org_libraries,
org['library']['buckets']
))
for term in org['library']['buckets']:
if term['key'] in org_libraries:
term['name'] = org_libraries[term['key']]

# filter locations by library
for library in org['library']['buckets']:
locations = LocationsSearch() \
query = LocationsSearch() \
.filter('term', library__pid=library['key'])\
.source(['pid', 'name']).scan()
library_location = [{location.pid: location.name}
for location in locations]
library_location_pids = \
list(set()
.union(*(loc.keys()
for loc in library_location)))
library['location']['buckets'] = \
[location for location
in library['location']['buckets']
if location['key'] in library_location_pids]

for location in library['location']['buckets']:
for loc in library_location:
if location['key'] in loc:
location['name'] = loc[location['key']]
break
.source(['pid', 'name'])
lib_locations = {hit.pid: hit.name for hit in query.scan()}
library['location']['buckets'] = list(filter(
lambda l: l['key'] in lib_locations,
library['location']['buckets']
))
for term in library['location']['buckets']:
if term['key'] in lib_locations:
term['name'] = lib_locations[term['key']]

# Complete Organisation aggregation information
# with corresponding resource name
Expand Down
4 changes: 4 additions & 0 deletions rero_ils/modules/serializers/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def __init__(self, limit=2000):
self._resources = {}
self._limit = limit

def reset(self):
"""Resetting the cache."""
self._resources.clear()

def append(self, key, resource):
"""Append a resource into the cache.
Expand Down
8 changes: 5 additions & 3 deletions rero_ils/modules/serializers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"""
from __future__ import absolute_import, print_function

from copy import deepcopy
from datetime import datetime

from flask import current_app
Expand Down Expand Up @@ -51,9 +50,12 @@ def search_responsify(serializer, mimetype):
"""
def view(pid_fetcher, search_result, code=200, headers=None, links=None,
item_links_factory=None):
copy_serializer = deepcopy(serializer)
# Check if the serializer implement a 'reset' function. If yes, then
# call this function before perform serialization.
if (reset := getattr(serializer, 'reset', None)) and callable(reset):
reset()
response = current_app.response_class(
copy_serializer.serialize_search(
serializer.serialize_search(
pid_fetcher, search_result,
links=links, item_links_factory=item_links_factory),
mimetype=mimetype)
Expand Down
6 changes: 3 additions & 3 deletions tests/api/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ def test_cached_serializers(client, rero_json_header, item_lib_martigny,
# return is affected by changed.

# STEP#1 : first items search serialization
list_url = url_for('invenio_records_rest.item_list')
item = item_lib_martigny
list_url = url_for('invenio_records_rest.item_list', q=item.pid)
response = client.get(list_url, headers=rero_json_header)
assert response.status_code == 200

Expand All @@ -461,8 +462,7 @@ def test_cached_serializers(client, rero_json_header, item_lib_martigny,
location['name'] = 'new location name'
location = location.update(location, dbcommit=True, reindex=True)
flush_index(LocationsSearch.Meta.index)
from rero_ils.modules.locations.api import Location
assert Location.get_record_by_pid(location.pid).get('name') == \
assert LocationsSearch().get_record_by_pid(location.pid)['name'] == \
location.get('name')

# STEP#3 : second items search serialization
Expand Down

0 comments on commit 2ded21d

Please sign in to comment.