source: publico/PortalInterlegis/produtos/il.portalinterlegis/src/il/portalinterlegis/content/createcontent.py @ 6430

Última Alteração nesse arquivo desde 6430 foi 6430, incluída por mazza, 9 anos atrás

views iniciadas para cada uma das homes

File size: 3.4 KB
Linha 
1# -*- coding: utf-8 -*-
2from Products.CMFPlone.utils import _createObjectByType
3from Products.CMFCore.utils import getToolByName
4from Products.CMFCore.WorkflowCore import WorkflowException
5import logging
6from Products.Five.utilities.marker import mark
7
8# codigo de
9# http://keeshink.blogspot.com.br/2011/05/creating-plone-content-when-installing.html
10# com alteracao para usar logger
11# http://collective-docs.readthedocs.org/en/latest/testing_and_debugging/logging.html#logging-from-python-code
12
13def createObjects(parent, children):
14    """This will create new objects, or modify existing ones if id's and type
15    match.
16
17    This takes two arguments: the parent to create the content in, and the
18    children to create.
19
20    Children is a list of dictionaries defined as follows:
21
22    new_objects = [
23        {   'id': 'some-id',
24            'title': 'Some Title',
25            'description': 'Some Description',
26            'type': 'Folder',
27            'layout': 'folder_contents',
28            'workflow_transition': 'retract',
29            'exclude_from_nav': True,
30            'children': profile_children,
31            'marker_interface': marker_interface
32            },
33        ]
34   
35    * layout:               optional, it sets a different default layout
36    * workflow_transition:  optional, it tries to start that state transition
37        after the object is created. (You cannot directly set the workflow to
38        any state, but you must push it through legal state transitions.)
39    * exclude_from_nav:     optional, excludes item from navigation
40    * children:             optional, is a list of dictionaries (like this one)
41    * marker_interface:     optional, a marker interface to apply
42
43    """
44    logger = logging.getLogger("Plone")
45    logger.info("Creating %s in %s" % (children, parent))
46
47    workflowTool = getToolByName(parent, "portal_workflow")
48
49    existing = parent.objectIds()
50    for new_object in children:
51        if new_object['id'] in existing:
52            logger.info("%s exists, skipping" % new_object['id'])
53        else:
54            _createObjectByType(new_object['type'], parent, \
55                id=new_object['id'], title=new_object['title'], \
56                description=new_object['description'])
57        logger.info("Now to modify the new_object...")
58        obj = parent.get(new_object['id'], None)
59        if obj is None:
60            logger.info("can't get new_object %s to modify it!" % new_object['id'])
61        else:
62            if obj.Type() != new_object['type']:
63                logger.info("types don't match!")
64            else:   
65                if new_object.has_key('layout'):
66                    obj.setLayout(new_object['layout'])
67                if new_object.has_key('workflow_transition'):
68                    try:
69                        workflowTool.doActionFor(obj,
70                            new_object['workflow_transition'])
71                    except WorkflowException:
72                        logger.info(
73                            "WARNING: couldn't do workflow transition")
74                if new_object.has_key('exclude_from_nav'):
75                    obj.setExcludeFromNav(new_object['exclude_from_nav'])
76                if new_object.has_key('marker_interface'):
77                    mark(obj, new_object['marker_interface'])
78                obj.reindexObject()
79                children = new_object.get('children',[])
80                if len(children) > 0:
81                    createObjects(obj, children)
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.