mgmt_rules.py 9.27 KB
Newer Older
1
# copyright 2016-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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 management rules"""

18
19
import json

20
21
22
from logilab.mtconverter import xml_escape

from cubicweb.predicates import is_instance
23
from cubicweb.web import INTERNAL_FIELD_VALUE
24
from cubicweb.web.views import reledit, uicfg
25

26
27
28
from ..entities import parent_and_container, rule_type_from_etype
from . import viewlib
from . import uicfg as sedauicfg  # noqa - ensure those rules are defined first
29
30
31
32
33
34
35
36
37
38
39


affk = uicfg.autoform_field_kwargs


def rule_ref_vocabulary(form, field):
    """Form vocabulary function, for fields referencing a management rules (i.e. a concept in a
    scheme defined on the archive transfer)
    """
    req = form._cw
    parent, container = parent_and_container(form.edited_entity)
40
    assert container is not None
41
42
43
44
45
46
47
    parent_etype = form.edited_entity.cw_etype
    if parent_etype == 'SEDARefNonRuleId':
        if parent is not None:
            parent_etype = parent.cw_etype
        else:
            parent_etype = json.loads(req.form['arg'][1])
    return _rule_ref_vocabulary(container, parent_etype)
48
49


50
51
52
def _rule_ref_vocabulary(container, parent_etype):
    req = container._cw
    rule_type = rule_type_from_etype(parent_etype)
53
54
55
56
57
58
59
60
61
62
63
64
    if container.cw_etype == 'SEDAArchiveTransfer':
        rql = ('Any C WHERE C in_scheme CS, AT eid %(at)s, '
               'CACLV seda_{0}_rule_code_list_version_from AT, '
               'CACLV seda_{0}_rule_code_list_version_to CS'.format(rule_type))
        rset = req.execute(rql, {'at': container.eid})
    else:
        # component archive unit, suppose we can use default vocabularies
        assert rule_type in ('access', 'appraisal')
        rql = ('Any C WHERE C in_scheme CS, CS scheme_relation_type RT, RT name %(rt)s,'
               'CS scheme_entity_type ET, ET name %(et)s')
        etype = 'SEDASeq{0}RuleRule'.format(rule_type.capitalize())
        rset = req.execute(rql, {'rt': 'seda_rule', 'et': etype})
65
    if rset:
66
        return ([(req._('<no value specified>'), INTERNAL_FIELD_VALUE)]
Noé Gaumont's avatar
Noé Gaumont committed
67
                + sorted([(concept.label(), str(concept.eid))
68
                          for concept in rset.entities()]))
69
    else:
70
        scheme_relation = 'seda_{0}_rule_code_list_version_from_object'.format(rule_type)
71
72
73
74
75
76
        scheme_relation = req._(scheme_relation)
        msg = req._('you must specify a scheme for {0} to select a value').format(scheme_relation)
        return [(msg, INTERNAL_FIELD_VALUE)]


affk.tag_subject_of(('*', 'seda_ref_non_rule_id_to', '*'),
77
78
                    {'choices': rule_ref_vocabulary,
                     'sort': False})
79
affk.tag_subject_of(('*', 'seda_rule', '*'),
80
81
                    {'choices': rule_ref_vocabulary,
                     'sort': False})
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112


class RuleComplexLinkEntityAttributeView(viewlib.TextEntityAttributeView):
    __select__ = (viewlib.ComplexLinkEntityAttributeView.__select__
                  & is_instance('SEDAAccessRule', 'SEDADisseminationRule', 'SEDAReuseRule'))

    def entity_call(self, entity):
        _ = self._cw._
        for rule_seq in entity.rules:
            if rule_seq.seda_rule:
                rule = rule_seq.seda_rule[0].label()
                rule = _('rule: {0}').format(rule)
            else:
                rule = _('<no rule specified>')
            if rule_seq.reverse_seda_start_date:
                start_date = _('with {0} start date').format(
                    rule_seq.reverse_seda_start_date[0].user_cardinality)
            else:
                start_date = _('without start date')
            self.w(u'<div>{0} {1}, {2}</div>'.format(xml_escape(rule),
                                                     rule_seq.view('seda.xsdmeta'),
                                                     xml_escape(start_date),))
        if entity.inheritance_control:
            ctrl = entity.inheritance_control
            alternatives = []
            if ctrl.reverse_seda_prevent_inheritance:
                prevent = ctrl.reverse_seda_prevent_inheritance[0]
                # if prevent.prevent_inheritance is None:
                #     alternatives.append(_('XXX prevent to be specified'))
                if prevent.prevent_inheritance:
                    alternatives.append(xml_escape(_('prevent all rules')))
113
                elif prevent.prevent_inheritance is False:
114
                    alternatives.append(xml_escape(_("don't prevent inheritance")))
115
116
                else:  # unspecified
                    alternatives.append(xml_escape(_("prevent inheritance or not to be specified")))
117
118
119
120
121
122
123
124
125
126
            non_rules = []
            for non_rule in ctrl.reverse_seda_ref_non_rule_id_from:
                if non_rule.seda_ref_non_rule_id_to:
                    rule_name = non_rule.seda_ref_non_rule_id_to[0].label()
                    non_rules.append(xml_escape(_('prevent rule {0}').format(rule_name)))
                else:
                    non_rules.append(xml_escape(_('<no rule to prevent specified>'))
                                     + ' ' + non_rule.view('seda.xsdmeta'))
            if non_rules:
                alternatives.append(u', '.join(non_rules))
127
            self.w((u'<b>{0}</b>'.format(self._cw._(' ALT_I18N '))).join(alternatives))
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180


class FinalActionRuleComplexLinkEntityAttributeView(RuleComplexLinkEntityAttributeView):
    __select__ = (viewlib.ComplexLinkEntityAttributeView.__select__
                  & is_instance('SEDAStorageRule', 'SEDAAppraisalRule'))

    def entity_call(self, entity):
        _ = self._cw._
        if entity.seda_final_action:
            action = entity.seda_final_action[0].label()
            final_action = _('final action: {0}').format(action)
        else:
            final_action = _('<no final action specified>')
        self.w(u'<div>{0} {1}</div>'.format(xml_escape(final_action), entity.view('seda.xsdmeta')))
        super(FinalActionRuleComplexLinkEntityAttributeView, self).entity_call(entity)


class ClassificationRuleComplexLinkEntityAttributeView(RuleComplexLinkEntityAttributeView):
    __select__ = (viewlib.ComplexLinkEntityAttributeView.__select__
                  & is_instance('SEDAClassificationRule'))

    def entity_call(self, entity):
        _ = self._cw._
        if entity.seda_classification_level:
            classification_level = entity.seda_classification_level[0].label()
            classification_level = _('classification level: {0}').format(classification_level)
        else:
            classification_level = _('<no classification level specified>')
        self.w(u'<div>{0} {1}</div>'.format(xml_escape(classification_level),
                                            entity.view('seda.xsdmeta')))
        if entity.classification_owner:
            classification_owner = entity.classification_owner
            classification_owner = _('classification owner: {0}').format(classification_owner)
        else:
            classification_owner = _('<no classification owner specified>')
        self.w(u'<div>{0}</div>'.format(xml_escape(classification_owner)))
        if entity.reverse_seda_classification_reassessing_date:
            reassessing_date = _('with {0} reassessing date').format(
                entity.reverse_seda_classification_reassessing_date[0].user_cardinality)
        else:
            reassessing_date = _('without reassessing date')
        self.w(u'<div>{0}</div>'.format(xml_escape(reassessing_date)))

        if entity.reverse_seda_need_reassessing_authorization:
            reassessing_authorization = entity.reverse_seda_need_reassessing_authorization[0]
            # if reassessing_authorization.need_reassessing_authorization is None:
            #     XXX
            if reassessing_authorization.need_reassessing_authorization:
                need_human = _('need human intervention')
            else:
                need_human = _('without human intervention')
            self.w(u'<div>{0}</div>'.format(xml_escape(need_human)))
        super(ClassificationRuleComplexLinkEntityAttributeView, self).entity_call(entity)
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195


class MgmtRuleAutoClickAndEditFormView(reledit.AutoClickAndEditFormView):
    __select__ = (reledit.AutoClickAndEditFormView.__select__
                  & is_instance('SEDASeqAltArchiveUnitArchiveUnitRefIdManagement'))

    def _compute_default_value(self, rschema, role):
        if rschema.type.endswith('_rule'):
            rule_type = rschema.type[len('seda_'):-len('_rule')]
            rule = self.entity.inherited_rule(rule_type)
            if rule:
                return rule.view('seda.reledit.text') + u'<p><mark>{}</mark></p>'.format(
                    self._cw._(u'inherited value'))
        return super(MgmtRuleAutoClickAndEditFormView, self)._compute_default_value(
            rschema, role)