Introduce a custom vocabulary function for archive unit references
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 25 Aug 2016 11:33:59 +0200
changeset 1567 0bc7c97416c0
parent 1566 5c2023bb6e0c
child 1568 2121a3e866ef
Introduce a custom vocabulary function for archive unit references so that on creation, we don't get every archive unit in the database but only those in the parent's container.
views/archiveunit.py
--- a/views/archiveunit.py	Fri Aug 26 11:42:20 2016 +0200
+++ b/views/archiveunit.py	Thu Aug 25 11:33:59 2016 +0200
@@ -24,7 +24,7 @@
 
 from cubes.compound import views as compound
 
-from cubes.seda.entities import simplified_profile
+from cubes.seda.entities import simplified_profile, parent_and_container
 from cubes.seda.entities.itree import parent_archive_unit
 from cubes.seda.views import (add_subobject_link, add_subobjects_button, dropdown_button,
                               rtags_from_rtype_role_targets, copy_rtag)
@@ -33,6 +33,7 @@
 
 
 afs = uicfg.autoform_section
+affk = uicfg.autoform_field_kwargs
 pvs = uicfg.primaryview_section
 rec = uicfg.reledit_ctrl
 
@@ -65,6 +66,22 @@
         return 0 if entity.content_sequence else 1
 
 
+def unit_ref_vocabulary(form, field):
+    """Form vocabulary function for archive unit references, necessary to get parent container while
+    the entity is being created.
+    """
+    req = form._cw
+    parent, container = parent_and_container(form.edited_entity)
+    assert container is not None
+    rset = req.execute('Any XID, X ORDERBY XID WHERE '
+                       'X is SEDAArchiveUnit, X id XID, X container R, R eid %(root)s',
+                       {'root': container.eid})
+    return [(label, str(eid)) for label, eid in rset]
+
+affk.tag_subject_of(('SEDAArchiveUnitRefId', 'seda_archive_unit_ref_id_to', '*'),
+                    {'choices': unit_ref_vocabulary})
+
+
 pvs.tag_subject_of(
     ('SEDAArchiveUnit', 'seda_alt_archive_unit_archive_unit_ref_id', '*'),
     'hidden')