def contenttype_directive(_context, portal_type, class_, schema, behaviors=None, add_permission=None, allowed_types=None): """ Generate factory for the passed schema """ logger.warn('plone:contenttype directive will be removed in 1.0.0 final') # prevent circular import from plone.server.content import ResourceFactory factory = ResourceFactory(class_, title='', description='', portal_type=portal_type, schema=schema, behaviors=behaviors or (), add_permission=add_permission or DEFAULT_ADD_PERMISSION, allowed_types=allowed_types) utility( _context, provides=IResourceFactory, component=factory, name=portal_type, )
def __init__(self): self.request = FakeRequest() self.request.security.add(TestParticipation(self.request)) self.request.security.invalidate_cache() self.request._cache_groups = {} parser = self.get_parser() arguments = parser.parse_args() if os.path.exists(arguments.configuration): with open(arguments.configuration, 'r') as config: settings = json.load(config) else: logger.warn('Could not find the configuration file {}. Using default settings.'.format( arguments.configuration )) settings = MISSING_SETTINGS.copy() app = self.make_app(settings) logging.basicConfig(stream=sys.stdout) logger.setLevel(logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) if arguments.debug: logger.setLevel(logging.DEBUG) logging.basicConfig( stream=sys.stdout, level=logging.DEBUG) ch.setLevel(logging.DEBUG) self.run(arguments, settings, app)
def __init__(self): self.request = FakeRequest() self.request.security.add(TestParticipation(self.request)) self.request.security.invalidate_cache() self.request._cache_groups = {} parser = self.get_parser() arguments = parser.parse_args() if os.path.exists(arguments.configuration): with open(arguments.configuration, 'r') as config: settings = json.load(config) else: logger.warn( 'Could not find the configuration file {}. Using default settings.' .format(arguments.configuration)) settings = MISSING_SETTINGS.copy() app = self.make_app(settings) logging.basicConfig(stream=sys.stdout) logger.setLevel(logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) if arguments.debug: logger.setLevel(logging.DEBUG) logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) ch.setLevel(logging.DEBUG) self.run(arguments, settings, app)
def contenttype_directive(_context, portal_type, class_, schema, behaviors=None, add_permission=None, allowed_types=None): """ Generate factory for the passed schema """ logger.warn('plone:contenttype directive will be removed in 1.0.0 final') # prevent circular import from plone.server.content import ResourceFactory factory = ResourceFactory( class_, title='', description='', portal_type=portal_type, schema=schema, behaviors=behaviors or (), add_permission=add_permission or DEFAULT_ADD_PERMISSION, allowed_types=allowed_types ) utility( _context, provides=IResourceFactory, component=factory, name=portal_type, )
def add_on(_context, name, title, handler): logger.warn('plone:addon directive will be removed in 1.0.0 final') from plone.server import app_settings if name not in app_settings['available_addons']: app_settings['available_addons'][name] = { 'title': title, 'handler': handler }
def api_directive(_context, file): # noqa 'too complex' :) logger.warn('plone:api directive will be removed in 1.0.0 final') from plone.server import app_settings if file: file = os.path.abspath(_context.path(file)) if not os.path.isfile(file): raise ConfigurationError('No such file', file) with open(file, 'r') as f: json_info = json.loads(f.read(), object_pairs_hook=OrderedDict) f.close() if 'contenttypes' in json_info: app_settings['api_definition'] = reduce( rec_merge, (json_info['contenttypes'], app_settings['api_definition'])) if 'methods' in json_info: for method, method_interface in json_info['methods'].items(): app_settings['http_methods'][method] = import_class( method_interface) if 'layer' in json_info: layer = json_info['layer'] layer = import_class(layer) if len(app_settings['default_layers']) == 0: app_settings['default_layers'].append(layer) else: layer = app_settings['default_layers'][0] if 'default_permission' in json_info: default_permission = json_info['default_permission'] if not app_settings['default_permission']: app_settings['default_permission'] = default_permission else: default_permission = app_settings['default_permission'] if 'renderers' in json_info: for accept, renderer_interface in json_info['renderers'].items(): # We define which Interface is for the content negotiation # Order is important !! app_settings['renderers'][accept] = import_class( renderer_interface) if 'languages' in json_info: for language, language_interface in json_info['languages'].items(): # We define which Interface is for the languages logger.debug(language_interface) app_settings['languages'][language] = import_class( language_interface) if 'contenttypes' in json_info: for contenttype, configuration in json_info['contenttypes'].items(): content_interface = import_class(contenttype) for method, method_configuration in configuration.items(): if method != 'endpoints': register_service(_context, method_configuration, content_interface, method, layer, default_permission) if 'endpoints' in configuration: for endpoint, endpoint_configuration in configuration[ 'endpoints'].items(): # noqa for method, method_configuration in endpoint_configuration.items( ): # noqa register_service(_context, method_configuration, content_interface, method, layer, default_permission, endpoint)
from plone.server.interfaces import IDefaultLayer # noqa from plone.server import logger logger.warn('plone.server.api.layer.IDefaultLayer has been moved to ' 'plone.server.interfaces.IDefaultLayer. This import will ' 'no longer work in version 2.0.0 of plone.server')
def api_directive(_context, file): # noqa 'too complex' :) logger.warn('plone:api directive will be removed in 1.0.0 final') from plone.server import app_settings if file: file = os.path.abspath(_context.path(file)) if not os.path.isfile(file): raise ConfigurationError('No such file', file) with open(file, 'r') as f: json_info = json.loads(f.read(), object_pairs_hook=OrderedDict) f.close() if 'contenttypes' in json_info: app_settings['api_definition'] = reduce( rec_merge, (json_info['contenttypes'], app_settings['api_definition'])) if 'methods' in json_info: for method, method_interface in json_info['methods'].items(): app_settings['http_methods'][method] = import_class(method_interface) if 'layer' in json_info: layer = json_info['layer'] layer = import_class(layer) if len(app_settings['default_layers']) == 0: app_settings['default_layers'].append(layer) else: layer = app_settings['default_layers'][0] if 'default_permission' in json_info: default_permission = json_info['default_permission'] if not app_settings['default_permission']: app_settings['default_permission'] = default_permission else: default_permission = app_settings['default_permission'] if 'renderers' in json_info: for accept, renderer_interface in json_info['renderers'].items(): # We define which Interface is for the content negotiation # Order is important !! app_settings['renderers'][accept] = import_class(renderer_interface) if 'languages' in json_info: for language, language_interface in json_info['languages'].items(): # We define which Interface is for the languages logger.debug(language_interface) app_settings['languages'][language] = import_class(language_interface) if 'contenttypes' in json_info: for contenttype, configuration in json_info['contenttypes'].items(): content_interface = import_class(contenttype) for method, method_configuration in configuration.items(): if method != 'endpoints': register_service( _context, method_configuration, content_interface, method, layer, default_permission) if 'endpoints' in configuration: for endpoint, endpoint_configuration in configuration['endpoints'].items(): # noqa for method, method_configuration in endpoint_configuration.items(): # noqa register_service( _context, method_configuration, content_interface, method, layer, default_permission, endpoint)
async def real_resolve(self, request): """Main function to resolve a request.""" alsoProvides(request, IRequest) alsoProvides(request, IDefaultLayer) request._futures = {} request.security = IInteraction(request) method = app_settings['http_methods'][request.method] language = language_negotiation(request) language_object = language(request) try: resource, tail = await self.traverse(request) except Exception as _exc: request.resource = request.tail = None request.exc = _exc # XXX should only should traceback if in some sort of dev mode? raise HTTPBadRequest(text=json.dumps({ 'success': False, 'exception_message': str(_exc), 'exception_type': getattr(type(_exc), '__name__', str(type(_exc))), # noqa 'traceback': traceback.format_exc() })) request.resource = resource request.tail = tail if request.resource is None: raise HTTPBadRequest(text='Resource not found') traverse_to = None if tail and len(tail) == 1: view_name = tail[0] elif tail is None or len(tail) == 0: view_name = '' else: view_name = tail[0] traverse_to = tail[1:] await self.apply_authorization(request) translator = queryMultiAdapter( (language_object, resource, request), ITranslated) if translator is not None: resource = translator.translate() # Add anonymous participation if len(request.security.participations) == 0: # logger.info("Anonymous User") request.security.add(AnonymousParticipation(request)) # Site registry lookup try: view = queryMultiAdapter( (resource, request), method, name=view_name) except AttributeError: view = None # Traverse view if its needed if traverse_to is not None and view is not None: if not ITraversableView.providedBy(view): return None else: try: view = view.publishTraverse(traverse_to) except Exception as e: logger.error( "Exception on view execution", exc_info=e) return None permission = getUtility(IPermission, name='plone.AccessContent') allowed = request.security.checkPermission(permission.id, resource) if not allowed: # Check if its a CORS call: if IOPTIONS != method or not app_settings['cors']: # Check if the view has permissions explicit if view is None or not view.__allow_access__: logger.warn("No access content {content} with {auths}".format( content=resource, auths=str([x.principal.id for x in request.security.participations]))) raise HTTPUnauthorized() if view is None and method == IOPTIONS: view = DefaultOPTIONS(resource, request) checker = getCheckerForInstancesOf(view.__class__) if checker is not None: view = ProxyFactory(view, checker) # We want to check for the content negotiation renderer = content_type_negotiation(request, resource, view) renderer_object = renderer(request) rendered = queryMultiAdapter( (renderer_object, view, request), IRendered) if rendered is not None: return MatchInfo(resource, request, view, rendered) else: return None