Commit 242269ea authored by Yann Voté's avatar Yann Voté
Browse files

[hooks] Create a content when an empty archive unit is created_updated

parent 1ab54f2a2801
......@@ -20,6 +20,7 @@ from collections import defaultdict
from yams import ValidationError
from yams.schema import role_name
from cubicweb.predicates import is_instance
from cubicweb.server import hook
from cubes.seda.entities import rule_type_from_etype
......@@ -104,6 +105,40 @@ class CheckRefNonRuleIdCodeListOp(hook.LateOperation):
{role_name('seda_ref_non_rule_id_to', 'subject'): msg})
class EnsureArchiveUnitHasAContentOp(hook.DataOperationMixIn, hook.Operation):
"""Make sure that a SEDAArchiveUnit has at least one related SEDAContent."""
def precommit_event(self):
for entity in self.get_data():
rset = entity.related('seda_alt_archive_unit_archive_unit_ref_id', role='subject')
if rset:
choice = rset.one()
else:
choice = self.cnx.create_entity(
'SEDAAltArchiveUnitArchiveUnitRefId',
reverse_seda_alt_archive_unit_archive_unit_ref_id=entity)
seq = choice.content_sequence
if not seq:
seq = choice = self.cnx.create_entity(
'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=choice)
if seq.reverse_seda_content:
continue
content = self.cnx.create_entity('SEDAContent', seda_content=seq)
self.cnx.create_entity('SEDATitle', seda_title=content)
self.cnx.create_entity('SEDACustodialHistoryItem', seda_custodial_history_item=content)
class EnsureArchiveUnitHasAContentHook(hook.Hook):
"""Make sure that a SEDAArchiveUnit has at least one related SEDAContent."""
__regid__ = 'seda.archive-unit.ensure-content'
__select__ = hook.Hook.__select__ & is_instance('SEDAArchiveUnit')
events = ('before_add_entity', 'before_update_entity')
def __call__(self):
EnsureArchiveUnitHasAContentOp.get_instance(self._cw).add_data(self.entity)
def registration_callback(vreg):
vreg.register_all(globals().values(), __name__)
from cubes.seda.entities import seda_profile_container_def
......
......@@ -58,6 +58,51 @@ class ValidationHooksTC(CubicWebTC):
cnx.commit()
class ArchiveUnitContentTC(CubicWebTC):
"""Test case checking hook creating SEDAContent after SEDAArchiveUnit."""
def test_content_created(self):
"""Check that a SEDAContent entity is created when an empty ArchiveUnit is created."""
with self.admin_access.client_cnx() as cnx:
# Create an empty SEDAArchiveUnit
transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
au = cnx.create_entity('SEDAArchiveUnit', id=u'au1', seda_archive_unit=transfer)
alt = cnx.create_entity('SEDAAltArchiveUnitArchiveUnitRefId',
reverse_seda_alt_archive_unit_archive_unit_ref_id=au)
cnx.create_entity('SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=alt)
cnx.commit()
au = cnx.entity_from_eid(au.eid)
# Check that a SEDAContent has been created
self.assertTrue(au.first_level_choice.content_sequence.reverse_seda_content)
# A title and a custodial history item have also been created
content = au.first_level_choice.content_sequence.reverse_seda_content[0]
self.assertTrue(content.reverse_seda_title)
self.assertTrue(content.reverse_seda_custodial_history_item)
def test_no_new_content_create(self):
"""Check that a SEDAContent entity is not created when an ArchiveUnit with one is created.
"""
with self.admin_access.client_cnx() as cnx:
# Create an SEDAArchiveUnit with a SEDAContent
transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
au = cnx.create_entity('SEDAArchiveUnit', id=u'au1', seda_archive_unit=transfer)
alt = cnx.create_entity('SEDAAltArchiveUnitArchiveUnitRefId',
reverse_seda_alt_archive_unit_archive_unit_ref_id=au)
seq = cnx.create_entity(
'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=alt)
content = cnx.create_entity('SEDAContent', seda_content=seq)
cnx.create_entity('SEDATitle', seda_title=content)
cnx.create_entity('SEDACustodialHistoryItem', seda_custodial_history_item=content)
cnx.commit()
au = cnx.entity_from_eid(au.eid)
# Check that no new SEDAContent has been created
self.assertEqual(len(au.first_level_choice.content_sequence.reverse_seda_content), 1)
related_content = au.first_level_choice.content_sequence.reverse_seda_content[0]
self.assertEqual(related_content.eid, content.eid)
if __name__ == '__main__':
import unittest
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment