def execute(self, args): model_cls = creatable_models.get(args.type) form = RawDataValidatingFactory(args.keywords, model_cls, marker=getattr(self.current_obj, '__contains__', None)) if form.errors: form.write_errors(to=self) return obj = form.create() vh = PreValidateHookMixin(obj) try: blocking_yield(vh.validate_hook(self.protocol.principal)) except Exception: msg = 'Cancelled executing "%s" due to validate_hook failure' % self.name self.write('%s\n' % msg) log.msg(msg, system='set') return interaction = self.protocol.interaction if not interaction: auth = getUtility(IAuthentication, context=None) principal = auth.getPrincipal(None) else: principal = interaction.participations[0].principal obj.__owner__ = principal obj_id = self.current_obj.add(obj) self.write("%s\n" % obj_id)
def arguments(self, parser, args, rest): parser.declare_argument('keywords', {}) model_or_obj, args_required = ((creatable_models.get(args.type), True) if self.context.name == 'mk' else (self.context.traverse(args.path), False)) schema_fields = get_schema_fields(model_or_obj, marker=getattr( self.context.current_obj, '__contains__', None)) for name, field, schema in schema_fields: if field.readonly: continue field = field.bind(model_or_obj) choices = ([i.value.encode('utf-8') for i in field.vocabulary] if isinstance(field, zope.schema.Choice) else None) type = (int if isinstance(field, zope.schema.Int) else None) kwargs = {} if isinstance(field, Path): kwargs['is_path'] = True base_path = '.' if field.relative_to == Path.PARENT: if self.context.name == 'mk': base_path = self.context.protocol._cwd() else: base_path = canonical_path(model_or_obj.__parent__) kwargs['base_path'] = os.path.join(base_path, field.base_path) parser.add_argument('=%s' % name, required=(args_required and field.required), type=type, action=GroupDictAction, group='keywords', help=field.title.encode('utf8'), choices=choices, **kwargs) return parser
def arguments(self, parser, args, rest): parser.declare_argument('keywords', {}) model_or_obj, args_required = ((creatable_models.get(args.type), True) if self.context.name == 'mk' else (self.context.traverse(args.path), False)) schema_fields = get_schema_fields(model_or_obj, marker=getattr(self.context.current_obj, '__contains__', None)) for name, field, schema in schema_fields: if field.readonly: continue field = field.bind(model_or_obj) choices = ([i.value.encode('utf-8') for i in field.vocabulary] if isinstance(field, zope.schema.Choice) else None) type = (int if isinstance(field, zope.schema.Int) else None) kwargs = {} if isinstance(field, Path): kwargs['is_path'] = True base_path = '.' if field.relative_to == Path.PARENT: if self.context.name == 'mk': base_path = self.context.protocol._cwd() else: base_path = canonical_path(model_or_obj.__parent__) kwargs['base_path'] = os.path.join(base_path, field.base_path) parser.add_argument('=%s' % name, required=(args_required and field.required), type=type, action=GroupDictAction, group='keywords', help=field.title.encode('utf8'), choices=choices, **kwargs) return parser