Esempio n. 1
0
    def createAndAdd(self, data):
        """Add the desired object using the data in the data argument.

        The data argument is a dictionary with the data entered in the form.
        """

        args = []
        if self._arguments:
            for name in self._arguments:
                args.append(data[name])

        kw = {}
        if self._keyword_arguments:
            for name in self._keyword_arguments:
                if name in data:
                    kw[str(name)] = data[name]

        content = self.create(*args, **kw)
        adapted = self.schema(content)

        errors = []

        if self._set_before_add:
            for name in self._set_before_add:
                if name in data:
                    field = self.schema[name]
                    try:
                        field.set(adapted, data[name])
                    except ValidationError:
                        errors.append(sys.exc_info()[1])

        if errors:
            raise WidgetsError(*errors)

        notify(ObjectCreatedEvent(content))

        content = self.add(content)
        adapted = self.schema(content)

        if self._set_after_add:
            for name in self._set_after_add:
                if name in data:
                    field = self.schema[name]
                    try:
                        field.set(adapted, data[name])
                    except ValidationError:
                        errors.append(sys.exc_info()[1])
            # We have modified the object, so we need to publish an
            # object-modified event:
            description = Attributes(self.schema, *self._set_after_add)
            notify(ObjectModifiedEvent(content, description))

        if errors:
            raise WidgetsError(*errors)

        return content
Esempio n. 2
0
    def update(self):
        if self.update_status is not None:
            # We've been called before. Just return the status we previously
            # computed.
            return self.update_status

        status = ''

        content = self.adapted

        if Update in self.request.form.keys():
            changed = False
            try:
                changed = applyWidgetsChanges(self,
                                              self.schema,
                                              target=content,
                                              names=self.fieldNames)
                # We should not generate events when an adapter is used.
                # That's the adapter's job.  We need to unwrap the objects to
                # compare them, as they are wrapped differently.
                # Additionally, we can't use Acquisition.aq_base() because
                # it strangely returns different objects for these two even
                # when they are identical.  In particular
                # aq_base(self.adapted) != self.adapted.aq_base :-(
                if changed and getattr(self.context, 'aq_base', self.context)\
                            is getattr(self.adapted, 'aq_base', self.adapted):
                    description = Attributes(self.schema, *self.fieldNames)
                    notify(ObjectModifiedEvent(content, description))
            except WidgetsError, errors:
                self.errors = errors
                status = _("An error occurred.")
                transaction.abort()
            else:
                setUpEditWidgets(self,
                                 self.schema,
                                 source=self.adapted,
                                 ignoreStickyValues=True,
                                 names=self.fieldNames)
                if changed:
                    self.changed()
                    # XXX: Needs locale support:
                    #formatter = self.request.locale.dates.getFormatter(
                    #    'dateTime', 'medium')
                    #status = _("Updated on ${date_time}",
                    #           mapping={'date_time':
                    #                    formatter.format(datetime.utcnow())})
                    status = _("Updated on ${date_time}",
                               mapping={'date_time': str(datetime.utcnow())})
Esempio n. 3
0
    def update(self):
        if self.update_status is not None:
            # We've been called before. Just return the status we previously
            # computed.
            return self.update_status

        status = ''

        content = self.adapted

        if Update in self.request.form.keys():
            changed = False
            try:
                changed = applyWidgetsChanges(self,
                                              self.schema,
                                              target=content,
                                              names=self.fieldNames)
                # We should not generate events when an adapter is used.
                # That's the adapter's job.
                if changed and self.context is self.adapted:
                    notify(ObjectModifiedEvent(content))
            except WidgetsError, errors:
                self.errors = errors
                status = _("An error occurred.")
                transaction.abort()
            else:
                setUpEditWidgets(self,
                                 self.schema,
                                 source=self.adapted,
                                 ignoreStickyValues=True,
                                 names=self.fieldNames)
                if changed:
                    self.changed()
                    # XXX: Needs locale support:
                    #formatter = self.request.locale.dates.getFormatter(
                    #    'dateTime', 'medium')
                    #status = _("Updated on ${date_time}",
                    #           mapping={'date_time':
                    #                    formatter.format(datetime.utcnow())})
                    status = _("Updated on ${date_time}",
                               mapping={'date_time': str(datetime.utcnow())})