Re: getting an object x in an external method that can be passed as context to ContentInit( ... ).initialize(context)
Enrique Pérez Arnaud <[email protected]>
| Newsgroups | gmane.comp.web.zope.plone.archetypes.devel |
|---|---|
| Message-ID | <[email protected]> |
Suresh's answer sounds much better than my solution, that I give
below. Also, to answer Martin's post
The general idea is to have content types that are created according
to a ttw representation of a type of real object; a hotel, a room, a
town, a kitchen recipe, an ingredient, an artist, a painting. This is
managed by using ATSE on something like RichDocument. Then I want to
relate this content among them with Relations, and then I want to
categorize it with PloneTaxonomy.
What I did to get a context that can registerClass was to create a
plone tool from my initialize function in the root __init__.py of my
product, that has the context that is passed to initialize as an
attribute, and has a method that returns it. Then I get that tool in
my external method with getToolByName.
def initialize(context):
...
class ContextoTool( UniqueObject, SimpleItem ):
id = 'contexto_tool'
meta_type = 'Contexto Tool'
plone_tool = 1
contexto = context
def getContexto(self):
return self.contexto
Products.Objeto.ContextoTool = ContextoTool
InitializeClass(Products.Objeto.ContextoTool)
utils.ToolInit(
'Contexto Tool',
tools = (ContextoTool,),
product_name = 'Objeto',
icon = 'tool.gif'
).initialize(context)
This seems to work, though I guess it is clear by now that I am new to
plone, and to python for that matter. In any case, I have encountered
more problems.
This is what I have done so far:
In my Products I create two types, one folderish called "Almacen"
based on SchemaEditor and ATFolder and another "Objeto" wich is a copy
of Martin's RichDocument with the addition of atse stuff (atse_managed
for the fields of the schema, parentManagedSchema as base class for
Objeto. Almacen, in addition, has a field called "Contenido", the
mutator of which (hazContenido) triggers the creation of a new content
type, based on Objeto, with its schema managed by the almacen
instance.
the mutator:
def hazContenido(self,Contenido):
""" """
if Contenido:
clase = self.instalarContenido( Contenido)
self.allowed_content_types = (Contenido,)
self.atse_registerSchema(Contenido, getattr(clase, 'schema'))
self.Contenido=Contenido
the external method instalarContenido is basically a copy of
RichDocument's install function with this prepended:
from Products.Objeto.content import objeto
Objeto = objeto.Objeto
def instalarContenido( self, Contenido ):
context = getToolByName(self,'portal_url').getPortalObject()
contexto = getToolByName(self,'contexto_tool').getContexto()
class_name=Contenido
out = StringIO()
typeinfo_name = 'Objeto: '+Contenido+ ' ('+Contenido+')'
fti_meta_type = FactoryTypeInformation.meta_type
portal_types=getToolByName(context,'portal_types')
archTool=getToolByName(context,'archetype_tool')
clase = type.__new__(type,Contenido,(Objeto,),{
'__implements__':Objeto.__implements__,
'id': Contenido,
'portal_type': Contenido,
'meta_type': Contenido,
'archetype_name': Contenido,
'product': PROJECTNAME,
'typeDescription': 'Un '+Contenido,
'typeDescMsgId': Contenido+'_description_edit',
'_at_rename_after_creation': True,
'schema':Objeto.schema.copy(),
'actions':ATDocument.actions,
'allowed_content_types':['Imagen', 'Documento'],
'global_allow':1,
'default_view':'objeto_view',
'immediate_view':'objeto_view',
'suppl_views':('objeto_view_preview', 'objeto_view_float'),
'security':Objeto.security,
### These following attributes I had to add due to repeated errors I
write about below
'__module__': 'Products.Objeto.content.objeto',
'portal_url': getToolByName(self, 'portal_url'),
'mimetypes_registry': getToolByName(self, 'mimetypes_registry'),
'portal_discussion': getToolByName(self, 'portal_discussion'),
'allow_discussion': True,
})
objeto.__dict__[Contenido] = clase
def addCosa(self, id, **kwargs):
o = clase(id)
self._setObject(id, o)
o = getattr(self, id)
o.initializeArchetype(**kwargs)
return id
addCosa = function(addCosa.func_code,
addCosa.func_globals,
'add'+Contenido,
addCosa.func_defaults,
addCosa.func_closure,
)
objeto.__dict__['add'+Contenido] = addCosa
registerType(clase)
arquetipo = getType(Contenido, PROJECTNAME)
types = (arquetipo,)
content_types, constructors, ftis = process_types(
types,
PROJECTNAME)
utils.ContentInit(
PROJECTNAME + ' Content',
content_types = content_types,
permission = DEFAULT_ADD_CONTENT_PERMISSION,
extra_constructors = constructors,
fti = ftis,
).initialize(contexto)
installTypes(context,out,types,PROJECTNAME)
...
and then the rest of the install function.
This goes well up to creating an Almacen and editing the schema of its
content. When I try to create new content, innumerable problems crop
up. AttributeError: 'module'. AttributeError: 'portal_url'.
AttributeError: 'mimetype_registry'. 'portal_discussion'. I have
"solved" those by giving attributes to my ghost Contenido type, the
ones that follow the comment. I know there has to be a better way, i
should delve deeper into the source, but I am working against the
time. The last error i am getting says:
------
2005-12-02T00:20:26 ERROR Archetypes Catched Unauthorized on
discussiontool.overrideDiscussionFor()
Traceback (most recent call last):
File "/var/lib/zope/Products/Archetypes/ExtensibleMetadata.py", line
256, in allowDiscussion
dtool.overrideDiscussionFor(self, allowDiscussion)
File "/var/lib/zope/Products/CMFDefault/DiscussionTool.py", line 88,
in overrideDiscussionFor
raise AccessControl_Unauthorized
Unauthorized: <AccessControl.unauthorized.Unauthorized instance at 0xb056f5ec>
I guess that I should be putting the newly created types somewhere
else, since they are not in the filesystem, but I am not sure where
and how.
Any pointers?
--
Enrique Pérez
[email protected]
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click