def toEditableString(self, descriptions=False): obj = self.context with closing(StringIO()) as s: data = [(key, value, field) for (key, value), field in zip(model_to_dict(obj).items(), model_to_dict(obj, use_fields=True).keys())] for key, value, field in data: if isinstance(value, dict): pretty_value = ', '.join(['%s:%s' % i for i in value.items()]) elif hasattr(value, '__iter__'): strings = [str(i) for i in value] if not isinstance(value, tuple): strings = sorted(strings) pretty_value = ', '.join(strings) else: pretty_value = value if field.readonly: continue if descriptions and field.title: print >>s, "##", field.title if field.description: print >>s, "#", field.description print >>s, "%s = %s" % (key.encode('utf8'), str(pretty_value).encode('utf8')) if descriptions: print >>s, '' return s.getvalue()
def _do_cat(self, obj, attrs, filename=None): name = '%s: ' % filename if filename else '' data = [(key, value, name + title) for (key, value), title in zip(model_to_dict(obj).items(), model_to_dict(obj, use_titles=True).keys()) if key in attrs or not attrs] log.msg('data: %s' % data, system='cat-cmd') if data: max_title_len = max(len(title) for key, _, title in data) for key, value, title in data: if isinstance(value, dict): # security proxies don't mimic tuple() perfectly # thus cannot be passed to "%" directly pretty_value = ', '.join(['%s:%s' % tuple(i) for i in value.items()]) elif hasattr(value, '__iter__'): strings = [str(i) for i in value] if not isinstance(value, tuple): strings = sorted(strings) pretty_value = ', '.join(strings) elif key in ('mtime', 'ctime'): pretty_value = datetime.datetime.fromtimestamp(value).isoformat() else: pretty_value = value self.write("%s\t%s\n" % ((title.encode('utf8') + ':').ljust(max_title_len), str(pretty_value).encode('utf8'))) if not attrs and IIncomplete.providedBy(obj): self.write("-----------------\n") self.write("This %s is incomplete.\n" % (type(removeSecurityProxy(obj)).__name__))
def test_schema(self): auth = getUtility(IAuthentication, context=None) auth.registerPrincipal(User('userSchema')) prinperG.grantPermissionToPrincipal('read', 'userSchema') prinperG.grantPermissionToPrincipal('modify', 'userSchema') interaction = self._get_interaction('userSchema') # get the object being secured compute = self.make_compute() compute_proxy = proxy_factory(compute, interaction) eq_(model_to_dict(compute), model_to_dict(compute_proxy))
def _do_cat(self, obj, attrs=[], add_full_paths=False): items = model_to_dict(obj).items() data = dict((key, value) for key, value in items if obj and (key in attrs or not attrs)) data['__name__'] = obj.__name__ if 'owner' in attrs or not attrs: data['owner'] = obj.__owner__ cls = type(removeSecurityProxy(obj)) data['__classname__'] = cls.__name__ data['__module__'] = cls.__module__ if 'permissions' in attrs or not attrs: data['permissions'] = self._do_cat_acl(obj) if 'tags' in data: data['tags'] = list(data['tags']) if len(data.keys()) == 0: return if add_full_paths: data.update({'path': canonical_path(obj)}) if not attrs and IIncomplete.providedBy(obj): data.update({'incomplete': True}) return data
def _do_cat(self, obj, attrs, multiline, filename=None): name = '%s: ' % filename if filename else '' data = [(key, value, name + title) for (key, value), title in zip( model_to_dict(obj).items(), model_to_dict(obj, use_titles=True).keys()) if key in attrs or not attrs] log.msg('data: %s' % data, system='cat-cmd') if data: max_title_len = max(len(title) for key, _, title in data) + 1 for key, value, title in data: if isinstance(value, dict): if multiline: separator = '\n ' + ' ' * max_title_len else: separator = ', ' # security proxies don't mimic tuple() perfectly # thus cannot be passed to "%" directly pretty_value = separator.join( ['%s: %s' % tuple(i) for i in value.items()]) elif hasattr(value, '__iter__'): strings = [str(i) for i in value] if not isinstance(value, tuple): strings = sorted(strings) if multiline: separator = '\n ' + ' ' * max_title_len else: separator = ', ' pretty_value = separator.join(strings) elif key in ('mtime', 'ctime'): pretty_value = datetime.datetime.fromtimestamp( value).isoformat() else: pretty_value = value self.write("%s %s\n" % ((title.encode('utf8') + ':').ljust(max_title_len), str(pretty_value).encode('utf8'))) if not attrs and IIncomplete.providedBy(obj): self.write("-----------------\n") self.write("This %s is incomplete.\n" % (type(removeSecurityProxy(obj)).__name__))
def render_GET(self, request): if not request.interaction.checkPermission('view', self.context): raise NotFound data = model_to_dict(self.context) data['id'] = self.context.__name__ data['__type__'] = type(removeSecurityProxy(self.context)).__name__ try: data['url'] = ILocation(self.context).get_url() except Unauthorized: data['url'] = '' interaction = get_interaction(self.context) data['permissions'] = effective_perms(interaction, self.context) if interaction else [] # XXX: simplejson can't serialize sets if 'tags' in data: data['tags'] = list(data['tags']) return data
def render_GET(self, request): if not request.interaction.checkPermission('view', self.context): raise NotFound data = model_to_dict(self.context) data['id'] = self.context.__name__ data['__type__'] = type(removeSecurityProxy(self.context)).__name__ try: data['url'] = ILocation(self.context).get_url() except Unauthorized: data['url'] = '' interaction = get_interaction(self.context) data['permissions'] = effective_perms( interaction, self.context) if interaction else [] # XXX: simplejson can't serialize sets if 'tags' in data: data['tags'] = list(data['tags']) return data