Commit e3277814 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[ui] Move parent_and_container function from views to entities

We'll need that here soon!
parent 9a2147273199
......@@ -15,6 +15,8 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Custom logic (eg entities and adapters) for cubicweb-seda."""
import json
from cubes.compound.entities import IContainer, IContained
from cubes.seda import seda_profile_container_def
......@@ -24,6 +26,36 @@ from cubes.seda.xsd import XSDMMapping
XSDM_MAPPING = XSDMMapping('ArchiveTransfer')
def parent_and_container(entity):
"""Attempt to return the direct parent and container from the entity, handling case where entity
is being created and container information will be found through linkto or parent form
(partially supported)
"""
if entity.has_eid():
# entity is expected to be a contained entity, not the container itself
container = entity.cw_adapt_to('IContained').container
parent = entity.cw_adapt_to('IContained').parent
else:
# but parent entity, retrieved through linkto, may be the container itself or a
# contained entity
try:
parent_eid = int(entity._cw.form['__linkto'].split(':')[1])
except KeyError:
# ajax created form
try:
parent_eid = int(json.loads(entity._cw.form['arg'][0]))
except ValueError:
# unable to get parent eid for now :()
return None, None
parent = entity._cw.entity_from_eid(parent_eid)
icontainer = parent.cw_adapt_to('IContainer')
if icontainer is None:
container = parent.cw_adapt_to('IContained').container
else:
container = icontainer.container
return parent, container
def rule_type_from_etype(etype):
"""Return the rule type from an etype like SEDAAccessRule or SEDAAltAccessRulePreventInheritance
"""
......
......@@ -14,8 +14,6 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
from yams import BASE_TYPES
from cubicweb import tags, neg_role
......@@ -180,33 +178,3 @@ def dropdown_button(text, links):
w(u'</ul>')
w(u'</div>')
return data.getvalue()
def parent_and_container(entity):
"""Attempt to return the direct parent and container from the entity, handling case where entity
is being created and container information will be found through linkto or parent form
(partially supported)
"""
if entity.has_eid():
# entity is expected to be a contained entity, not the container itself
container = entity.cw_adapt_to('IContained').container
parent = entity.cw_adapt_to('IContained').parent
else:
# but parent entity, retrieved through linkto, may be the container itself or a
# contained entity
try:
parent_eid = int(entity._cw.form['__linkto'].split(':')[1])
except KeyError:
# ajax created form
try:
parent_eid = int(json.loads(entity._cw.form['arg'][0]))
except ValueError:
# unable to get parent eid for now :()
return None, None
parent = entity._cw.entity_from_eid(parent_eid)
icontainer = parent.cw_adapt_to('IContainer')
if icontainer is None:
container = parent.cw_adapt_to('IContained').container
else:
container = icontainer.container
return parent, container
......@@ -27,7 +27,8 @@ from cubes.relationwidget import views as rwdg
from cubes.skos.views import widgets as skos
from cubes.seda.xsd2yams import RDEF_CONSTRAINTS
from cubes.seda.views import rtags_from_xsd_element, add_subobject_link, parent_and_container
from cubes.seda.entities import parent_and_container
from cubes.seda.views import rtags_from_xsd_element, add_subobject_link
from cubes.seda.views import viewlib
from cubes.seda.views import uicfg as sedauicfg # noqa - ensure those rules are defined first
......
......@@ -21,8 +21,8 @@ from cubicweb.predicates import is_instance
from cubicweb.web import INTERNAL_FIELD_VALUE
from cubicweb.web.views import uicfg
from cubes.seda.entities import rule_type_from_etype
from cubes.seda.views import parent_and_container, viewlib
from cubes.seda.entities import parent_and_container, rule_type_from_etype
from cubes.seda.views import viewlib
from cubes.seda.views import uicfg as sedauicfg # noqa - ensure those rules are defined first
......
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