Пример #1
0
    def process(self, csvfile, update=False):
        """Process the input file, validate and
        create the users.
        """
        data = tablib.Dataset()
        try:
            data.csv = csvfile
        except tablib.core.InvalidDimensions:
            return self._show_message_redirect(
                _("File incorrectly formatted.")
            )

        try:
            self.validate(data)
        except custom_exc.MissingCoreFields as e:
            return self._show_message_redirect(_(e.message))
        except custom_exc.ExtraneousFields as e:
            return self._show_message_redirect(_(e.message))
        except tablib.core.HeadersNeeded as e:
            return self._show_message_redirect(_(e.message))

        message_type = 'error'
        try:
            count = self.create_update_users(data, update)
        except custom_exc.DuplicateUser as e:
            message = _(
                u"{} on row {}".format(
                    e.message, e.details['row'])
            )
        except custom_exc.RequiredMissing as e:
            message = _(
                u"Missing required field {} on row {}".format(
                    e.message, e.details['row'])
            )
        except custom_exc.ConstraintNotSatisfied as e:
            message = _(
                u"Constraint not satisfied for {} at row {}.".format(
                    e.details['field'], e.details['row'])
            )
        except custom_exc.WrongType as e:
            message = _(
                u"Wrong type for {} at row {}.".format(
                    e.details['field'], e.details['row'])
            )
        else:
            message_type = 'info'
            verb = update and "Updated" or "Created"
            message = _(u"{} user(s) {}.".format(count, verb))

        if message_type == 'error':
            transaction.abort()

        api.portal.show_message(
            message=message,
            request=self.request,
            type=message_type,
        )
        return self._redirect()