Skip to content

Commit

Permalink
entity: remove type key from linked entity json schema.
Browse files Browse the repository at this point in the history
The `type` key is derivate from linked entity during the reference
replacement process. It's no longer necessary to store this value into
the document metadata (and into JSON schema).

Co-Authored-by: Renaud Michotte <[email protected]>
  • Loading branch information
zannkukai committed Apr 4, 2023
1 parent a509f5a commit d0b33cc
Show file tree
Hide file tree
Showing 12 changed files with 1,412 additions and 2,840 deletions.
3,296 changes: 1,099 additions & 2,197 deletions data/documents_big.json

Large diffs are not rendered by default.

869 changes: 290 additions & 579 deletions data/documents_small.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions rero_ils/modules/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,13 @@ def index_contributions(self, bulk=False):
for contribution in self.get('contribution', []):
ref = contribution['entity'].get('$ref')
if not ref and (cont_pid := contribution['entity'].get('pid')):
print("OK...")
if bulk:
uid = Entity.get_id_by_pid(cont_pid)
contributions_ids.append(uid)
else:
contrib = Entity.get_record_by_pid(cont_pid)
contrib.reindex()
if contributions_ids:
print("ids are", contributions_ids)
EntitiesIndexer().bulk_index(contributions_ids)
process_bulk_queue.apply_async()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,9 @@
"$ref"
],
"required": [
"type",
"$ref"
],
"properties": {
"type": {
"title": "Type",
"type": "string",
"default": "bf:Person",
"form": {
"type": "selectWithSort",
"templateOptions": {
"options": [
{
"label": "bf:Person",
"value": "bf:Person"
},
{
"label": "bf:Organisation",
"value": "bf:Organisation"
}
]
}
}
},
"$ref": {
"title": "MEF Link",
"type": "string",
Expand Down
13 changes: 2 additions & 11 deletions rero_ils/modules/documents/utils_mef.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ def process(self):
changed = True
url = f'{self.mef_url}/{cont_type}/' \
f'{cont[cont_type]["pid"]}'
new_contribution = {
'$ref': url,
'type': contribution['entity']['type']
}
new_contribution = {'$ref': url}
self.print_debug(
f'{hit.pid} Change:',
f' {contribution["entity"]}',
Expand Down Expand Up @@ -299,7 +296,6 @@ def process(self):
if self.verbose:
click.echo(f'Found identifiedBy {self.name}: '
f'{self._query_filter().count()}')
hits = list(self._query_filter().source('pid').scan())
for hit in list(self._query_filter().source('pid').scan()):
with contextlib.suppress(NoResultFound):
doc = Document.get_record(hit.meta.id)
Expand All @@ -319,12 +315,7 @@ def process(self):
changed = True
url = f'{self.mef_url}/{cont_type}/' \
f'{cont[cont_type]["pid"]}'
new_subject = {
'$ref': url,
# TOTO: we have to correct all wrong
# bf:Organisation
'type': subject['type']
}
new_subject = {'$ref': url}
self.print_debug(
f'{hit.pid} Change:',
f' {subject}',
Expand Down
7 changes: 3 additions & 4 deletions tests/api/documents/test_documents_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ def test_document_boosting(client, ebook_1, ebook_4):

@mock.patch('requests.get')
def test_documents_resolve(
mock_contributions_mef_get, client, mef_agents_url,loc_public_martigny, document_ref,
entity_person_response_data
mock_contributions_mef_get, client, mef_agents_url, loc_public_martigny,
document_ref, entity_person_response_data
):
"""Test document detailed view with items filter."""
res = client.get(url_for(
Expand All @@ -500,8 +500,7 @@ def test_documents_resolve(
'entity': {
'$ref': f'{mef_agents_url}/rero/A017671081',
'pid': 'ent_pers',
'type': 'bf:Person'
},
},
'role': ['aut']
}]
assert res.status_code == 200
Expand Down
4 changes: 2 additions & 2 deletions tests/api/documents/test_documents_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ def test_replace_idby_subjects(mock_contributions_mef_get, app,

# clean up
doc.delete(dbcommit=True, delindex=True, force=True)
for id in Entity.get_all_ids():
cont = Entity.get_record(id)
for _id in Entity.get_all_ids():
cont = Entity.get_record(_id)
cont.delete(dbcommit=True, delindex=True, force=True)
4 changes: 1 addition & 3 deletions tests/data/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,6 @@
"contribution": [
{
"entity": {
"type": "bf:Person",
"$ref": "https://mef.rero.ch/api/agents/rero/A017671081"
},
"role": [
Expand Down Expand Up @@ -2626,8 +2625,7 @@
"contribution": [
{
"entity": {
"$ref": "https://mef.rero.ch/api/agents/idref/029314100",
"type": "bf:Person"
"$ref": "https://mef.rero.ch/api/agents/idref/029314100"
},
"role": [
"ctb"
Expand Down
5 changes: 1 addition & 4 deletions tests/data/documents.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"contribution": [
{
"entity": {
"type": "bf:Person",
"$ref": "https://mef.rero.ch/api/agents/idref/223977268"
},
"role": [
Expand All @@ -63,7 +62,6 @@
},
{
"entity": {
"type": "bf:Person",
"$ref": "https://mef.rero.ch/api/agents/rero/A017671081"
},
"role": [
Expand Down Expand Up @@ -172,7 +170,6 @@
"contribution": [
{
"entity": {
"type": "bf:Person",
"$ref": "https://mef.rero.ch/api/agents/idref/223977268"
},
"role": [
Expand Down Expand Up @@ -254,4 +251,4 @@
"encodingLevel": "Minimal level"
}
}
]
]
25 changes: 13 additions & 12 deletions tests/ui/documents/test_documents_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from rero_ils.modules.documents.models import DocumentIdentifier
from rero_ils.modules.ebooks.tasks import create_records
from rero_ils.modules.entities.api import EntitiesSearch, Entity
from rero_ils.modules.entities.models import EntityType
from rero_ils.modules.tasks import process_bulk_queue


Expand Down Expand Up @@ -334,21 +335,21 @@ def test_document_replace_refs(document, mef_agents_url):
data = document.replace_refs()
assert len(data.get('contribution')) == 1

contributions = document.get('contribution', [])

# add MEF contribution agent
contributions.append({
'entity': {
'type': 'bf:Person',
'$ref': f'{mef_agents_url}/rero/A017671081'
},
'role': [
'aut'
]
document['contribution'].append({
'entity': {'$ref': f'{mef_agents_url}/rero/A017671081'},
'role': ['aut']
})
document.update(document, True, True)

document.update(document, dbcommit=True, reindex=True)
flush_index(DocumentsSearch.Meta.index)
es_doc = DocumentsSearch().get_record_by_pid(document.pid).to_dict()
assert es_doc['contribution'][1]['entity']['type'] == EntityType.PERSON

data = document.replace_refs()
assert len(data.get('contribution')) == 2
assert 'entity' in data.get('contribution')[1]
assert data.get('contribution')[1].get('role') == ['aut']
document.update(orig, True, True)

# Reset fixtures
document.update(orig, dbcommit=True, reindex=True)
4 changes: 1 addition & 3 deletions tests/ui/entities/test_entities_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from rero_ils.modules.documents.api import Document, DocumentsSearch
from rero_ils.modules.entities.api import EntitiesSearch, Entity, \
entity_id_fetcher
from rero_ils.modules.entities.models import EntityType
from rero_ils.modules.entities.sync import SyncAgent


Expand Down Expand Up @@ -235,8 +234,7 @@ def test_entity_properties(
assert item.organisation_pid not in entity_person.organisation_pids
document['contribution'] = [{
'entity': {
'$ref': 'https://mef.rero.ch/api/agents/idref/223977268',
'type': EntityType.PERSON
'$ref': 'https://mef.rero.ch/api/agents/idref/223977268'
},
'role': ['cre']
}]
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/documents/test_documents_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,11 @@ def test_contribution(mef_agents_url, document_schema, document_data_tmp):
'role': ['aut']
}, {
'entity': {
'type': 'bf:Organisation',
'$ref': f'{mef_agents_url}/gnd/XXXXXXX'
},
'role': ['aut']
}, {
'entity': {
'type': 'bf:Person',
'$ref': f'{mef_agents_url}/gnd/XXXXXXX'
},
'role': ['aut']
Expand Down

0 comments on commit d0b33cc

Please sign in to comment.