# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""schema definition related entities"""__docformat__="restructuredtext en"fromlogilab.common.decoratorsimportcachedfromyams.schemaimportrole_namefromcubicwebimportValidationErrorfromcubicweb.schemaimportERQLExpression,RRQLExpressionfromcubicweb.entitiesimportAnyEntity,fetch_configclassCWEType(AnyEntity):__regid__='CWEType'fetch_attrs,fetch_order=fetch_config(['name'])defdc_title(self):returnu'%s (%s)'%(self.name,self._cw._(self.name))defdc_long_title(self):stereotypes=[]_=self._cw._ifself.final:stereotypes.append(_('final'))ifstereotypes:returnu'%s <<%s>>'%(self.dc_title(),', '.join(stereotypes))returnself.dc_title()defdb_key_name(self):"""XXX goa specific"""returnself.get('name')classCWRType(AnyEntity):__regid__='CWRType'fetch_attrs,fetch_order=fetch_config(['name'])defdc_title(self):returnu'%s (%s)'%(self.name,self._cw._(self.name))defdc_long_title(self):stereotypes=[]_=self._cw._ifself.symmetric:stereotypes.append(_('symmetric'))ifself.inlined:stereotypes.append(_('inlined'))ifself.final:stereotypes.append(_('final'))ifstereotypes:returnu'%s <<%s>>'%(self.dc_title(),', '.join(stereotypes))returnself.dc_title()defcheck_inlined_allowed(self):"""check inlining is possible, raise ValidationError if not possible """# don't use the persistent schema, we may miss cardinality changes# in the same transactionforrdefinself.reverse_relation_type:card=rdef.cardinality[0]ifnotcardin'?1':qname=role_name('inlined','subject')rtype=self.namestype=rdef.stypeotype=rdef.otypemsg=self._cw._("can't set inlined=True, ""%(stype)s%(rtype)s%(otype)s ""has cardinality=%(card)s")raiseValidationError(self.eid,{qname:msg%locals()})defdb_key_name(self):"""XXX goa specific"""returnself.get('name')classCWRelation(AnyEntity):__regid__='CWRelation'fetch_attrs=fetch_config(['cardinality'])[0]defdc_title(self):returnu'%s%s%s'%(self.from_entity[0].name,self.relation_type[0].name,self.to_entity[0].name)defdc_long_title(self):card=self.cardinalityscard,ocard=u'',u''ifcard[0]!='1':scard='[%s]'%card[0]ifcard[1]!='1':ocard='[%s]'%card[1]returnu'%s%s%s%s%s'%(self.from_entity[0].name,scard,self.relation_type[0].name,ocard,self.to_entity[0].name)@propertydefrtype(self):returnself.relation_type[0]@propertydefstype(self):returnself.from_entity[0]@propertydefotype(self):returnself.to_entity[0]defyams_schema(self):rschema=self._cw.vreg.schema.rschema(self.rtype.name)returnrschema.rdefs[(self.stype.name,self.otype.name)]classCWAttribute(CWRelation):__regid__='CWAttribute'defdc_long_title(self):card=self.cardinalityscard=u''ifcard[0]=='1':scard='+'returnu'%s%s%s%s'%(self.from_entity[0].name,scard,self.relation_type[0].name,self.to_entity[0].name)classCWConstraint(AnyEntity):__regid__='CWConstraint'fetch_attrs,fetch_order=fetch_config(['value'])defdc_title(self):return'%s(%s)'%(self.cstrtype[0].name,self.valueoru'')@propertydeftype(self):returnself.cstrtype[0].nameclassRQLExpression(AnyEntity):__regid__='RQLExpression'fetch_attrs,fetch_order=fetch_config(['exprtype','mainvars','expression'])defdc_title(self):returnself.expressionoru''defdc_long_title(self):return'%s(%s)'%(self.exprtype,self.expressionoru'')@propertydefexpression_of(self):forrelin('read_permission','add_permission','delete_permission','update_permission','condition'):values=getattr(self,'reverse_%s'%rel)ifvalues:returnvalues[0]@cacheddef_rqlexpr(self):ifself.exprtype=='ERQLExpression':returnERQLExpression(self.expression,self.mainvars,self.eid)#if self.exprtype == 'RRQLExpression':returnRRQLExpression(self.expression,self.mainvars,self.eid)defcheck_expression(self,*args,**kwargs):returnself._rqlexpr().check(*args,**kwargs)classCWPermission(AnyEntity):__regid__='CWPermission'fetch_attrs,fetch_order=fetch_config(['name','label'])defdc_title(self):ifself.label:return'%s (%s)'%(self._cw._(self.name),self.label)returnself._cw._(self.name)