1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
""" Basic Site content type registry
$Id: ContentTypeRegistry.py,v 1.9.4.2 2002/08/04 02:42:22 efge Exp $
"""
from OFS.SimpleItem import SimpleItem, Item
from AccessControl import ClassSecurityInfo
from Globals import DTMLFile, InitializeClass, PersistentMapping
from ZPublisher.mapply import mapply
from CMFCorePermissions import ManagePortal
from utils import _dtmldir, getToolByName
import re, os, string, urllib
class MajorMinorPredicate( SimpleItem ):
"""
Predicate matching on 'major/minor' content types.
Empty major or minor implies wildcard (all match).
"""
major = minor = None
PREDICATE_TYPE = 'major_minor'
security = ClassSecurityInfo()
def __init__( self, id ):
self.id = id
security.declareProtected( ManagePortal, 'getMajorType' )
def getMajorType( self, join=string.join ):
"""
"""
if self.major is None:
return 'None'
return join( self.major )
security.declareProtected( ManagePortal, 'getMinorType' )
def getMinorType( self, join=string.join ):
"""
"""
if self.minor is None:
return 'None'
return join( self.minor )
security.declareProtected( ManagePortal, 'edit' )
def edit( self, major, minor, COMMA_SPLIT=re.compile( r'[, ]' ) ):
if major == 'None':
major = None
if type( major ) is type( '' ):
major = filter( None, COMMA_SPLIT.split( major ) )
if minor == 'None':
minor = None
if type( minor ) is type( '' ):
minor = filter( None, COMMA_SPLIT.split( minor ) )
self.major = major
self.minor = minor
#
# ContentTypeRegistryPredicate interface
#
security.declareObjectPublic()
def __call__( self, name, typ, body, SLASH_SPLIT=re.compile( '/' ) ):
"""
Return true if the rule matches, else false.
"""
if self.major is None:
return 0
if self.minor is None:
return 0
typ = typ or '/'
if not '/' in typ:
typ = typ + '/'
major, minor = SLASH_SPLIT.split( typ )
if self.major and not major in self.major:
return 0
if self.minor and not minor in self.minor:
return 0
return 1
security.declareProtected( ManagePortal, 'getTypeLabel' )
def getTypeLabel( self ):
"""
Return a human-readable label for the predicate type.
"""
return self.PREDICATE_TYPE
security.declareProtected( ManagePortal, 'predicateWidget' )
predicateWidget = DTMLFile( 'majorMinorWidget', _dtmldir )
InitializeClass( MajorMinorPredicate )
class ExtensionPredicate( SimpleItem ):
"""
Predicate matching on filename extensions.
"""
extensions = None
PREDICATE_TYPE = 'extension'
security = ClassSecurityInfo()
def __init__( self, id ):
self.id = id
security.declareProtected( ManagePortal, 'getExtensions' )
def getExtensions( self, join=string.join ):
"""
"""
if self.extensions is None:
return 'None'
return join( self.extensions )
security.declareProtected( ManagePortal, 'edit' )
def edit( self, extensions, COMMA_SPLIT=re.compile( r'[, ]' ) ):
if extensions == 'None':
extensions = None
if type( extensions ) is type( '' ):
extensions = filter( None, COMMA_SPLIT.split( extensions ) )
self.extensions = extensions
#
# ContentTypeRegistryPredicate interface
#
security.declareObjectPublic()
def __call__( self, name, typ, body ):
"""
Return true if the rule matches, else false.
"""
if self.extensions is None:
return 0
base, ext = os.path.splitext( name )
if ext and ext[ 0 ] == '.':
ext = ext[ 1: ]
return ext in self.extensions
security.declareProtected( ManagePortal, 'getTypeLabel' )
def getTypeLabel( self ):
"""
Return a human-readable label for the predicate type.
"""
return self.PREDICATE_TYPE
security.declareProtected( ManagePortal, 'predicateWidget' )
predicateWidget = DTMLFile( 'extensionWidget', _dtmldir )
InitializeClass( ExtensionPredicate )
class MimeTypeRegexPredicate( SimpleItem ):
"""
Predicate matching only on 'typ', using regex matching for
string patterns (other objects conforming to 'match' can
also be passed).
"""
pattern = None
PREDICATE_TYPE = 'mimetype_regex'
security = ClassSecurityInfo()
def __init__( self, id ):
self.id = id
security.declareProtected( ManagePortal, 'getPatternStr' )
def getPatternStr( self ):
if self.pattern is None:
return 'None'
return self.pattern.pattern
security.declareProtected( ManagePortal, 'edit' )
def edit( self, pattern ):
if pattern == 'None':
pattern = None
if type( pattern ) is type( '' ):
pattern = re.compile( pattern )
self.pattern = pattern
#
# ContentTypeRegistryPredicate interface
#
security.declareObjectPublic()
def __call__( self, name, typ, body ):
"""
Return true if the rule matches, else false.
"""
if self.pattern is None:
return 0
return self.pattern.match( typ )
security.declareProtected( ManagePortal, 'getTypeLabel' )
def getTypeLabel( self ):
"""
Return a human-readable label for the predicate type.
"""
return self.PREDICATE_TYPE
security.declareProtected( ManagePortal, 'predicateWidget' )
predicateWidget = DTMLFile( 'patternWidget', _dtmldir )
InitializeClass( MimeTypeRegexPredicate )
class NameRegexPredicate( SimpleItem ):
"""
Predicate matching only on 'name', using regex matching
for string patterns (other objects conforming to 'match'
and 'pattern' can also be passed).
"""
pattern = None
PREDICATE_TYPE = 'name_regex'
security = ClassSecurityInfo()
def __init__( self, id ):
self.id = id
security.declareProtected( ManagePortal, 'getPatternStr' )
def getPatternStr( self ):
"""
Return a string representation of our pattern.
"""
if self.pattern is None:
return 'None'
return self.pattern.pattern
security.declareProtected( ManagePortal, 'edit' )
def edit( self, pattern ):
if pattern == 'None':
pattern = None
if type( pattern ) is type( '' ):
pattern = re.compile( pattern )
self.pattern = pattern
#
# ContentTypeRegistryPredicate interface
#
security.declareObjectPublic()
def __call__( self, name, typ, body ):
"""
Return true if the rule matches, else false.
"""
if self.pattern is None:
return 0
return self.pattern.match( name )
security.declareProtected( ManagePortal, 'getTypeLabel' )
def getTypeLabel( self ):
"""
Return a human-readable label for the predicate type.
"""
return self.PREDICATE_TYPE
security.declareProtected( ManagePortal, 'predicateWidget' )
predicateWidget = DTMLFile( 'patternWidget', _dtmldir )
InitializeClass( NameRegexPredicate )
_predicate_types = []
def registerPredicateType( typeID, klass ):
"""
Add a new predicate type.
"""
_predicate_types.append( ( typeID, klass ) )
for klass in ( MajorMinorPredicate
, ExtensionPredicate
, MimeTypeRegexPredicate
, NameRegexPredicate
):
registerPredicateType( klass.PREDICATE_TYPE, klass )
class ContentTypeRegistry( SimpleItem ):
"""
Registry for rules which map PUT args to a CMF Type Object.
"""
meta_type = 'Content Type Registry'
id = 'content_type_registry'
manage_options = ( { 'label' : 'Predicates'
, 'action' : 'manage_predicates'
}
, { 'label' : 'Test'
, 'action' : 'manage_testRegistry'
}
) + SimpleItem.manage_options
security = ClassSecurityInfo()
def __init__( self ):
self.predicate_ids = ()
self.predicates = PersistentMapping()
#
# ZMI
#
security.declarePublic( 'listPredicateTypes' )
def listPredicateTypes( self ):
"""
"""
return map( lambda x: x[0], _predicate_types )
security.declareProtected( ManagePortal, 'manage_predicates' )
manage_predicates = DTMLFile( 'registryPredList', _dtmldir )
security.declareProtected( ManagePortal, 'doAddPredicate' )
def doAddPredicate( self, predicate_id, predicate_type, REQUEST ):
"""
"""
self.addPredicate( predicate_id, predicate_type )
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_predicates'
+ '?manage_tabs_message=Predicate+added.'
)
security.declareProtected( ManagePortal, 'doUpdatePredicate' )
def doUpdatePredicate( self
, predicate_id
, predicate
, typeObjectName
, REQUEST
):
"""
"""
self.updatePredicate( predicate_id, predicate, typeObjectName )
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_predicates'
+ '?manage_tabs_message=Predicate+updated.'
)
security.declareProtected( ManagePortal, 'doMovePredicateUp' )
def doMovePredicateUp( self, predicate_id, REQUEST ):
"""
"""
predicate_ids = list( self.predicate_ids )
ndx = predicate_ids.index( predicate_id )
if ndx == 0:
msg = "Predicate+already+first."
else:
self.reorderPredicate( predicate_id, ndx - 1 )
msg = "Predicate+moved."
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_predicates'
+ '?manage_tabs_message=%s' % msg
)
security.declareProtected( ManagePortal, 'doMovePredicateDown' )
def doMovePredicateDown( self, predicate_id, REQUEST ):
"""
"""
predicate_ids = list( self.predicate_ids )
ndx = predicate_ids.index( predicate_id )
if ndx == len( predicate_ids ) - 1:
msg = "Predicate+already+last."
else:
self.reorderPredicate( predicate_id, ndx + 1 )
msg = "Predicate+moved."
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_predicates'
+ '?manage_tabs_message=%s' % msg
)
security.declareProtected( ManagePortal, 'doRemovePredicate' )
def doRemovePredicate( self, predicate_id, REQUEST ):
"""
"""
self.removePredicate( predicate_id )
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_predicates'
+ '?manage_tabs_message=Predicate+removed.'
)
security.declareProtected( ManagePortal, 'manage_testRegistry' )
manage_testRegistry = DTMLFile( 'registryTest', _dtmldir )
security.declareProtected( ManagePortal, 'doTestRegistry' )
def doTestRegistry( self, name, content_type, body, REQUEST ):
"""
"""
typeName = self.findTypeName( name, content_type, body )
if typeName is None:
typeName = '<unknown>'
else:
types_tool = getToolByName(self, 'portal_types')
typeName = types_tool.getTypeInfo(typeName).Title()
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_testRegistry'
+ '?testResults=Type:+%s'
% urllib.quote( typeName )
)
#
# Predicate manipulation
#
security.declarePublic( 'getPredicate' )
def getPredicate( self, predicate_id ):
"""
Find the predicate whose id is 'id'; return the predicate
object, if found, or else None.
"""
return self.predicates.get( predicate_id, ( None, None ) )[0]
security.declarePublic( 'listPredicates' )
def listPredicates( self ):
"""
Return a sequence of tuples,
'( id, ( predicate, typeObjectName ) )'
for all predicates in the registry
"""
result = []
for predicate_id in self.predicate_ids:
result.append( ( predicate_id, self.predicates[ predicate_id ] ) )
return tuple( result )
security.declarePublic( 'getTypeObjectName' )
def getTypeObjectName( self, predicate_id ):
"""
Find the predicate whose id is 'id'; return the name of
the type object, if found, or else None.
"""
return self.predicates.get( predicate_id, ( None, None ) )[1]
security.declareProtected( ManagePortal, 'addPredicate' )
def addPredicate( self, predicate_id, predicate_type ):
"""
Add a predicate to this element of type 'typ' to the registry.
"""
if predicate_id in self.predicate_ids:
raise ValueError, "Existing predicate: %s" % predicate_id
klass = None
for key, value in _predicate_types:
if key == predicate_type:
klass = value
if klass is None:
raise ValueError, "Unknown predicate type: %s" % predicate_type
self.predicates[ predicate_id ] = ( klass( predicate_id ), None )
self.predicate_ids = self.predicate_ids + ( predicate_id, )
security.declareProtected( ManagePortal, 'updatePredicate' )
def updatePredicate( self, predicate_id, predicate, typeObjectName ):
"""
Update a predicate in this element.
"""
if not predicate_id in self.predicate_ids:
raise ValueError, "Unknown predicate: %s" % predicate_id
predObj = self.predicates[ predicate_id ][0]
mapply( predObj.edit, (), predicate.__dict__ )
self.assignTypeName( predicate_id, typeObjectName )
security.declareProtected( ManagePortal, 'removePredicate' )
def removePredicate( self, predicate_id ):
"""
Remove a predicate from the registry.
"""
del self.predicates[ predicate_id ]
idlist = list( self.predicate_ids )
ndx = idlist.index( predicate_id )
idlist = idlist[ :ndx ] + idlist[ ndx+1: ]
self.predicate_ids = tuple( idlist )
security.declareProtected( ManagePortal, 'reorderPredicate' )
def reorderPredicate( self, predicate_id, newIndex ):
"""
Move a given predicate to a new location in the list.
"""
idlist = list( self.predicate_ids )
ndx = idlist.index( predicate_id )
pred = idlist[ ndx ]
idlist = idlist[ :ndx ] + idlist[ ndx+1: ]
idlist.insert( newIndex, pred )
self.predicate_ids = tuple( idlist )
security.declareProtected( ManagePortal, 'assignTypeName' )
def assignTypeName( self, predicate_id, typeObjectName ):
"""
Bind the given predicate to a particular type object.
"""
pred, oldTypeObjName = self.predicates[ predicate_id ]
self.predicates[ predicate_id ] = ( pred, typeObjectName )
#
# ContentTypeRegistry interface
#
def findTypeName( self, name, typ, body ):
"""
Perform a lookup over a collection of rules, returning the
the name of the Type object corresponding to name/typ/body.
Return None if no match found.
"""
for predicate_id in self.predicate_ids:
pred, typeObjectName = self.predicates[ predicate_id ]
if pred( name, typ, body ):
return typeObjectName
return None
InitializeClass( ContentTypeRegistry )
def manage_addRegistry( self, REQUEST=None ):
"""
Add a CTR to self.
"""
CTRID = ContentTypeRegistry.id
reg = ContentTypeRegistry()
self._setObject( CTRID, reg )
reg = self._getOb( CTRID )
if REQUEST is not None:
REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
+ '/manage_main'
+ '?manage_tabs_message=Registry+added.'
)
|