99from plone .app .linkintegrity .exceptions import LinkIntegrityNotificationException
1010from plone .app .uuid .utils import uuidToObject
1111from plone .dexterity .fti import DexterityFTI
12- from plone .dexterity .utils import addContentToContainer
1312from plone .dexterity .utils import createContent
1413from plone .uuid .interfaces import IUUID
1514from Products .CMFCore .WorkflowCore import WorkflowException
@@ -69,6 +68,12 @@ def create(
6968 if isinstance (fti , DexterityFTI ):
7069 # For dexterity objects we want to not use the invokeFactory
7170 # method because we want to have the id generated by the name chooser
71+ if not fti .isConstructionAllowed (container ):
72+ raise ValueError (f"Cannot create { type } " )
73+
74+ container_fti = container .getTypeInfo ()
75+ if container_fti is not None and not container_fti .allowType (type ):
76+ raise ValueError (f"Disallowed subobject type: { type } " )
7277 constraints = IConstrainTypes (container , None )
7378 if constraints and fti not in constraints .allowedContentTypes ():
7479 raise ValueError (
@@ -78,18 +83,23 @@ def create(
7883 kwargs ["id" ] = id
7984 content = createContent (type , ** kwargs )
8085
81- if not content .id :
82- content .id = INameChooser (container ).chooseName (title , content )
83- if not safe_id :
84- content .id = INameChooser (container ).chooseName (
85- content .id or title , content
86- )
87- if id and id != content .id :
88- raise BadRequest (
89- "The id you provided conflicts with an existing object or it is reserved"
90- )
91- addContentToContainer (container , content )
92- content_id = content .id
86+ name_chooser = INameChooser (container )
87+ if content .id :
88+ # Check that the id we picked is valid
89+ if not name_chooser .checkName (content .id , content ):
90+ if safe_id :
91+ content .id = INameChooser (container ).chooseName (
92+ content .id , content
93+ )
94+ else :
95+ raise BadRequest (
96+ "The id you provided conflicts with "
97+ "an existing object or it is reserved"
98+ )
99+ else :
100+ content .id = name_chooser .chooseName (title , content )
101+
102+ content_id = container ._setObject (content .id , content )
93103 else :
94104 content_id = not safe_id and id or str (random .randint (0 , 99999999 ))
95105 container .invokeFactory (type , content_id , ** kwargs )
0 commit comments