cubicweb/schema.py
changeset 11215 4e79acdc36a6
parent 11151 4259c55df3e7
child 11343 892e4c12f03f
equal deleted inserted replaced
11212:3309ddb97059 11215:4e79acdc36a6
    38 import yams
    38 import yams
    39 from yams import BadSchemaDefinition, buildobjs as ybo
    39 from yams import BadSchemaDefinition, buildobjs as ybo
    40 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema, \
    40 from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema, \
    41      RelationDefinitionSchema, PermissionMixIn, role_name
    41      RelationDefinitionSchema, PermissionMixIn, role_name
    42 from yams.constraints import (BaseConstraint, FormatConstraint, BoundaryConstraint,
    42 from yams.constraints import (BaseConstraint, FormatConstraint, BoundaryConstraint,
    43                               IntervalBoundConstraint, StaticVocabularyConstraint)
    43                               IntervalBoundConstraint, StaticVocabularyConstraint,
       
    44                               cstr_json_dumps, cstr_json_loads)
    44 from yams.reader import (CONSTRAINTS, PyFileReader, SchemaLoader,
    45 from yams.reader import (CONSTRAINTS, PyFileReader, SchemaLoader,
    45                          cleanup_sys_modules, fill_schema_from_namespace)
    46                          cleanup_sys_modules, fill_schema_from_namespace)
    46 
    47 
    47 from rql import parse, nodes, RQLSyntaxError, TypeResolverException
    48 from rql import parse, nodes, RQLSyntaxError, TypeResolverException
    48 from rql.analyze import ETypeResolver
    49 from rql.analyze import ETypeResolver
  1143 class BaseRQLConstraint(RRQLExpression, BaseConstraint):
  1144 class BaseRQLConstraint(RRQLExpression, BaseConstraint):
  1144     """base class for rql constraints"""
  1145     """base class for rql constraints"""
  1145     distinct_query = None
  1146     distinct_query = None
  1146 
  1147 
  1147     def serialize(self):
  1148     def serialize(self):
  1148         # start with a semicolon for bw compat, see below
  1149         return cstr_json_dumps({u'mainvars': sorted(self.mainvars),
  1149         return ';' + ','.join(sorted(self.mainvars)) + ';' + self.expression
  1150                                 u'expression': self.expression})
  1150 
  1151 
  1151     @classmethod
  1152     @classmethod
  1152     def deserialize(cls, value):
  1153     def deserialize(cls, value):
  1153         _, mainvars, expression = value.split(';', 2)
  1154         try:
  1154         return cls(expression, mainvars)
  1155             d = cstr_json_loads(value)
       
  1156             return cls(d['expression'], d['mainvars'])
       
  1157         except ValueError:
       
  1158             _, mainvars, expression = value.split(';', 2)
       
  1159             return cls(expression, mainvars)
  1155 
  1160 
  1156     def check(self, entity, rtype, value):
  1161     def check(self, entity, rtype, value):
  1157         """return true if the value satisfy the constraint, else false"""
  1162         """return true if the value satisfy the constraint, else false"""
  1158         # implemented as a hook in the repository
  1163         # implemented as a hook in the repository
  1159         return 1
  1164         return 1
  1197     def __init__(self, expression, mainvars=None, msg=None):
  1202     def __init__(self, expression, mainvars=None, msg=None):
  1198         super(RepoEnforcedRQLConstraintMixIn, self).__init__(expression, mainvars)
  1203         super(RepoEnforcedRQLConstraintMixIn, self).__init__(expression, mainvars)
  1199         self.msg = msg
  1204         self.msg = msg
  1200 
  1205 
  1201     def serialize(self):
  1206     def serialize(self):
  1202         # start with a semicolon for bw compat, see below
  1207         return cstr_json_dumps({
  1203         return ';%s;%s\n%s' % (','.join(sorted(self.mainvars)), self.expression,
  1208             u'mainvars': sorted(self.mainvars),
  1204                                self.msg or '')
  1209             u'expression': self.expression,
       
  1210             u'msg': self.msg})
  1205 
  1211 
  1206     @classmethod
  1212     @classmethod
  1207     def deserialize(cls, value):
  1213     def deserialize(cls, value):
  1208         value, msg = value.split('\n', 1)
  1214         try:
  1209         _, mainvars, expression = value.split(';', 2)
  1215             d = cstr_json_loads(value)
  1210         return cls(expression, mainvars, msg)
  1216             return cls(d['expression'], d['mainvars'], d['msg'])
       
  1217         except ValueError:
       
  1218             value, msg = value.split('\n', 1)
       
  1219             _, mainvars, expression = value.split(';', 2)
       
  1220             return cls(expression, mainvars, msg)
  1211 
  1221 
  1212     def repo_check(self, session, eidfrom, rtype, eidto=None):
  1222     def repo_check(self, session, eidfrom, rtype, eidto=None):
  1213         """raise ValidationError if the relation doesn't satisfy the constraint
  1223         """raise ValidationError if the relation doesn't satisfy the constraint
  1214         """
  1224         """
  1215         if not self.match_condition(session, eidfrom, eidto):
  1225         if not self.match_condition(session, eidfrom, eidto):