예제 #1
0
class ContentAddFormBase(_EditFormMixin, form.PageAddForm):

    adapts(IFolderish, ICMFDefaultSkin, ITypeInformation)
    implementsOnly(IPageForm)

    security = ClassSecurityInfo()
    security.declareObjectPrivate()

    actions = form.Actions(
        form.Action(name='add',
                    label=form._('Add'),
                    condition=form.haveInputWidgets,
                    success='handle_add',
                    failure='handle_failure'),
        form.Action(name='cancel',
                    label=_(u'Cancel'),
                    validator='handle_cancel_validate',
                    success='handle_cancel_success'))

    def __init__(self, context, request, ti):
        self.context = context
        self.request = request
        self.ti = ti

    security.declareProtected(AddPortalContent, '__call__')

    def __call__(self):
        container = self.context
        portal_type = self.ti.getId()

        # check allowed (sometimes redundant, but better safe than sorry)
        if not self.ti.isConstructionAllowed(container):
            raise AccessControl_Unauthorized('Cannot create %s' % portal_type)

        # check container constraints
        ttool = getUtility(ITypesTool)
        container_ti = ttool.getTypeInfo(container)
        if container_ti is not None and \
                not container_ti.allowType(portal_type):
            raise ValueError('Disallowed subobject type: %s' % portal_type)

        return super(ContentAddFormBase, self).__call__()

    @property
    def label(self):
        obj_type = translate(self.ti.Title(), self.context)
        return _(u'Add ${obj_type}', mapping={'obj_type': obj_type})

    @property
    def description(self):
        return self.ti.Description()

    #same as in form.AddFormBase but without action decorator
    def handle_add(self, action, data):
        self.createAndAdd(data)

    def handle_cancel_success(self, action, data):
        return self._setRedirect('portal_types',
                                 ('object/folderContents', 'object/view'))

    def create(self, data):
        id = data.pop('id', '') or ''
        factory = getUtility(IFactory, self.ti.factory)
        obj = factory(id=id, **data)
        obj._setPortalTypeName(self.ti.getId())
        return obj

    def add(self, obj):
        container = self.context

        name = INameChooser(container).chooseName(obj.getId(), obj)
        obj.id = name
        container._setObject(name, obj)
        obj = container._getOb(name)

        obj_type = translate(obj.Type(), container)
        self.status = _(u'${obj_type} added.', mapping={'obj_type': obj_type})
        self._finished_add = True
        self._added_obj = obj
        return obj

    def nextURL(self):
        obj = self._added_obj

        message = translate(self.status, self.context)
        if isinstance(message, unicode):
            message = message.encode(self._getBrowserCharset())
        return '%s/%s?%s' % (obj.absolute_url(), self.ti.immediate_view,
                             make_query(portal_status_message=message))
예제 #2
0
파일: form.py 프로젝트: bendavis78/zope
class ContentAddFormBase(_EditFormMixin, PageAddForm):

    actions = form.Actions(
        form.Action(name='add',
                    label=form._('Add'),
                    condition=form.haveInputWidgets,
                    success='handle_add',
                    failure='handle_failure'),
        form.Action(name='cancel',
                    label=_(u'Cancel'),
                    success='handle_cancel_success',
                    failure='handle_cancel_failure'))

    description = u''

    @property
    def label(self):
        obj_type_id = self.widgets['portal_type']._getFormValue()
        # look it up to get an i18n message object with correct i18n domain
        ttool = self._getTool('portal_types')
        fti = ttool.getTypeInfo(obj_type_id)
        obj_type = translate(fti.Title(), self.context)
        return _(u'Add ${obj_type}', mapping={'obj_type': obj_type})

    #same as in form.AddFormBase but without action decorator
    def handle_add(self, action, data):
        self.createAndAdd(data)

    def handle_cancel_success(self, action, data):
        return self._setRedirect('portal_types',
                                 ('object/folderContents', 'object/view'))

    def handle_cancel_failure(self, action, data, errors):
        self.status = None
        return self._setRedirect('portal_types',
                                 ('object/folderContents', 'object/view'))

    def create(self, data):
        id = data.pop('id', '')
        portal_type = data.pop('portal_type')
        ttool = self._getTool('portal_types')
        fti = ttool.getTypeInfo(portal_type)
        factory = getUtility(IFactory, fti.factory)
        obj = factory(id=id, **data)
        obj._setPortalTypeName(portal_type)
        return obj

    def add(self, obj):
        container = self.context

        #check container constraints
        ttool = self._getTool('portal_types')
        container_ti = ttool.getTypeInfo(container)
        portal_type = obj.getPortalTypeName()
        if container_ti is not None and \
                not container_ti.allowType(portal_type):
            raise ValueError('Disallowed subobject type: %s' % portal_type)

        name = INameChooser(container).chooseName(obj.getId(), obj)
        obj.id = name
        container._setObject(name, obj)
        obj = container._getOb(name)

        obj_type = translate(obj.Type(), self.context)
        self.status = _(u'${obj_type} added.', mapping={'obj_type': obj_type})
        self._finished_add = True
        self._added_obj = obj
        return obj

    def nextURL(self):
        obj = self._added_obj
        fti = obj.getTypeInfo()

        message = translate(self.status, self.context)
        if isinstance(message, unicode):
            message = message.encode(self._getBrowserCharset())
        return '%s/%s?%s' % (obj.absolute_url(), fti.immediate_view,
                             make_query(portal_status_message=message))