1 | ## Controller Python Script "createObject" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind state=state |
---|
7 | ##bind subpath=traverse_subpath |
---|
8 | ##parameters=id=None,type_name=None,script_id=None |
---|
9 | ##title= |
---|
10 | ## |
---|
11 | |
---|
12 | from DateTime import DateTime |
---|
13 | from Products.CMFPlone.utils import transaction_note |
---|
14 | from Products.CMFPlone import PloneMessageFactory as _ |
---|
15 | from Products.CMFCore.utils import getToolByName |
---|
16 | REQUEST=context.REQUEST |
---|
17 | |
---|
18 | if type_name == 'CaptchaField': |
---|
19 | id = 'key' |
---|
20 | |
---|
21 | if id is None: |
---|
22 | id=context.generateUniqueId(type_name) |
---|
23 | |
---|
24 | if type_name is None: |
---|
25 | raise Exception, 'Type name not specified' |
---|
26 | |
---|
27 | types_tool = getToolByName(context, 'portal_types') |
---|
28 | |
---|
29 | fti = types_tool.getTypeInfo(type_name) |
---|
30 | |
---|
31 | if not fti.queryMethodID('edit') and \ |
---|
32 | not fti.getActionObject('object/edit'): |
---|
33 | state.setStatus('success_no_edit') |
---|
34 | |
---|
35 | if context.portal_factory.getFactoryTypes().has_key(type_name): |
---|
36 | new_url = 'portal_factory/' + type_name + '/' + id |
---|
37 | if state.getStatus() != 'success_no_edit': |
---|
38 | new_url = new_url + '/edit' |
---|
39 | state.set(status='factory', next_action='redirect_to:string:%s'%new_url) |
---|
40 | # If there's an issue with object creation, let the factory handle it |
---|
41 | return state |
---|
42 | else: |
---|
43 | new_id = context.invokeFactory(id=id, type_name=type_name) |
---|
44 | if new_id is None or new_id == '': |
---|
45 | new_id = id |
---|
46 | o=getattr(context, new_id, None) |
---|
47 | tname = o.getTypeInfo().Title() |
---|
48 | message = _(u'${tname} has been created.', mapping={u'tname' : tname}) |
---|
49 | transaction_note('Created %s with id %s in %s' % (o.getTypeInfo().getId(), new_id, context.absolute_url())) |
---|
50 | |
---|
51 | if o is None: |
---|
52 | raise Exception |
---|
53 | |
---|
54 | if script_id: |
---|
55 | state.setId(script_id) |
---|
56 | |
---|
57 | if message: |
---|
58 | context.plone_utils.addPortalMessage(message) |
---|
59 | return state.set(context=o) |
---|