dataobject.py 6.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# 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/>.
"""cubicweb-seda views for data objects (BinaryDataObject / PhysicalDataObject)"""

18
19
import json

20
21
from cubicweb.predicates import match_form_params, is_instance
from cubicweb.web.views import uicfg, tabs
22
23
24

from cubes.relationwidget import views as rwdg

25
26
from cubes.seda.xsd2yams import RDEF_CONSTRAINTS
from cubes.seda.views import rtags_from_xsd_element
27

28
29
_ = unicode

30
31
32
pvs = uicfg.primaryview_section
rec = uicfg.reledit_ctrl
afs = uicfg.autoform_section
33
34
affk = uicfg.autoform_field_kwargs

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

def _container_eid(entity):
    """Attempt to return the container eid from the entity, handling case where entity is being
    created and container information will be find 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
    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
        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 container.eid


class ContainedRelationFacetWidget(rwdg.RelationFacetWidget):

    def trigger_search_url(self, entity, url_params):
        """Overriden to add information about who is the container

        This information will be used later for proper vocabulary computation.
        """
        # first retrieve the container entity
        container_eid = _container_eid(entity)
        # and put it as an extra url param
        if container_eid is not None:
            url_params['container'] = unicode(container_eid)
        return super(ContainedRelationFacetWidget, self).trigger_search_url(entity, url_params)


class ContainedSearchForRelatedEntitiesView(rwdg.SearchForRelatedEntitiesView):
    __select__ = rwdg.SearchForRelatedEntitiesView.__select__ & match_form_params('container')

    def linkable_rset(self):
        """Return rset of entities to be displayed as possible values for the
        edited relation. You may want to override this.
        """
        entity = self.compute_entity()
        rtype, tetype, role = self.rdef
        rdef = entity.e_schema.rdef(rtype, role, tetype)
        assert len(rdef.constraints) == 1 and 'S container AT' in rdef.constraints[0].expression
        baserql = rdef.constraints[0].expression
        baserql.replace('S container AT', 'AT eid %(at)s')
        rql = 'Any O WHERE ' + baserql
        return self._cw.execute(rql, {'at': int(self._cw.form['container'])})


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# hide some relation from autoform because of limitation of _container_eid
pvs.tag_object_of(('*', 'seda_compressed', 'SEDABinaryDataObject'), 'attributes')
afs.tag_object_of(('*', 'seda_compressed', 'SEDABinaryDataObject'), 'main', 'hidden')
rec.tag_object_of(('*', 'seda_compressed', 'SEDABinaryDataObject'),
                  {'rvid': 'seda.reledit.complexlink',
                   'novalue_label': _('<unauthorized>')})

rec.tag_subject_of(('SEDABinaryDataObject', 'seda_alt_binary_data_object_attachment', '*'),
                   {'rvid': 'seda.reledit.alternative',
                    'novalue_label': _('<unauthorized>')})

for etype in ('SEDABinaryDataObject', 'SEDAPhysicalDataObject'):
    for rtype in ('seda_data_object_version_from', 'seda_relationship'):
        pvs.tag_object_of(('*', rtype, etype), 'attributes')
        afs.tag_object_of(('*', rtype, etype), 'main', 'hidden')

    rec.tag_object_of(('*', 'seda_data_object_version_from', etype),
                      {'rvid': 'seda.reledit.complexlink',
                       'novalue_label': _('<unauthorized>')})


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def uri_cardinality_vocabulary(form, field):
    req = form._cw
    try:
        # inlined creation form
        parent_type = json.loads(req.form['arg'][1])
    except KeyError:
        # edition through reledit
        parent_eid = req.form['eid']
        parent_type = req.describe(int(parent_eid))[0]
    if parent_type in ('SEDABinaryDataObject', 'SEDAAltBinaryDataObjectAttachment'):
        return [u'1']
    return [u'0..1', u'1']

affk.tag_attribute(('SEDAUri', 'user_cardinality'),
                   {'choices': uri_cardinality_vocabulary})


135
136
137
138
139
140
141
142
143
for key, rql_expr in RDEF_CONSTRAINTS.items():
    if 'S container AT' in rql_expr:
        try:
            etype, rtype = key
        except ValueError:
            etype = '*'
            rtype = key
        affk.tag_subject_of((etype, rtype, '*'),
                            {'widget': ContainedRelationFacetWidget(dialog_options={'width': 800})})
144
145
146
147
148
149


class BinaryDataObjectTabbedPrimaryView(tabs.TabbedPrimaryView):
    __select__ = tabs.TabbedPrimaryView.__select__ & is_instance('SEDABinaryDataObject')
    tabs = [
        'main_tab',
150
        _('seda_bdo_format_identification'),
151
        _('seda_bdo_file_information'),
152
153
154
    ]


155
156
157
158
159
160
161
162
class BinaryDataObjectFormatIdentificationTab(tabs.PrimaryTab):
    """Display format identification information of a binary data object"""
    __regid__ = 'seda_bdo_format_identification'
    __select__ = tabs.PrimaryTab.__select__ & is_instance('SEDABinaryDataObject')

    rsection, display_ctrl = rtags_from_xsd_element('SEDABinaryDataObject', 'FormatIdentification')


163
164
165
166
167
168
169
170
class BinaryDataObjectFileInfoTab(tabs.PrimaryTab):
    """Display file information of a binary data object"""
    __regid__ = 'seda_bdo_file_information'
    __select__ = tabs.PrimaryTab.__select__ & is_instance('SEDABinaryDataObject')

    rsection, display_ctrl = rtags_from_xsd_element('SEDABinaryDataObject', 'FileInfo')


171
172
173
174
175
class PhysicalDataObjectTabbedPrimaryView(tabs.TabbedPrimaryView):
    __select__ = tabs.TabbedPrimaryView.__select__ & is_instance('SEDAPhysicalDataObject')
    tabs = [
        'main_tab',
    ]