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
|
Extending a type
================
With Archetypes and ATContentTypes it's very easy to extend a type. This small
example will show you how
Code Example
============
How to extend ATEvent by adding a new field and changing an existing field:
MyATEvent.py
------------
from Products.ATContentTypes.config import HAS_LINGUA_PLONE
from Products.MyATEvent.config import *
if HAS_LINGUA_PLONE:
from Products.LinguaPlone.public import *
else:
from Products.Archetypes.public import *
from Products.CMFCore import CMFCorePermissions
from Products.CMFCore.utils import getToolByName
from AccessControl import ClassSecurityInfo
from Products.ATContentTypes.types.ATContentType import updateActions
from Products.ATContentTypes.types.schemata import ATEventSchema
from Products.ATContentTypes.types.ATEvent import ATEvent
from Products.ATContentTypes.Permissions import ChangeEvents
from Products.validation.validators.SupplValidators import MaxSizeValidator
from Products.Archetypes.Marshall import PrimaryFieldMarshaller
# at first make a copy of the ATEvent schema so we can change everything without
# conflicting (and afflicting) ATEvent
MyATEventBaseSchema = ATEventSchema.copy()
# disable validation of the phone number
MyATEventBaseSchema['contactPhone'].validators = ()
# description shouldn't be primary field because we have an image
MyATEventBaseSchema['description'].primary = False
MyATEventSchema = MyATEventBaseSchema + Schema((
ImageField('flyer',
required=0,
primary=1,
languageIndependent=True,
sizes= {'preview' : (400, 400),
'thumb' : (128, 128),
'tile' : (64, 64),
'icon' : (32, 32),
'listing' : (16, 16),
},
validators = MaxSizeValidator('checkFlyerMaxSize',
maxsize=0.5), # 500kb
widget = ImageWidget(
description = "Select a flyer",
description_msgid = "help_image_flyer",
label= "Flyer",
label_msgid = "label_image_flyer",
i18n_domain = "plone"))
), marshall=PrimaryFieldMarshaller())
# now we have a schema without phone number validation for contactPhone and a
# flyer field which can contain an image as primary field
# creating a new type called MyATEvent
class MyATEvent(ATEvent):
"""My own event based on ATEvent"""
schema = MyATEventSchema
content_icon = 'myevent_icon.gif'
meta_type = 'MyATEvent'
archetype_name = 'My AT Event'
immediate_view = 'my_event_view'
default_view = 'my_event_view'
suppl_views = ()
typeDescription= 'My cool new event'
typeDescMsgId = 'description_edit_my_event'
assocMimetypes = ()
assocFileExt = ('myevent', )
__implements__ = ATEvent.__implements__
security = ClassSecurityInfo()
registerType(MyATEvent, PROJECTNAME)
# For the rest like config.py, __init__.py and skins you should use ArchExample
# as boiler plate
|