schema.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 30 Sep 2015 16:14:29 +0200
changeset 10736 8d49849ec2a6
parent 10689 49a62b8f6d43
child 10791 22611236983d
permissions -rw-r--r--
[cwctl] stop playing tricks with std{out,err} buffering Python3 doesn't allow unbuffered text I/O, because it doesn't make sense.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9547
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5122
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6017
5f6a60ea8544 [relations in ui] provide context information when computing label for a relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    18
"""classes to define schemas for CubicWeb"""
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
    19
from __future__ import print_function
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
import re
9366
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
    24
from os.path import join, basename
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
from logging import getLogger
1133
8a409ea0c9ec more linting
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    26
from warnings import warn
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
10689
49a62b8f6d43 [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10674
diff changeset
    28
from six import text_type, string_types, add_metaclass
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    29
from six.moves import range
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    30
9366
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
    31
from logilab.common import tempattr
9280
bae0caa8477a [schema] edit syntax tree instead of playing with strings for RQLExpressions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9262
diff changeset
    32
from logilab.common.decorators import cached, clear_cache, monkeypatch, cachedproperty
2730
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2718
diff changeset
    33
from logilab.common.logging_ext import set_log_methods
8256
852c73392b00 [schema] make cubicweb 3.14 compatible with yams 0.35
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8160
diff changeset
    34
from logilab.common.deprecation import deprecated, class_moved, moved
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
    35
from logilab.common.textutils import splitstrip
2926
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
    36
from logilab.common.graph import get_cycles
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
10553
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
    38
import yams
2531
531ea4e7013e [cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2526
diff changeset
    39
from yams import BadSchemaDefinition, buildobjs as ybo
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
    40
from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema, \
5030
5238d9a8dfee [form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    41
     RelationDefinitionSchema, PermissionMixIn, role_name
10447
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
    42
from yams.constraints import (BaseConstraint, FormatConstraint, BoundaryConstraint,
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
    43
                              IntervalBoundConstraint, StaticVocabularyConstraint)
2531
531ea4e7013e [cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2526
diff changeset
    44
from yams.reader import (CONSTRAINTS, PyFileReader, SchemaLoader,
10517
fa9a0c80556d [schema] drop old backward compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10447
diff changeset
    45
                         cleanup_sys_modules, fill_schema_from_namespace)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
from rql import parse, nodes, RQLSyntaxError, TypeResolverException
9952
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
    48
from rql.analyze import ETypeResolver
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
2730
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2718
diff changeset
    50
import cubicweb
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10663
diff changeset
    51
from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized, _
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    53
try:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    54
    from cubicweb import server
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    55
except ImportError:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    56
    # We need to lookup DEBUG from there,
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    57
    # however a pure dbapi client may not have it.
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    58
    class server(object): pass
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    59
    server.DEBUG = False
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    60
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
    61
2596
d02eed70937f [R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2531
diff changeset
    62
PURE_VIRTUAL_RTYPES = set(('identity', 'has_text',))
d02eed70937f [R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2531
diff changeset
    63
VIRTUAL_RTYPES = set(('eid', 'identity', 'has_text',))
d02eed70937f [R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2531
diff changeset
    64
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4843
diff changeset
    65
# set of meta-relations available for every entity types
2622
3c7edaa6c6d2 oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2616
diff changeset
    66
META_RTYPES = set((
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    67
    'owned_by', 'created_by', 'is', 'is_instance_of', 'identity',
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6377
diff changeset
    68
    'eid', 'creation_date', 'cw_source', 'modification_date', 'has_text', 'cwuri',
2184
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2181
diff changeset
    69
    ))
6375
df4fd2a1b0e7 [schema] introduce new WORKFLOW_RTYPES set and use it to build SYSTEM_RTYPES/DONT_CHECK_RTYPES_ON_ADD/DONT_CHECK_RTYPES_ON_DEL sets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6262
diff changeset
    70
WORKFLOW_RTYPES = set(('custom_workflow', 'in_state', 'wf_info_for'))
6377
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    71
WORKFLOW_DEF_RTYPES = set(('workflow_of', 'state_of', 'transition_of',
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    72
                           'initial_state', 'default_workflow',
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    73
                           'allowed_transition', 'destination_state',
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    74
                           'from_state', 'to_state', 'condition',
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    75
                           'subworkflow', 'subworkflow_state', 'subworkflow_exit',
9660
a78efec4cf04 Add missing relation types items in schema global variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9635
diff changeset
    76
                           'by_transition',
6377
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    77
                           ))
7797
a71618a75b53 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
    78
SYSTEM_RTYPES = set(('in_group', 'require_group',
6377
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    79
                     # cwproperty
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    80
                     'for_user',
9660
a78efec4cf04 Add missing relation types items in schema global variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9635
diff changeset
    81
                     'cw_schema', 'cw_import_of', 'cw_for_source',
a78efec4cf04 Add missing relation types items in schema global variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9635
diff changeset
    82
                     'cw_host_config_of',
6377
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    83
                     )) | WORKFLOW_RTYPES
6481
103774c8c215 [schema categorization] new NO_I18NCONTEXT set usable to somewhat control c-c i18n
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    84
NO_I18NCONTEXT = META_RTYPES | WORKFLOW_RTYPES
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
6951
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
    86
SKIP_COMPOSITE_RELS = [('cw_source', 'subject')]
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
    87
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4843
diff changeset
    88
# set of entity and relation types used to build the schema
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    89
SCHEMA_TYPES = set((
9974
b240b33c7125 [schema] CWComputedRType is a schema type
Julien Cristau <julien.cristau@logilab.fr>
parents: 9967
diff changeset
    90
    'CWEType', 'CWRType', 'CWComputedRType', 'CWAttribute', 'CWRelation',
6229
c4a70a5dd144 [schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6191
diff changeset
    91
    'CWConstraint', 'CWConstraintType', 'CWUniqueTogetherConstraint',
c4a70a5dd144 [schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6191
diff changeset
    92
    'RQLExpression',
6377
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6375
diff changeset
    93
    'specializes',
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    94
    'relation_type', 'from_entity', 'to_entity',
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    95
    'constrained_by', 'cstrtype',
6229
c4a70a5dd144 [schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6191
diff changeset
    96
    'constraint_of', 'relations',
6262
84901d735156 add permission relations to schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6233
diff changeset
    97
    'read_permission', 'add_permission',
84901d735156 add permission relations to schema types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6233
diff changeset
    98
    'delete_permission', 'update_permission',
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    99
    ))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
4434
101344a6ff9b Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4252
diff changeset
   101
WORKFLOW_TYPES = set(('Transition', 'State', 'TrInfo', 'Workflow',
4759
af2e6c377c71 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4757
diff changeset
   102
                      'WorkflowTransition', 'BaseTransition',
af2e6c377c71 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4757
diff changeset
   103
                      'SubWorkflowExitPoint'))
af2e6c377c71 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4757
diff changeset
   104
9173
f3286e817f28 [schema] mark CWDataImport as an internal type. Closes #3025536
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9168
diff changeset
   105
INTERNAL_TYPES = set(('CWProperty', 'CWCache', 'ExternalUri', 'CWDataImport',
6949
267ce7cf77f1 [test, ui] mark CWSourceSchemaConfig as an internal type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   106
                      'CWSource', 'CWSourceHostConfig', 'CWSourceSchemaConfig'))
4434
101344a6ff9b Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4252
diff changeset
   107
101344a6ff9b Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4252
diff changeset
   108
2142
098aa2075903 include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   109
_LOGGER = getLogger('cubicweb.schemaloader')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8892
diff changeset
   111
# entity and relation schema created from serialized schema have an eid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
ybo.ETYPE_PROPERTIES += ('eid',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
ybo.RTYPE_PROPERTIES += ('eid',)
2926
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   114
9951
8cdcbf3f4fd0 [schema] add utility function to build a CubicWebSchema from a namespace
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9892
diff changeset
   115
def build_schema_from_namespace(items):
8cdcbf3f4fd0 [schema] add utility function to build a CubicWebSchema from a namespace
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9892
diff changeset
   116
    schema = CubicWebSchema('noname')
8cdcbf3f4fd0 [schema] add utility function to build a CubicWebSchema from a namespace
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9892
diff changeset
   117
    fill_schema_from_namespace(schema, items, register_base_types=False)
8cdcbf3f4fd0 [schema] add utility function to build a CubicWebSchema from a namespace
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9892
diff changeset
   118
    return schema
8cdcbf3f4fd0 [schema] add utility function to build a CubicWebSchema from a namespace
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9892
diff changeset
   119
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   120
# Bases for manipulating RQL in schema #########################################
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   121
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   122
def guess_rrqlexpr_mainvars(expression):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   123
    defined = set(split_expression(expression))
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   124
    mainvars = set()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   125
    if 'S' in defined:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   126
        mainvars.add('S')
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   127
    if 'O' in defined:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   128
        mainvars.add('O')
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   129
    if 'U' in defined:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   130
        mainvars.add('U')
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   131
    if not mainvars:
9957
5def1d98fce7 [schema] properly raise BadSchemaDefinition
Laura Médioni <laura.medioni@logilab.fr>
parents: 9952
diff changeset
   132
        raise BadSchemaDefinition('unable to guess selection variables in %r'
5def1d98fce7 [schema] properly raise BadSchemaDefinition
Laura Médioni <laura.medioni@logilab.fr>
parents: 9952
diff changeset
   133
                                  % expression)
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   134
    return mainvars
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   135
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   136
def split_expression(rqlstring):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   137
    for expr in rqlstring.split(','):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   138
        for noparen1 in expr.split('('):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   139
            for noparen2 in noparen1.split(')'):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   140
                for word in noparen2.split():
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   141
                    yield word
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   142
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   143
def normalize_expression(rqlstring):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   144
    """normalize an rql expression to ease schema synchronization (avoid
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   145
    suppressing and reinserting an expression if only a space has been
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   146
    added/removed for instance)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   147
    """
10653
42c5bd7286b7 [schema] improve normalization of RQLExpressions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10612
diff changeset
   148
    union = parse(u'Any 1 WHERE %s' % rqlstring).as_string()
42c5bd7286b7 [schema] improve normalization of RQLExpressions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10612
diff changeset
   149
    if isinstance(union, str):
42c5bd7286b7 [schema] improve normalization of RQLExpressions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10612
diff changeset
   150
        union = union.decode('utf-8')
42c5bd7286b7 [schema] improve normalization of RQLExpressions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10612
diff changeset
   151
    return union.split(' WHERE ', 1)[1]
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   152
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   153
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   154
def _check_valid_formula(rdef, formula_rqlst):
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   155
    """Check the formula is a valid RQL query with some restriction (no union,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   156
    single selected node, etc.), raise BadSchemaDefinition if not
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   157
    """
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   158
    if len(formula_rqlst.children) != 1:
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   159
        raise BadSchemaDefinition('computed attribute %(attr)s on %(etype)s: '
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   160
                                  'can not use UNION in formula %(form)r' %
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   161
                                  {'attr' : rdef.rtype,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   162
                                   'etype' : rdef.subject.type,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   163
                                   'form' : rdef.formula})
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   164
    select = formula_rqlst.children[0]
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   165
    if len(select.selection) != 1:
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   166
        raise BadSchemaDefinition('computed attribute %(attr)s on %(etype)s: '
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   167
                                  'can only select one term in formula %(form)r' %
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   168
                                  {'attr' : rdef.rtype,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   169
                                   'etype' : rdef.subject.type,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   170
                                   'form' : rdef.formula})
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   171
    term = select.selection[0]
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   172
    types = set(term.get_type(sol) for sol in select.solutions)
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   173
    if len(types) != 1:
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   174
        raise BadSchemaDefinition('computed attribute %(attr)s on %(etype)s: '
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   175
                                  'multiple possible types (%(types)s) for formula %(form)r' %
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   176
                                  {'attr' : rdef.rtype,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   177
                                   'etype' : rdef.subject.type,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   178
                                   'types' : list(types),
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   179
                                   'form' : rdef.formula})
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   180
    computed_type = types.pop()
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   181
    expected_type = rdef.object.type
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   182
    if computed_type != expected_type:
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   183
        raise BadSchemaDefinition('computed attribute %(attr)s on %(etype)s: '
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   184
                                  'computed attribute type (%(comp_type)s) mismatch with '
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   185
                                  'specified type (%(attr_type)s)' %
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   186
                                  {'attr' : rdef.rtype,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   187
                                   'etype' : rdef.subject.type,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   188
                                   'comp_type' : computed_type,
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   189
                                   'attr_type' : expected_type})
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   190
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
   191
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   192
class RQLExpression(object):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   193
    """Base class for RQL expression used in schema (constraints and
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   194
    permissions)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   195
    """
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   196
    # these are overridden by set_log_methods below
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   197
    # only defining here to prevent pylint from complaining
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   198
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   199
    # to be defined in concrete classes
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   200
    rqlst = None
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   201
    predefined_variables = None
9958
3b755c9057fd [schema] define full_rql on RQLExpression class
Laura Médioni <laura.medioni@logilab.fr>
parents: 9957
diff changeset
   202
    full_rql = None
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   203
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   204
    def __init__(self, expression, mainvars, eid):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   205
        """
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   206
        :type mainvars: sequence of RQL variables' names. Can be provided as a
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   207
                        comma separated string.
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   208
        :param mainvars: names of the variables being selected.
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   209
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   210
        """
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   211
        self.eid = eid # eid of the entity representing this rql expression
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   212
        assert mainvars, 'bad mainvars %s' % mainvars
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   213
        if isinstance(mainvars, string_types):
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   214
            mainvars = set(splitstrip(mainvars))
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   215
        elif not isinstance(mainvars, set):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   216
            mainvars = set(mainvars)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   217
        self.mainvars = mainvars
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   218
        self.expression = normalize_expression(expression)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   219
        try:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   220
            self.full_rql = self.rqlst.as_string()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   221
        except RQLSyntaxError:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   222
            raise RQLSyntaxError(expression)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   223
        for mainvar in mainvars:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   224
            # if variable is predefined, an extra reference is inserted
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   225
            # automatically (`VAR eid %(v)s`)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   226
            if mainvar in self.predefined_variables:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   227
                min_refs = 3
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   228
            else:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   229
                min_refs = 2
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   230
            if len(self.rqlst.defined_vars[mainvar].references()) < min_refs:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   231
                _LOGGER.warn('You did not use the %s variable in your RQL '
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   232
                             'expression %s', mainvar, self)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   233
        # syntax tree used by read security (inserted in queries when necessary)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   234
        self.snippet_rqlst = parse(self.minimal_rql, print_errors=False).children[0]
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   235
        # graph of links between variables, used by rql rewriter
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   236
        self.vargraph = vargraph(self.rqlst)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   237
        # useful for some instrumentation, e.g. localperms permcheck command
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   238
        self.package = ybo.PACKAGE
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   239
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   240
    def __str__(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   241
        return self.full_rql
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   242
    def __repr__(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   243
        return '%s(%s)' % (self.__class__.__name__, self.full_rql)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   244
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   245
    def __lt__(self, other):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   246
        if hasattr(other, 'expression'):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   247
            return self.expression < other.expression
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   248
        return True
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   249
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   250
    def __eq__(self, other):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   251
        if hasattr(other, 'expression'):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   252
            return self.expression == other.expression
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   253
        return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   254
10661
e6eb0c7c2e98 implement __ne__ whenever we implement __eq__
Julien Cristau <julien.cristau@logilab.fr>
parents: 10653
diff changeset
   255
    def __ne__(self, other):
e6eb0c7c2e98 implement __ne__ whenever we implement __eq__
Julien Cristau <julien.cristau@logilab.fr>
parents: 10653
diff changeset
   256
        return not (self == other)
e6eb0c7c2e98 implement __ne__ whenever we implement __eq__
Julien Cristau <julien.cristau@logilab.fr>
parents: 10653
diff changeset
   257
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   258
    def __hash__(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   259
        return hash(self.expression)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   260
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   261
    def __deepcopy__(self, memo):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   262
        return self.__class__(self.expression, self.mainvars)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   263
    def __getstate__(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   264
        return (self.expression, self.mainvars)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   265
    def __setstate__(self, state):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   266
        self.__init__(*state)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   267
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   268
    @cachedproperty
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   269
    def rqlst(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   270
        select = parse(self.minimal_rql, print_errors=False).children[0]
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   271
        defined = set(split_expression(self.expression))
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   272
        for varname in self.predefined_variables:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   273
            if varname in defined:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   274
                select.add_eid_restriction(select.get_variable(varname), varname.lower(), 'Substitute')
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   275
        return select
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   276
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   277
    # permission rql expression specific stuff #################################
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   278
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   279
    @cached
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   280
    def transform_has_permission(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   281
        found = None
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   282
        rqlst = self.rqlst
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   283
        for var in rqlst.defined_vars.values():
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   284
            for varref in var.references():
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   285
                rel = varref.relation()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   286
                if rel is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   287
                    continue
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   288
                try:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   289
                    prefix, action, suffix = rel.r_type.split('_')
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   290
                except ValueError:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   291
                    continue
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   292
                if prefix != 'has' or suffix != 'permission' or \
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   293
                       not action in ('add', 'delete', 'update', 'read'):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   294
                    continue
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   295
                if found is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   296
                    found = []
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   297
                    rqlst.save_state()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   298
                assert rel.children[0].name == 'U'
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   299
                objvar = rel.children[1].children[0].variable
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   300
                rqlst.remove_node(rel)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   301
                selected = [v.name for v in rqlst.get_selected_variables()]
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   302
                if objvar.name not in selected:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   303
                    colindex = len(selected)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   304
                    rqlst.add_selected(objvar)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   305
                else:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   306
                    colindex = selected.index(objvar.name)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   307
                found.append((action, colindex))
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   308
                # remove U eid %(u)s if U is not used in any other relation
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   309
                uvrefs = rqlst.defined_vars['U'].references()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   310
                if len(uvrefs) == 1:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   311
                    rqlst.remove_node(uvrefs[0].relation())
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   312
        if found is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   313
            rql = rqlst.as_string()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   314
            if len(rqlst.selection) == 1 and isinstance(rqlst.where, nodes.Relation):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   315
                # only "Any X WHERE X eid %(x)s" remaining, no need to execute the rql
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   316
                keyarg = rqlst.selection[0].name.lower()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   317
            else:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   318
                keyarg = None
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   319
            rqlst.recover()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   320
            return rql, found, keyarg
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   321
        return rqlst.as_string(), None, None
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   322
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   323
    def _check(self, _cw, **kwargs):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   324
        """return True if the rql expression is matching the given relation
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   325
        between fromeid and toeid
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   326
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   327
        _cw may be a request or a server side transaction
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   328
        """
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   329
        creating = kwargs.get('creating')
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   330
        if not creating and self.eid is not None:
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10661
diff changeset
   331
            key = (self.eid, tuple(sorted(kwargs.items())))
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   332
            try:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   333
                return _cw.local_perm_cache[key]
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   334
            except KeyError:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   335
                pass
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   336
        rql, has_perm_defs, keyarg = self.transform_has_permission()
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   337
        # when creating an entity, expression related to X satisfied
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   338
        if creating and 'X' in self.rqlst.defined_vars:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   339
            return True
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   340
        if keyarg is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   341
            kwargs.setdefault('u', _cw.user.eid)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   342
            try:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   343
                rset = _cw.execute(rql, kwargs, build_descr=True)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   344
            except NotImplementedError:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   345
                self.critical('cant check rql expression, unsupported rql %s', rql)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   346
                if self.eid is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   347
                    _cw.local_perm_cache[key] = False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   348
                return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   349
            except TypeResolverException as ex:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   350
                # some expression may not be resolvable with current kwargs
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   351
                # (type conflict)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   352
                self.warning('%s: %s', rql, str(ex))
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   353
                if self.eid is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   354
                    _cw.local_perm_cache[key] = False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   355
                return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   356
            except Unauthorized as ex:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   357
                self.debug('unauthorized %s: %s', rql, str(ex))
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   358
                if self.eid is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   359
                    _cw.local_perm_cache[key] = False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   360
                return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   361
        else:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   362
            rset = _cw.eid_rset(kwargs[keyarg])
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   363
        # if no special has_*_permission relation in the rql expression, just
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   364
        # check the result set contains something
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   365
        if has_perm_defs is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   366
            if rset:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   367
                if self.eid is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   368
                    _cw.local_perm_cache[key] = True
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   369
                return True
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   370
        elif rset:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   371
            # check every special has_*_permission relation is satisfied
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   372
            get_eschema = _cw.vreg.schema.eschema
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   373
            try:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   374
                for eaction, col in has_perm_defs:
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
   375
                    for i in range(len(rset)):
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   376
                        eschema = get_eschema(rset.description[i][col])
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   377
                        eschema.check_perm(_cw, eaction, eid=rset[i][col])
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   378
                if self.eid is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   379
                    _cw.local_perm_cache[key] = True
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   380
                return True
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   381
            except Unauthorized:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   382
                pass
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   383
        if self.eid is not None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   384
            _cw.local_perm_cache[key] = False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   385
        return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   386
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   387
    @property
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   388
    def minimal_rql(self):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   389
        return 'Any %s WHERE %s' % (','.join(sorted(self.mainvars)),
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   390
                                    self.expression)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   391
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   392
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   393
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   394
# rql expressions for use in permission definition #############################
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   395
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   396
class ERQLExpression(RQLExpression):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   397
    predefined_variables = 'XU'
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   398
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   399
    def __init__(self, expression, mainvars=None, eid=None):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   400
        RQLExpression.__init__(self, expression, mainvars or 'X', eid)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   401
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   402
    def check(self, _cw, eid=None, creating=False, **kwargs):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   403
        if 'X' in self.rqlst.defined_vars:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   404
            if eid is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   405
                if creating:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   406
                    return self._check(_cw, creating=True, **kwargs)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   407
                return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   408
            assert creating == False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   409
            return self._check(_cw, x=eid, **kwargs)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   410
        return self._check(_cw, **kwargs)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   411
9600
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   412
def constraint_by_eid(self, eid):
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   413
    for cstr in self.constraints:
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   414
        if cstr.eid == eid:
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   415
            return cstr
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   416
    raise ValueError('No constraint with eid %d' % eid)
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   417
RelationDefinitionSchema.constraint_by_eid = constraint_by_eid
bde625698f44 [merge] bring the 3.17.14 fixes to 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9395 9565
diff changeset
   418
9393
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   419
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   420
def vargraph(rqlst):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   421
    """ builds an adjacency graph of variables from the rql syntax tree, e.g:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   422
    Any O,S WHERE T subworkflow_exit S, T subworkflow WF, O state_of WF
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   423
    => {'WF': ['O', 'T'], 'S': ['T'], 'T': ['WF', 'S'], 'O': ['WF']}
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   424
    """
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   425
    vargraph = {}
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   426
    for relation in rqlst.get_nodes(nodes.Relation):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   427
        try:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   428
            rhsvarname = relation.children[1].children[0].variable.name
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   429
            lhsvarname = relation.children[0].name
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   430
        except AttributeError:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   431
            pass
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   432
        else:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   433
            vargraph.setdefault(lhsvarname, []).append(rhsvarname)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   434
            vargraph.setdefault(rhsvarname, []).append(lhsvarname)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   435
            #vargraph[(lhsvarname, rhsvarname)] = relation.r_type
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   436
    return vargraph
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   437
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   438
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   439
class GeneratedConstraint(object):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   440
    def __init__(self, rqlst, mainvars):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   441
        self.snippet_rqlst = rqlst
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   442
        self.mainvars = mainvars
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   443
        self.vargraph = vargraph(rqlst)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   444
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   445
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   446
class RRQLExpression(RQLExpression):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   447
    predefined_variables = 'SOU'
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   448
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   449
    def __init__(self, expression, mainvars=None, eid=None):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   450
        if mainvars is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   451
            mainvars = guess_rrqlexpr_mainvars(expression)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   452
        RQLExpression.__init__(self, expression, mainvars, eid)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   453
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   454
    def check(self, _cw, fromeid=None, toeid=None):
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   455
        kwargs = {}
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   456
        if 'S' in self.rqlst.defined_vars:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   457
            if fromeid is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   458
                return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   459
            kwargs['s'] = fromeid
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   460
        if 'O' in self.rqlst.defined_vars:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   461
            if toeid is None:
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   462
                return False
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   463
            kwargs['o'] = toeid
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   464
        return self._check(_cw, **kwargs)
8266c8c375bb [schema] move definition of RQLExpression classes before default permission dicts
Julien Cristau <julien.cristau@logilab.fr>
parents: 9366
diff changeset
   465
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   466
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   467
# In yams, default 'update' perm for attributes granted to managers and owners.
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   468
# Within cw, we want to default to users who may edit the entity holding the
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   469
# attribute.
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   470
# These default permissions won't be checked by the security hooks:
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   471
# since they delegate checking to the entity, we can skip actual checks.
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   472
ybo.DEFAULT_ATTRPERMS['update'] = ('managers', ERQLExpression('U has_update_permission X'))
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   473
ybo.DEFAULT_ATTRPERMS['add'] = ('managers', ERQLExpression('U has_add_permission X'))
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   474
10553
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   475
# we don't want 'add' or 'delete' permissions on computed relation types
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   476
# (they're hardcoded to '()' on computed relation definitions)
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   477
if 'add' in yams.DEFAULT_COMPUTED_RELPERMS:
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   478
    del yams.DEFAULT_COMPUTED_RELPERMS['add']
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   479
if 'delete' in yams.DEFAULT_COMPUTED_RELPERMS:
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   480
    del yams.DEFAULT_COMPUTED_RELPERMS['delete']
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   481
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   482
4754
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   483
PUB_SYSTEM_ENTITY_PERMS = {
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   484
    'read':   ('managers', 'users', 'guests',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   485
    'add':    ('managers',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   486
    'delete': ('managers',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   487
    'update': ('managers',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   488
    }
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   489
PUB_SYSTEM_REL_PERMS = {
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   490
    'read':   ('managers', 'users', 'guests',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   491
    'add':    ('managers',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   492
    'delete': ('managers',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   493
    }
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   494
PUB_SYSTEM_ATTR_PERMS = {
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   495
    'read':   ('managers', 'users', 'guests',),
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   496
    'add': ('managers',),
6944
0cf10429ad39 [sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6861
diff changeset
   497
    'update': ('managers',),
4754
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   498
    }
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   499
RO_REL_PERMS = {
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   500
    'read':   ('managers', 'users', 'guests',),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   501
    'add':    (),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   502
    'delete': (),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   503
    }
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   504
RO_ATTR_PERMS = {
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   505
    'read':   ('managers', 'users', 'guests',),
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9393
diff changeset
   506
    'add': ybo.DEFAULT_ATTRPERMS['add'],
4754
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   507
    'update': (),
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   508
    }
6bf17f810975 [schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4717
diff changeset
   509
2926
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   510
# XXX same algorithm as in reorder_cubes and probably other place,
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   511
# may probably extract a generic function
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   512
def order_eschemas(eschemas):
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   513
    """return entity schemas ordered such that entity types which specializes an
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   514
    other one appears after that one
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   515
    """
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   516
    graph = {}
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   517
    for eschema in eschemas:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   518
        if eschema.specializes():
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   519
            graph[eschema] = set((eschema.specializes(),))
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   520
        else:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   521
            graph[eschema] = set()
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   522
    cycles = get_cycles(graph)
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   523
    if cycles:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   524
        cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles)
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   525
        raise Exception('cycles in entity schema specialization: %s'
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   526
                        % cycles)
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   527
    eschemas = []
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   528
    while graph:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   529
        # sorted to get predictable results
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   530
        for eschema, deps in sorted(graph.items()):
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   531
            if not deps:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   532
                eschemas.append(eschema)
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   533
                del graph[eschema]
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   534
                for deps in graph.values():
2926
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   535
                    try:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   536
                        deps.remove(eschema)
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   537
                    except KeyError:
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   538
                        continue
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   539
    return eschemas
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
   540
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   541
def bw_normalize_etype(etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   542
    if etype in ETYPE_NAME_MAP:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   543
        msg = '%s has been renamed to %s, please update your code' % (
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   544
            etype, ETYPE_NAME_MAP[etype])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   545
        warn(msg, DeprecationWarning, stacklevel=4)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
        etype = ETYPE_NAME_MAP[etype]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
    return etype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
3275
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3240
diff changeset
   549
def display_name(req, key, form='', context=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   550
    """return a internationalized string for the key (schema entity or relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
    name) in a given form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   553
    assert form in ('', 'plural', 'subject', 'object')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
    if form == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   555
        form = ''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   556
    if form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
        key = key + '_' + form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   558
    # ensure unicode
3284
036cf5a25714 ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3275
diff changeset
   559
    if context is not None:
10689
49a62b8f6d43 [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10674
diff changeset
   560
        return text_type(req.pgettext(context, key))
3275
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3240
diff changeset
   561
    else:
10689
49a62b8f6d43 [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10674
diff changeset
   562
        return text_type(req._(key))
3275
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3240
diff changeset
   563
3978
2c95e3033f64 finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3968
diff changeset
   564
2c95e3033f64 finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3968
diff changeset
   565
# Schema objects definition ###################################################
2c95e3033f64 finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3968
diff changeset
   566
3471
8c57c71b859c can now give context to rschema.display_name(...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3401
diff changeset
   567
def ERSchema_display_name(self, req, form='', context=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
    """return a internationalized string for the entity/relation type name in
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   569
    a given form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   570
    """
3471
8c57c71b859c can now give context to rschema.display_name(...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3401
diff changeset
   571
    return display_name(req, self.type, form, context)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   572
ERSchema.display_name = ERSchema_display_name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   573
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
@cached
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   575
def get_groups(self, action):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
    """return the groups authorized to perform <action> on entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   577
    this type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
    :type action: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   580
    :param action: the name of a permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   581
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   582
    :rtype: tuple
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
    :return: names of the groups with the given permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   585
    assert action in self.ACTIONS, action
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
    #assert action in self._groups, '%s %s' % (self, action)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   587
    try:
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   588
        return frozenset(g for g in self.permissions[action] if isinstance(g, string_types))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   589
    except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   590
        return ()
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   591
PermissionMixIn.get_groups = get_groups
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   592
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   593
@cached
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   594
def get_rqlexprs(self, action):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   595
    """return the rql expressions representing queries to check the user is allowed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
    to perform <action> on entities of this type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   598
    :type action: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   599
    :param action: the name of a permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
    :rtype: tuple
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   602
    :return: the rql expressions with the given permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   603
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   604
    assert action in self.ACTIONS, action
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   605
    #assert action in self._rqlexprs, '%s %s' % (self, action)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   606
    try:
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   607
        return tuple(g for g in self.permissions[action] if not isinstance(g, string_types))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   608
    except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   609
        return ()
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   610
PermissionMixIn.get_rqlexprs = get_rqlexprs
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   612
orig_set_action_permissions = PermissionMixIn.set_action_permissions
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   613
def set_action_permissions(self, action, permissions):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   614
    """set the groups and rql expressions allowing to perform <action> on
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   615
    entities of this type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   616
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   617
    :type action: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   618
    :param action: the name of a permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   619
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   620
    :type permissions: tuple
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   621
    :param permissions: the groups and rql expressions allowing the given action
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   622
    """
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   623
    orig_set_action_permissions(self, action, tuple(permissions))
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   624
    clear_cache(self, 'get_rqlexprs')
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   625
    clear_cache(self, 'get_groups')
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   626
PermissionMixIn.set_action_permissions = set_action_permissions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   627
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   628
def has_local_role(self, action):
10140
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
   629
    """return true if the action *may* be granted locally (i.e. either rql
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   630
    expressions or the owners group are used in security definition)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   631
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   632
    XXX this method is only there since we don't know well how to deal with
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   633
    'add' action checking. Also find a better name would be nice.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   634
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   635
    assert action in self.ACTIONS, action
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   636
    if self.get_rqlexprs(action):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   637
        return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   638
    if action in ('update', 'delete'):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   639
        return 'owners' in self.get_groups(action)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   640
    return False
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   641
PermissionMixIn.has_local_role = has_local_role
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   642
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   643
def may_have_permission(self, action, req):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   644
    if action != 'read' and not (self.has_local_role('read') or
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   645
                                 self.has_perm(req, 'read')):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   646
        return False
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   647
    return self.has_local_role(action) or self.has_perm(req, action)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   648
PermissionMixIn.may_have_permission = may_have_permission
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   649
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   650
def has_perm(self, _cw, action, **kwargs):
9817
bb719d857421 [schema] spelling fixes in doc strings
Julien Cristau <julien.cristau@logilab.fr>
parents: 9711
diff changeset
   651
    """return true if the action is granted globally or locally"""
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   652
    try:
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   653
        self.check_perm(_cw, action, **kwargs)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   654
        return True
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   655
    except Unauthorized:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   656
        return False
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   657
PermissionMixIn.has_perm = has_perm
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   658
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   659
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   660
def check_perm(self, _cw, action, **kwargs):
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   661
    # NB: _cw may be a server transaction or a request object.
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   662
    #
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   663
    # check user is in an allowed group, if so that's enough internal
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   664
    # transactions should always stop there
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   665
    DBG = False
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   666
    if server.DEBUG & server.DBG_SEC:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   667
        if action in server._SECURITY_CAPS:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   668
            _self_str = str(self)
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   669
            if server._SECURITY_ITEMS:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   670
                if any(item in _self_str for item in server._SECURITY_ITEMS):
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   671
                    DBG = True
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   672
            else:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   673
                DBG = True
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   674
    groups = self.get_groups(action)
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   675
    if _cw.user.matching_groups(groups):
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   676
        if DBG:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
   677
            print('check_perm: %r %r: user matches %s' % (action, _self_str, groups))
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   678
        return
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   679
    # if 'owners' in allowed groups, check if the user actually owns this
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   680
    # object, if so that's enough
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   681
    #
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   682
    # NB: give _cw to user.owns since user is not be bound to a transaction on
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   683
    # the repository side
4607
55eab66c6592 [schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4586
diff changeset
   684
    if 'owners' in groups and (
55eab66c6592 [schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4586
diff changeset
   685
          kwargs.get('creating')
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   686
          or ('eid' in kwargs and _cw.user.owns(kwargs['eid']))):
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   687
        if DBG:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
   688
            print('check_perm: %r %r: user is owner or creation time' %
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
   689
                  (action, _self_str))
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   690
        return
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   691
    # else if there is some rql expressions, check them
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8945
diff changeset
   692
    if DBG:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
   693
        print('check_perm: %r %r %s' %
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
   694
              (action, _self_str, [(rqlexpr, kwargs, rqlexpr.check(_cw, **kwargs))
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10553
diff changeset
   695
                                   for rqlexpr in self.get_rqlexprs(action)]))
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   696
    if any(rqlexpr.check(_cw, **kwargs)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   697
           for rqlexpr in self.get_rqlexprs(action)):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   698
        return
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   699
    raise Unauthorized(action, str(self))
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   700
PermissionMixIn.check_perm = check_perm
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   701
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   702
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   703
RelationDefinitionSchema._RPROPERTIES['eid'] = None
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8892
diff changeset
   704
# remember rproperties defined at this point. Others will have to be serialized in
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8892
diff changeset
   705
# CWAttribute.extra_props
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8892
diff changeset
   706
KNOWN_RPROPERTIES = RelationDefinitionSchema.ALL_PROPERTIES()
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   707
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   708
def rql_expression(self, expression, mainvars=None, eid=None):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   709
    """rql expression factory"""
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   710
    if self.rtype.final:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   711
        return ERQLExpression(expression, mainvars, eid)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   712
    return RRQLExpression(expression, mainvars, eid)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   713
RelationDefinitionSchema.rql_expression = rql_expression
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   714
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   715
orig_check_permission_definitions = RelationDefinitionSchema.check_permission_definitions
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   716
def check_permission_definitions(self):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   717
    orig_check_permission_definitions(self)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   718
    schema = self.subject.schema
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10661
diff changeset
   719
    for action, groups in self.permissions.items():
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   720
        for group_or_rqlexpr in groups:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   721
            if action == 'read' and \
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   722
                   isinstance(group_or_rqlexpr, RQLExpression):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   723
                msg = "can't use rql expression for read permission of %s"
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   724
                raise BadSchemaDefinition(msg % self)
4574
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   725
            if self.final and isinstance(group_or_rqlexpr, RRQLExpression):
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   726
                msg = "can't use RRQLExpression on %s, use an ERQLExpression"
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   727
                raise BadSchemaDefinition(msg % self)
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   728
            if not self.final and isinstance(group_or_rqlexpr, ERQLExpression):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   729
                msg = "can't use ERQLExpression on %s, use a RRQLExpression"
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   730
                raise BadSchemaDefinition(msg % self)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   731
RelationDefinitionSchema.check_permission_definitions = check_permission_definitions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   732
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
class CubicWebEntitySchema(EntitySchema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
    """a entity has a type, a set of subject and or object relations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
    the entity schema defines the possible relations for a given type and some
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
    constraints on those relations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
    def __init__(self, schema=None, edef=None, eid=None, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   740
        super(CubicWebEntitySchema, self).__init__(schema, edef, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
        if eid is None and edef is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   742
            eid = getattr(edef, 'eid', None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   743
        self.eid = eid
4574
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   744
9547
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   745
    def targets(self, role):
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   746
        assert role in ('subject', 'object')
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   747
        if role == 'subject':
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   748
            return self.subjrels.values()
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   749
        return self.objrels.values()
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   750
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   751
    @cachedproperty
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   752
    def composite_rdef_roles(self):
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   753
        """Return all relation definitions that define the current entity
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   754
        type as a composite.
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   755
        """
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   756
        rdef_roles = []
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   757
        for role in ('subject', 'object'):
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   758
            for rschema in self.targets(role):
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   759
                if rschema.final:
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   760
                    continue
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   761
                for rdef in rschema.rdefs.values():
9711
59616edc20d7 [schema] fix buggy composite_rdef_roles and associated test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9660
diff changeset
   762
                    if (role == 'subject' and rdef.subject == self) or \
59616edc20d7 [schema] fix buggy composite_rdef_roles and associated test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9660
diff changeset
   763
                            (role == 'object' and rdef.object == self):
59616edc20d7 [schema] fix buggy composite_rdef_roles and associated test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9660
diff changeset
   764
                        crole = rdef.composite
59616edc20d7 [schema] fix buggy composite_rdef_roles and associated test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9660
diff changeset
   765
                        if crole == role:
59616edc20d7 [schema] fix buggy composite_rdef_roles and associated test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9660
diff changeset
   766
                            rdef_roles.append((rdef, role))
9547
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   767
        return rdef_roles
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   768
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   769
    @cachedproperty
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   770
    def is_composite(self):
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   771
        return bool(len(self.composite_rdef_roles))
43aace16a953 [schema] add composite handling helpers on EntitySchema (related to #3463112)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   772
4574
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   773
    def check_permission_definitions(self):
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   774
        super(CubicWebEntitySchema, self).check_permission_definitions()
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   775
        for groups in self.permissions.values():
4574
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   776
            for group_or_rqlexpr in groups:
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   777
                if isinstance(group_or_rqlexpr, RRQLExpression):
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   778
                    msg = "can't use RRQLExpression on %s, use an ERQLExpression"
2380444d982c [schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   779
                    raise BadSchemaDefinition(msg % self.type)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   780
6951
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   781
    def is_subobject(self, strict=False, skiprels=None):
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   782
        if skiprels is None:
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   783
            skiprels = SKIP_COMPOSITE_RELS
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   784
        else:
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   785
            skiprels += SKIP_COMPOSITE_RELS
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   786
        return super(CubicWebEntitySchema, self).is_subobject(strict,
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   787
                                                              skiprels=skiprels)
cb6314b09e0f [schema, ui] fix is_subobject to skip cw_source relation (else everything is considered as a subobject, since this relation is now composite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6949
diff changeset
   788
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   789
    def attribute_definitions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   790
        """return an iterator on attribute definitions
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   791
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   792
        attribute relations are a subset of subject relations where the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   793
        object's type is a final entity
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   794
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   795
        an attribute definition is a 2-uple :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   796
        * name of the relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   797
        * schema of the destination entity type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   798
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   799
        iter = super(CubicWebEntitySchema, self).attribute_definitions()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   800
        for rschema, attrschema in iter:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   801
            if rschema.type == 'has_text':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   802
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   803
            yield rschema, attrschema
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   804
2128
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   805
    def main_attribute(self):
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   806
        """convenience method that returns the *main* (i.e. the first non meta)
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   807
        attribute defined in the entity schema
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   808
        """
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   809
        for rschema, _ in self.attribute_definitions():
2622
3c7edaa6c6d2 oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2616
diff changeset
   810
            if not (rschema in META_RTYPES
2128
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   811
                    or self.is_metadata(rschema)):
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   812
                return rschema
464edb198faa drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2126
diff changeset
   813
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   814
    def add_subject_relation(self, rschema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   815
        """register the relation schema as possible subject relation"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   816
        super(CubicWebEntitySchema, self).add_subject_relation(rschema)
7467
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   817
        if rschema.final:
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   818
            if self.rdef(rschema).get('fulltextindexed'):
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   819
                self._update_has_text()
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   820
        elif rschema.fulltext_container:
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   821
            self._update_has_text()
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   822
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   823
    def add_object_relation(self, rschema):
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   824
        """register the relation schema as possible object relation"""
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   825
        super(CubicWebEntitySchema, self).add_object_relation(rschema)
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   826
        if rschema.fulltext_container:
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   827
            self._update_has_text()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   828
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   829
    def del_subject_relation(self, rtype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   830
        super(CubicWebEntitySchema, self).del_subject_relation(rtype)
7467
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   831
        if 'has_text' in self.subjrels:
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   832
            self._update_has_text(deletion=True)
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   833
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   834
    def del_object_relation(self, rtype):
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   835
        super(CubicWebEntitySchema, self).del_object_relation(rtype)
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   836
        if 'has_text' in self.subjrels:
5a6b3e51807d [schema] fix computing of has_text relation: when a fulltext_container relation is added, we need to recompute has_text for the target entity type as well. Also, avoid computation when not needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   837
            self._update_has_text(deletion=True)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   838
2632
920bfaff60a4 B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2622
diff changeset
   839
    def _update_has_text(self, deletion=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   840
        may_need_has_text, has_has_text = False, False
2632
920bfaff60a4 B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2622
diff changeset
   841
        need_has_text = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   842
        for rschema in self.subject_relations():
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3554
diff changeset
   843
            if rschema.final:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   844
                if rschema == 'has_text':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   845
                    has_has_text = True
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   846
                elif self.rdef(rschema).get('fulltextindexed'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   847
                    may_need_has_text = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   848
            elif rschema.fulltext_container:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   849
                if rschema.fulltext_container == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   850
                    may_need_has_text = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   851
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   852
                    need_has_text = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   853
        for rschema in self.object_relations():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   854
            if rschema.fulltext_container:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   855
                if rschema.fulltext_container == 'object':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   856
                    may_need_has_text = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   857
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   858
                    need_has_text = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   859
        if need_has_text is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   860
            need_has_text = may_need_has_text
2632
920bfaff60a4 B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2622
diff changeset
   861
        if need_has_text and not has_has_text and not deletion:
4755
13a5d3a7410e [schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4754
diff changeset
   862
            rdef = ybo.RelationDefinition(self.type, 'has_text', 'String',
13a5d3a7410e [schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4754
diff changeset
   863
                                          __permissions__=RO_ATTR_PERMS)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   864
            self.schema.add_relation_def(rdef)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   865
        elif not need_has_text and has_has_text:
5122
c06078d59e87 [schema] ensure we don't remove has_text relation type unfortunatly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   866
            # use rschema.del_relation_def and not schema.del_relation_def to
c06078d59e87 [schema] ensure we don't remove has_text relation type unfortunatly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   867
            # avoid deleting the relation type accidentally...
c06078d59e87 [schema] ensure we don't remove has_text relation type unfortunatly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   868
            self.schema['has_text'].del_relation_def(self, self.schema['String'])
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   869
5896
67683b7e591a cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
   870
    def schema_entity(self): # XXX @property for consistency with meta
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   871
        """return True if this entity type is used to build the schema"""
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   872
        return self.type in SCHEMA_TYPES
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   873
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   874
    def rql_expression(self, expression, mainvars=None, eid=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   875
        """rql expression factory"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   876
        return ERQLExpression(expression, mainvars, eid)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   877
2252
dd9758cb77e1 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2222
diff changeset
   878
10553
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   879
class CubicWebRelationSchema(PermissionMixIn, RelationSchema):
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   880
    permissions = {}
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   881
    ACTIONS = ()
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   882
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   883
    def __init__(self, schema=None, rdef=None, eid=None, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   884
        if rdef is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   885
            # if this relation is inlined
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   886
            self.inlined = rdef.inlined
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   887
        super(CubicWebRelationSchema, self).__init__(schema, rdef, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   888
        if eid is None and rdef is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   889
            eid = getattr(rdef, 'eid', None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   890
        self.eid = eid
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   891
10553
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   892
    def init_computed_relation(self, rdef):
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   893
        self.ACTIONS = ('read',)
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   894
        super(CubicWebRelationSchema, self).init_computed_relation(rdef)
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   895
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   896
    def advertise_new_add_permission(self):
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   897
        pass
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   898
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   899
    def check_permission_definitions(self):
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   900
        RelationSchema.check_permission_definitions(self)
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   901
        PermissionMixIn.check_permission_definitions(self)
1d824df4f2bd Fix (de)serialization of ComputedRelation read permissions
Julien Cristau <julien.cristau@logilab.fr>
parents: 10552
diff changeset
   902
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   903
    @property
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   904
    def meta(self):
2622
3c7edaa6c6d2 oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2616
diff changeset
   905
        return self.type in META_RTYPES
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   906
5896
67683b7e591a cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
   907
    def schema_relation(self): # XXX @property for consistency with meta
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   908
        """return True if this relation type is used to build the schema"""
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   909
        return self.type in SCHEMA_TYPES
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   910
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   911
    def may_have_permission(self, action, req, eschema=None, role=None):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   912
        if eschema is not None:
4019
7f68077d2c3b api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4005
diff changeset
   913
            for tschema in self.targets(eschema, role):
7f68077d2c3b api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4005
diff changeset
   914
                rdef = self.role_rdef(eschema, tschema, role)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   915
                if rdef.may_have_permission(action, req):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   916
                    return True
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   917
        else:
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   918
            for rdef in self.rdefs.values():
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   919
                if rdef.may_have_permission(action, req):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   920
                    return True
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   921
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   922
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   923
    def has_perm(self, _cw, action, **kwargs):
10283
8e6aa8ed6ee1 [schema] typo
Julien Cristau <julien.cristau@logilab.fr>
parents: 10193
diff changeset
   924
        """return true if the action is granted globally or locally"""
4575
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   925
        if self.final:
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   926
            assert not ('fromeid' in kwargs or 'toeid' in kwargs), kwargs
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   927
            assert action in ('read', 'update')
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   928
            if 'eid' in kwargs:
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9395
diff changeset
   929
                subjtype = _cw.entity_metas(kwargs['eid'])['type']
4575
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   930
            else:
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   931
                subjtype = objtype = None
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   932
        else:
4575
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   933
            assert not 'eid' in kwargs, kwargs
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   934
            assert action in ('read', 'add', 'delete')
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   935
            if 'fromeid' in kwargs:
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9395
diff changeset
   936
                subjtype = _cw.entity_metas(kwargs['fromeid'])['type']
5746
f4fc424747db [security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5703
diff changeset
   937
            elif 'frometype' in kwargs:
f4fc424747db [security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5703
diff changeset
   938
                subjtype = kwargs.pop('frometype')
4575
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   939
            else:
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   940
                subjtype = None
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   941
            if 'toeid' in kwargs:
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9395
diff changeset
   942
                objtype = _cw.entity_metas(kwargs['toeid'])['type']
5746
f4fc424747db [security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5703
diff changeset
   943
            elif 'toetype' in kwargs:
f4fc424747db [security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5703
diff changeset
   944
                objtype = kwargs.pop('toetype')
4575
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   945
            else:
4a7fe84f7803 [schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4574
diff changeset
   946
                objtype = None
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   947
        if objtype and subjtype:
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   948
            return self.rdef(subjtype, objtype).has_perm(_cw, action, **kwargs)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   949
        elif subjtype:
4004
c52619c738a5 api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4003
diff changeset
   950
            for tschema in self.targets(subjtype, 'subject'):
c52619c738a5 api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4003
diff changeset
   951
                rdef = self.rdef(subjtype, tschema)
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   952
                if not rdef.has_perm(_cw, action, **kwargs):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   953
                    return False
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   954
        elif objtype:
4004
c52619c738a5 api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4003
diff changeset
   955
            for tschema in self.targets(objtype, 'object'):
c52619c738a5 api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4003
diff changeset
   956
                rdef = self.rdef(tschema, objtype)
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   957
                if not rdef.has_perm(_cw, action, **kwargs):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   958
                    return False
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   959
        else:
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   960
            for rdef in self.rdefs.values():
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
   961
                if not rdef.has_perm(_cw, action, **kwargs):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   962
                    return False
4233
94ffaecd8e8c ouch, has_perm was always returning False...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   963
        return True
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   964
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   965
    @deprecated('use .rdef(subjtype, objtype).role_cardinality(role)')
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   966
    def cardinality(self, subjtype, objtype, target):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
   967
        return self.rdef(subjtype, objtype).role_cardinality(target)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   968
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   969
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   970
class CubicWebSchema(Schema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   971
    """set of entities and relations schema defining the possible data sets
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   972
    used in an application
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   973
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   974
    :type name: str
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2460
diff changeset
   975
    :ivar name: name of the schema, usually the instance identifier
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   976
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   977
    :type base: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   978
    :ivar base: path of the directory where the schema is defined
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   979
    """
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   980
    reading_from_database = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   981
    entity_class = CubicWebEntitySchema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   982
    relation_class = CubicWebRelationSchema
2958
44e5446b649b no inference for the identity relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2926
diff changeset
   983
    no_specialization_inference = ('identity',)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   984
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   985
    def __init__(self, *args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   986
        self._eid_index = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   987
        super(CubicWebSchema, self).__init__(*args, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   988
        ybo.register_base_types(self)
2300
c8151d004e06 meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   989
        rschema = self.add_relation_type(ybo.RelationType('eid'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   990
        rschema.final = True
2300
c8151d004e06 meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   991
        rschema = self.add_relation_type(ybo.RelationType('has_text'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   992
        rschema.final = True
2300
c8151d004e06 meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   993
        rschema = self.add_relation_type(ybo.RelationType('identity'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   994
        rschema.final = False
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   995
7440
539de926ec9d [test] fix failure introduced by 1719eab69551: the regexp should be fixed, not the error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7383
diff changeset
   996
    etype_name_re = r'[A-Z][A-Za-z0-9]*[a-z]+[A-Za-z0-9]*$'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   997
    def add_entity_type(self, edef):
10674
f0006bdd36e9 [py3k] bytes vs str
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10667
diff changeset
   998
        edef.name = str(edef.name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   999
        edef.name = bw_normalize_etype(edef.name)
7383
1719eab69551 [schema] fix imprecise error message
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7152
diff changeset
  1000
        if not re.match(self.etype_name_re, edef.name):
6861
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1001
            raise BadSchemaDefinition(
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1002
                '%r is not a valid name for an entity type. It should start '
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1003
                'with an upper cased letter and be followed by at least a '
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1004
                'lower cased letter' % edef.name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1005
        eschema = super(CubicWebSchema, self).add_entity_type(edef)
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3554
diff changeset
  1006
        if not eschema.final:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1007
            # automatically add the eid relation to non final entity types
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1008
            rdef = ybo.RelationDefinition(eschema.type, 'eid', 'Int',
4755
13a5d3a7410e [schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4754
diff changeset
  1009
                                          cardinality='11', uid=True,
13a5d3a7410e [schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4754
diff changeset
  1010
                                          __permissions__=RO_ATTR_PERMS)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1011
            self.add_relation_def(rdef)
4755
13a5d3a7410e [schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4754
diff changeset
  1012
            rdef = ybo.RelationDefinition(eschema.type, 'identity', eschema.type,
13a5d3a7410e [schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4754
diff changeset
  1013
                                          __permissions__=RO_REL_PERMS)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1014
            self.add_relation_def(rdef)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1015
        self._eid_index[eschema.eid] = eschema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1016
        return eschema
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1017
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1018
    def add_relation_type(self, rdef):
6861
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1019
        if not rdef.name.islower():
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1020
            raise BadSchemaDefinition(
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1021
                '%r is not a valid name for a relation type. It should be '
9d4e11d6e783 [schema] ease understanding of schema naming error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6827
diff changeset
  1022
                'lower cased' % rdef.name)
10674
f0006bdd36e9 [py3k] bytes vs str
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10667
diff changeset
  1023
        rdef.name = str(rdef.name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1024
        rschema = super(CubicWebSchema, self).add_relation_type(rdef)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1025
        self._eid_index[rschema.eid] = rschema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1026
        return rschema
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1027
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1028
    def add_relation_def(self, rdef):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1029
        """build a part of a relation schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1030
        (i.e. add a relation between two specific entity's types)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1031
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1032
        :type subject: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1033
        :param subject: entity's type that is subject of the relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1034
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1035
        :type rtype: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1036
        :param rtype: the relation's type (i.e. the name of the relation)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1037
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1038
        :type obj: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1039
        :param obj: entity's type that is object of the relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1040
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1041
        :rtype: RelationSchema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1042
        :param: the newly created or just completed relation schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1043
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1044
        rdef.name = rdef.name.lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1045
        rdef.subject = bw_normalize_etype(rdef.subject)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1046
        rdef.object = bw_normalize_etype(rdef.object)
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
  1047
        rdefs = super(CubicWebSchema, self).add_relation_def(rdef)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
  1048
        if rdefs:
1034
0356bbfb2f26 fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1049
            try:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
  1050
                self._eid_index[rdef.eid] = rdefs
1034
0356bbfb2f26 fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1051
            except AttributeError:
0356bbfb2f26 fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1052
                pass # not a serialized schema
4003
b9436fe77c9e fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
  1053
        return rdefs
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1054
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1055
    def del_relation_type(self, rtype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1056
        rschema = self.rschema(rtype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1057
        self._eid_index.pop(rschema.eid, None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1058
        super(CubicWebSchema, self).del_relation_type(rtype)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1059
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1060
    def del_relation_def(self, subjtype, rtype, objtype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1061
        for k, v in self._eid_index.items():
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
  1062
            if not isinstance(v, RelationDefinitionSchema):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
  1063
                continue
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3827
diff changeset
  1064
            if v.subject == subjtype and v.rtype == rtype and v.object == objtype:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1065
                del self._eid_index[k]
2718
3a56b87bb5d6 [schema] break when found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2716
diff changeset
  1066
                break
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1067
        super(CubicWebSchema, self).del_relation_def(subjtype, rtype, objtype)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1068
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1069
    def del_entity_type(self, etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1070
        eschema = self.eschema(etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1071
        self._eid_index.pop(eschema.eid, None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1072
        # deal with has_text first, else its automatic deletion (see above)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1073
        # may trigger an error in ancestor's del_entity_type method
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1074
        if 'has_text' in eschema.subject_relations():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1075
            self.del_relation_def(etype, 'has_text', 'String')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1076
        super(CubicWebSchema, self).del_entity_type(etype)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1077
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1078
    def schema_by_eid(self, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1079
        return self._eid_index[eid]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1080
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1081
    def iter_computed_attributes(self):
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1082
        for relation in self.relations():
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
  1083
            for rdef in relation.rdefs.values():
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1084
                if rdef.final and rdef.formula is not None:
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1085
                    yield rdef
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1086
9952
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1087
    def iter_computed_relations(self):
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1088
        for relation in self.relations():
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1089
            if relation.rule:
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1090
                yield relation
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1091
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1092
    def finalize(self):
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1093
        super(CubicWebSchema, self).finalize()
9966
6c2d57d1b6de [CWEP002] Add schema finalization checks for computed attributes
Laura Médioni <laura.medioni@logilab.fr>
parents: 9963
diff changeset
  1094
        self.finalize_computed_attributes()
9952
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1095
        self.finalize_computed_relations()
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1096
9966
6c2d57d1b6de [CWEP002] Add schema finalization checks for computed attributes
Laura Médioni <laura.medioni@logilab.fr>
parents: 9963
diff changeset
  1097
    def finalize_computed_attributes(self):
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1098
        """Check computed attributes validity (if any), else raise
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1099
        `BadSchemaDefinition`
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1100
        """
9966
6c2d57d1b6de [CWEP002] Add schema finalization checks for computed attributes
Laura Médioni <laura.medioni@logilab.fr>
parents: 9963
diff changeset
  1101
        analyzer = ETypeResolver(self)
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1102
        for rdef in self.iter_computed_attributes():
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1103
            rqlst = parse(rdef.formula)
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1104
            select = rqlst.children[0]
10192
365e5a0287d6 [computed attribute] ensure attribute's formula apply only to the correct type. Closes #4901163
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10140
diff changeset
  1105
            select.add_type_restriction(select.defined_vars['X'], str(rdef.subject))
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1106
            analyzer.visit(select)
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1107
            _check_valid_formula(rdef, rqlst)
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1108
            rdef.formula_select = select # avoid later recomputation
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1109
9966
6c2d57d1b6de [CWEP002] Add schema finalization checks for computed attributes
Laura Médioni <laura.medioni@logilab.fr>
parents: 9963
diff changeset
  1110
9952
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1111
    def finalize_computed_relations(self):
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1112
        """Build relation definitions for computed relations
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1113
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1114
        The subject and object types are infered using rql analyzer.
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1115
        """
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1116
        analyzer = ETypeResolver(self)
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1117
        for rschema in self.iter_computed_relations():
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1118
            # XXX rule is valid if both S and O are defined and not in an exists
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1119
            rqlexpr = RRQLExpression(rschema.rule)
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1120
            rqlst = rqlexpr.snippet_rqlst
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1121
            analyzer.visit(rqlst)
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1122
            couples = set((sol['S'], sol['O']) for sol in rqlst.solutions)
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1123
            for subjtype, objtype in couples:
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1124
                if self[objtype].final:
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1125
                    raise BadSchemaDefinition('computed relations cannot be final')
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1126
                rdef = ybo.RelationDefinition(
10193
a0a11be5a9cb [schema] set permissions that do not allow edition on computed relation. Closes #4903918
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10192
diff changeset
  1127
                    subjtype, rschema.type, objtype,
a0a11be5a9cb [schema] set permissions that do not allow edition on computed relation. Closes #4903918
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10192
diff changeset
  1128
                    __permissions__={'add': (),
a0a11be5a9cb [schema] set permissions that do not allow edition on computed relation. Closes #4903918
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10192
diff changeset
  1129
                                     'delete': (),
10552
0e7fab504305 Copy read permissions on ComputedRelation rdefs
Christophe de Vienne <christophe@unlish.com>
parents: 10531
diff changeset
  1130
                                     'read': rschema.permissions['read']})
9952
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1131
                rdef.infered = True
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1132
                self.add_relation_def(rdef)
0f3f965b6365 [CWEP002] Add schema finalization checks for computed relations (rules)
Lea Capgen <lea.capgen@logilab.fr>, Sylvain Thénault <sylvain.thenault@logilab.fr>, Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9951
diff changeset
  1133
9963
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9958
diff changeset
  1134
    def rebuild_infered_relations(self):
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9958
diff changeset
  1135
        super(CubicWebSchema, self).rebuild_infered_relations()
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9958
diff changeset
  1136
        self.finalize_computed_relations()
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9958
diff changeset
  1137
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1138
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1139
# additional cw specific constraints ###########################################
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1140
10447
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
  1141
# these are implemented as CHECK constraints in sql, don't do the work
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
  1142
# twice
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
  1143
StaticVocabularyConstraint.check = lambda *args: True
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
  1144
IntervalBoundConstraint.check = lambda *args: True
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
  1145
BoundaryConstraint.check = lambda *args: True
d309c020d9fb [schema] neuter check method for constraints we implement in sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10283
diff changeset
  1146
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1147
class BaseRQLConstraint(RRQLExpression, BaseConstraint):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1148
    """base class for rql constraints"""
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1149
    distinct_query = None
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1150
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1151
    def serialize(self):
9253
77e31ede9b04 [schema] drop very old bw compat (pre 3.5.10)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9246
diff changeset
  1152
        # start with a semicolon for bw compat, see below
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1153
        return ';' + ','.join(sorted(self.mainvars)) + ';' + self.expression
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1154
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1155
    @classmethod
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1156
    def deserialize(cls, value):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1157
        _, mainvars, expression = value.split(';', 2)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1158
        return cls(expression, mainvars)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1159
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1160
    def check(self, entity, rtype, value):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1161
        """return true if the value satisfy the constraint, else false"""
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1162
        # implemented as a hook in the repository
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1163
        return 1
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1164
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1165
    def __str__(self):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1166
        if self.distinct_query:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1167
            selop = 'Any'
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1168
        else:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1169
            selop = 'DISTINCT Any'
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1170
        return '%s(%s %s WHERE %s)' % (self.__class__.__name__, selop,
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1171
                                       ','.join(sorted(self.mainvars)),
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1172
                                       self.expression)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1173
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1174
    def __repr__(self):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1175
        return '<%s @%#x>' % (self.__str__(), id(self))
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1176
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1177
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1178
class RQLVocabularyConstraint(BaseRQLConstraint):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1179
    """the rql vocabulary constraint:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1180
10140
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1181
    limits the proposed values to a set of entities returned by an rql query,
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1182
    but this is not enforced at the repository level
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1183
10140
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1184
    `expression` is an additional rql restriction that will be added to
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1185
    a predefined query, where the S and O variables respectively represent
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1186
    the subject and the object of the relation
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1187
10140
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1188
    `mainvars` is a set of variables that should be used as selection variables
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1189
    (i.e. `'Any %s WHERE ...' % mainvars`). If not specified, an attempt will be
e14ec2aac099 [schema] Typo and indentation fix in RQLVocabularyConstraint doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9990
diff changeset
  1190
    made to guess it based on the variables used in the expression.
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1191
    """
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1192
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1193
    def repo_check(self, session, eidfrom, rtype, eidto):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1194
        """raise ValidationError if the relation doesn't satisfy the constraint
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1195
        """
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7973
diff changeset
  1196
        pass # this is a vocabulary constraint, not enforced
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1197
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1198
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1199
class RepoEnforcedRQLConstraintMixIn(object):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1200
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1201
    def __init__(self, expression, mainvars=None, msg=None):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1202
        super(RepoEnforcedRQLConstraintMixIn, self).__init__(expression, mainvars)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1203
        self.msg = msg
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1204
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1205
    def serialize(self):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1206
        # start with a semicolon for bw compat, see below
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1207
        return ';%s;%s\n%s' % (','.join(sorted(self.mainvars)), self.expression,
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1208
                               self.msg or '')
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1209
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1210
    def deserialize(cls, value):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1211
        value, msg = value.split('\n', 1)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1212
        _, mainvars, expression = value.split(';', 2)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1213
        return cls(expression, mainvars, msg)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1214
    deserialize = classmethod(deserialize)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1215
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1216
    def repo_check(self, session, eidfrom, rtype, eidto=None):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1217
        """raise ValidationError if the relation doesn't satisfy the constraint
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1218
        """
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1219
        if not self.match_condition(session, eidfrom, eidto):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1220
            # XXX at this point if both or neither of S and O are in mainvar we
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1221
            # dunno if the validation error `occurred` on eidfrom or eidto (from
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1222
            # user interface point of view)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1223
            #
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1224
            # possible enhancement: check entity being created, it's probably
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1225
            # the main eid unless this is a composite relation
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1226
            if eidto is None or 'S' in self.mainvars or not 'O' in self.mainvars:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1227
                maineid = eidfrom
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1228
                qname = role_name(rtype, 'subject')
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1229
            else:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1230
                maineid = eidto
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1231
                qname = role_name(rtype, 'object')
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1232
            if self.msg:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1233
                msg = session._(self.msg)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1234
            else:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1235
                msg = '%(constraint)s %(expression)s failed' % {
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1236
                    'constraint':  session._(self.type()),
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1237
                    'expression': self.expression}
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1238
            raise ValidationError(maineid, {qname: msg})
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1239
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
  1240
    def exec_query(self, _cw, eidfrom, eidto):
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1241
        if eidto is None:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1242
            # checking constraint for an attribute relation
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1243
            expression = 'S eid %(s)s, ' + self.expression
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1244
            args = {'s': eidfrom}
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1245
        else:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1246
            expression = 'S eid %(s)s, O eid %(o)s, ' + self.expression
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1247
            args = {'s': eidfrom, 'o': eidto}
7640
85ebdbbcb321 [schema] closes #1816439: RQLConstraint does not take variable U into account
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7603
diff changeset
  1248
        if 'U' in self.rqlst.defined_vars:
85ebdbbcb321 [schema] closes #1816439: RQLConstraint does not take variable U into account
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7603
diff changeset
  1249
            expression = 'U eid %(u)s, ' + expression
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
  1250
            args['u'] = _cw.user.eid
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1251
        rql = 'Any %s WHERE %s' % (','.join(sorted(self.mainvars)), expression)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1252
        if self.distinct_query:
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1253
            rql = 'DISTINCT ' + rql
8540
fee98af8bb33 [schema] rename schema permissions function: session may also be a request, use _cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8258
diff changeset
  1254
        return _cw.execute(rql, args, build_descr=False)
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1255
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1256
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1257
class RQLConstraint(RepoEnforcedRQLConstraintMixIn, RQLVocabularyConstraint):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1258
    """the rql constraint is similar to the RQLVocabularyConstraint but
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1259
    are also enforced at the repository level
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1260
    """
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1261
    distinct_query = False
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1262
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1263
    def match_condition(self, session, eidfrom, eidto):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1264
        return self.exec_query(session, eidfrom, eidto)
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1265
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1266
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1267
class RQLUniqueConstraint(RepoEnforcedRQLConstraintMixIn, BaseRQLConstraint):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1268
    """the unique rql constraint check that the result of the query isn't
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1269
    greater than one.
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1270
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1271
    You *must* specify `mainvars` when instantiating the constraint since there
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1272
    is no way to guess it correctly (e.g. if using S,O or U the constraint will
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1273
    always be satisfied because we've to use a DISTINCT query).
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1274
    """
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1275
    # XXX turns mainvars into a required argument in __init__
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1276
    distinct_query = True
9227
762a331db741 [schema] fix spurious warning when rqlexpr/constraint mainvars specify a non predefined variable. Closes #3098165
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9173
diff changeset
  1277
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1278
    def match_condition(self, session, eidfrom, eidto):
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1279
        return len(self.exec_query(session, eidfrom, eidto)) <= 1
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1280
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7103
diff changeset
  1281
629
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1282
# workflow extensions #########################################################
2926
4484387ed012 when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1283
2460
ce1a7ffc6c90 fix a couple NameErrors
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2459
diff changeset
  1284
from yams.buildobjs import _add_relation as yams_add_relation
629
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1285
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1286
class workflowable_definition(ybo.metadefinition):
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1287
    """extends default EntityType's metaclass to add workflow relations
5378
0f54a0e128ac [schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5372
diff changeset
  1288
    (i.e. in_state, wf_info_for and custom_workflow). This is the default
0f54a0e128ac [schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5372
diff changeset
  1289
    metaclass for WorkflowableEntityType.
629
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1290
    """
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1291
    def __new__(mcs, name, bases, classdict):
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1292
        abstract = classdict.pop('__abstract__', False)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1293
        cls = super(workflowable_definition, mcs).__new__(mcs, name, bases,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1294
                                                          classdict)
629
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1295
        if not abstract:
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1296
            make_workflowable(cls)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1297
        return cls
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1298
10667
c23dc4f7467a [py3k] declare meta classes using six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
  1299
c23dc4f7467a [py3k] declare meta classes using six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
  1300
@add_metaclass(workflowable_definition)
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1301
class WorkflowableEntityType(ybo.EntityType):
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1302
    """Use this base class instead of :class:`EntityType` to have workflow
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1303
    relations (i.e. `in_state`, `wf_info_for` and `custom_workflow`) on your
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1304
    entity type.
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1305
    """
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1306
    __abstract__ = True
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1307
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1308
2957
2daabf4c646e [schema] make_workflowable now take an option description for the in_state relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2931
diff changeset
  1309
def make_workflowable(cls, in_state_descr=None):
5378
0f54a0e128ac [schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5372
diff changeset
  1310
    """Adds workflow relations as :class:`WorkflowableEntityType`, but usable on
0f54a0e128ac [schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5372
diff changeset
  1311
    existing classes which are not using that base class.
0f54a0e128ac [schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5372
diff changeset
  1312
    """
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1313
    existing_rels = set(rdef.name for rdef in cls.__relations__)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1314
    # let relation types defined in cw.schemas.workflow carrying
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1315
    # cardinality, constraints and other relation definition properties
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1316
    etype = getattr(cls, 'name', cls.__name__)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1317
    if 'custom_workflow' not in existing_rels:
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1318
        rdef = ybo.RelationDefinition(etype, 'custom_workflow', 'Workflow')
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1319
        yams_add_relation(cls.__relations__, rdef)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1320
    if 'in_state' not in existing_rels:
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1321
        rdef = ybo.RelationDefinition(etype, 'in_state', 'State',
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1322
                                      description=in_state_descr)
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1323
        yams_add_relation(cls.__relations__, rdef)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2782
diff changeset
  1324
    if 'wf_info_for' not in existing_rels:
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1325
        rdef = ybo.RelationDefinition('TrInfo', 'wf_info_for', etype)
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5378
diff changeset
  1326
        yams_add_relation(cls.__relations__, rdef)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1327
629
59b6542f5729 provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 628
diff changeset
  1328
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1329
# schema loading ##############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1330
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1331
CONSTRAINTS['RQLConstraint'] = RQLConstraint
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1332
CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1333
CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint
3980
b17cf4a2e09e this constraint is actually unusable in yams schema for cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3979
diff changeset
  1334
CONSTRAINTS.pop('MultipleStaticVocabularyConstraint', None) # don't want this in cw yams schema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1335
PyFileReader.context.update(CONSTRAINTS)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1336
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1337
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1338
class BootstrapSchemaLoader(SchemaLoader):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1339
    """cubicweb specific schema loader, loading only schema necessary to read
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1340
    the persistent schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1341
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1342
    schemacls = CubicWebSchema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1343
1034
0356bbfb2f26 fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1344
    def load(self, config, path=(), **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1345
        """return a Schema instance from the schema definition read
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1346
        from <directory>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1347
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1348
        return super(BootstrapSchemaLoader, self).load(
1034
0356bbfb2f26 fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1349
            path, config.appid, register_base_types=False, **kwargs)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1350
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1351
    def _load_definition_files(self, cubes=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1352
        # bootstraping, ignore cubes
2735
39c942241b48 no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2730
diff changeset
  1353
        filepath = join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'bootstrap.py')
2142
098aa2075903 include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
  1354
        self.info('loading %s', filepath)
9366
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
  1355
        with tempattr(ybo, 'PACKAGE', 'cubicweb'): # though we don't care here
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
  1356
            self.handle_file(filepath)
1451
982e8616d9a2 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
  1357
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1358
    def unhandled_file(self, filepath):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1359
        """called when a file without handler associated has been found"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1360
        self.warning('ignoring file %r', filepath)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1361
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6861
diff changeset
  1362
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6861
diff changeset
  1363
    # only defining here to prevent pylint from complaining
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6861
diff changeset
  1364
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1365
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1366
class CubicWebSchemaLoader(BootstrapSchemaLoader):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1367
    """cubicweb specific schema loader, automatically adding metadata to the
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2460
diff changeset
  1368
    instance's schema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1369
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1370
1034
0356bbfb2f26 fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents: 1016
diff changeset
  1371
    def load(self, config, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1372
        """return a Schema instance from the schema definition read
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1373
        from <directory>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1374
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1375
        self.info('loading %s schemas', ', '.join(config.cubes()))
2782
998f24dabd0d compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2742
diff changeset
  1376
        self.extrapath = {}
998f24dabd0d compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2742
diff changeset
  1377
        for cubesdir in config.cubes_search_path():
998f24dabd0d compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2742
diff changeset
  1378
            if cubesdir != config.CUBES_DIR:
998f24dabd0d compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2742
diff changeset
  1379
                self.extrapath[cubesdir] = 'cubes'
372
a8a975a88368 check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
  1380
        if config.apphome:
2598
a66fe74bd9fc [R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2596
diff changeset
  1381
            path = tuple(reversed([config.apphome] + config.cubes_path()))
372
a8a975a88368 check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
  1382
        else:
2598
a66fe74bd9fc [R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2596
diff changeset
  1383
            path = tuple(reversed(config.cubes_path()))
2526
40e41eb34a7d we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
  1384
        try:
40e41eb34a7d we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
  1385
            return super(CubicWebSchemaLoader, self).load(config, path=path, **kwargs)
40e41eb34a7d we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
  1386
        finally:
40e41eb34a7d we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
  1387
            # we've to cleanup modules imported from cubicweb.schemas as well
2735
39c942241b48 no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2730
diff changeset
  1388
            cleanup_sys_modules([join(cubicweb.CW_SOFTWARE_ROOT, 'schemas')])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1389
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1390
    def _load_definition_files(self, cubes):
2741
148d0cea1fb5 [schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2736
diff changeset
  1391
        for filepath in (join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'bootstrap.py'),
148d0cea1fb5 [schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2736
diff changeset
  1392
                         join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'base.py'),
148d0cea1fb5 [schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2736
diff changeset
  1393
                         join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'workflow.py'),
148d0cea1fb5 [schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2736
diff changeset
  1394
                         join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'Bookmark.py')):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1395
            self.info('loading %s', filepath)
9366
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
  1396
            with tempattr(ybo, 'PACKAGE', 'cubicweb'):
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
  1397
                self.handle_file(filepath)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1398
        for cube in cubes:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1399
            for filepath in self.get_schema_files(cube):
9366
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
  1400
                with tempattr(ybo, 'PACKAGE', basename(cube)):
bcbc92223b35 set proper PACKAGE information for yams 0.39
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9283
diff changeset
  1401
                    self.handle_file(filepath)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1402
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6861
diff changeset
  1403
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6861
diff changeset
  1404
    # only defining here to prevent pylint from complaining
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6861
diff changeset
  1405
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1406
9967
e65873ad0371 [CWEP002] Add support for computed attribute synchronization
Sylvain Thénault
parents: 9966
diff changeset
  1407
2459
d088d0ff48a1 move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2456
diff changeset
  1408
set_log_methods(CubicWebSchemaLoader, getLogger('cubicweb.schemaloader'))
d088d0ff48a1 move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2456
diff changeset
  1409
set_log_methods(BootstrapSchemaLoader, getLogger('cubicweb.bootstrapschemaloader'))
d088d0ff48a1 move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2456
diff changeset
  1410
set_log_methods(RQLExpression, getLogger('cubicweb.schema'))
d088d0ff48a1 move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2456
diff changeset
  1411
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1412
# _() is just there to add messages to the catalog, don't care about actual
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1413
# translation
7797
a71618a75b53 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1414
MAY_USE_TEMPLATE_FORMAT = set(('managers',))
2459
d088d0ff48a1 move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2456
diff changeset
  1415
NEED_PERM_FORMATS = [_('text/cubicweb-page-template')]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1416
2531
531ea4e7013e [cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2526
diff changeset
  1417
@monkeypatch(FormatConstraint)
3347
428f95118556 fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3287
diff changeset
  1418
def vocabulary(self, entity=None, form=None):
3401
f893f4f199fc use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
  1419
    cw = None
3347
428f95118556 fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3287
diff changeset
  1420
    if form is None and entity is not None:
3401
f893f4f199fc use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
  1421
        cw = entity._cw
3347
428f95118556 fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3287
diff changeset
  1422
    elif form is not None:
3401
f893f4f199fc use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
  1423
        cw = form._cw
4757
5d937aeaa6fb do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4755
diff changeset
  1424
    if cw is not None:
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1425
        if hasattr(cw, 'write_security'): # test it's a session and not a request
4757
5d937aeaa6fb do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4755
diff changeset
  1426
            # cw is a server session
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
  1427
            hasperm = not cw.write_security or \
4843
5f7363416765 fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
  1428
                      not cw.is_hook_category_activated('integrity') or \
7797
a71618a75b53 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1429
                      cw.user.matching_groups(MAY_USE_TEMPLATE_FORMAT)
4757
5d937aeaa6fb do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4755
diff changeset
  1430
        else:
7797
a71618a75b53 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1431
            hasperm = cw.user.matching_groups(MAY_USE_TEMPLATE_FORMAT)
4757
5d937aeaa6fb do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4755
diff changeset
  1432
        if hasperm:
5d937aeaa6fb do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4755
diff changeset
  1433
            return self.regular_formats + tuple(NEED_PERM_FORMATS)
2459
d088d0ff48a1 move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2456
diff changeset
  1434
    return self.regular_formats
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1435
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1436
# XXX itou for some Statement methods
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1437
from rql import stmts
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1438
orig_get_etype = stmts.ScopeNode.get_etype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1439
def bw_get_etype(self, name):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1440
    return orig_get_etype(self, bw_normalize_etype(name))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1441
stmts.ScopeNode.get_etype = bw_get_etype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1442
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1443
orig_add_main_variable_delete = stmts.Delete.add_main_variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1444
def bw_add_main_variable_delete(self, etype, vref):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1445
    return orig_add_main_variable_delete(self, bw_normalize_etype(etype), vref)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1446
stmts.Delete.add_main_variable = bw_add_main_variable_delete
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1447
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1448
orig_add_main_variable_insert = stmts.Insert.add_main_variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1449
def bw_add_main_variable_insert(self, etype, vref):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1450
    return orig_add_main_variable_insert(self, bw_normalize_etype(etype), vref)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1451
stmts.Insert.add_main_variable = bw_add_main_variable_insert
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1452
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1453
orig_set_statement_type = stmts.Select.set_statement_type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1454
def bw_set_statement_type(self, etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1455
    return orig_set_statement_type(self, bw_normalize_etype(etype))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1456
stmts.Select.set_statement_type = bw_set_statement_type