def test_after_render_controller_curriedview(self): from pyramid.view import render_view_to_response from zope.configuration.config import ConfigurationMachine from pyramid_formish import ValidationError from pyramid.response import Response def view(context, request): return Response('response') context = ConfigurationMachine() context.route_prefix = '' context.autocommit = True context.registry = self.config.registry directive = self._makeOne(context, view=view) formdirective = DummyFormDirective() formdirective.controller = make_controller_factory( exception=ValidationError) directive.forms = [formdirective] directive.actions = [] directive.after() request = testing.DummyRequest() request.params = {'__formish_form__': 'form_id', 'submit': True} display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'response') self.assertEqual(len(request.forms), 1)
def test_after_outside_forms_context(self): import webob.multidict import schemaish from pyramid.view import render_view_to_response from pyramid_formish.zcml import FormAction from zope.configuration.config import ConfigurationMachine context = ConfigurationMachine() context.route_prefix = '' context.autocommit = True context.registry = self.config.registry request = testing.DummyRequest() request.registry = self.config.registry title = schemaish.String() factory = make_controller_factory(fields=[('title', title)]) directive = self._makeOne(context, factory) directive._actions = [FormAction('submit', 'title', True)] directive.after() display = render_view_to_response(None, request, '') self.assertEqual(display.body, '123') request = testing.DummyRequest() request.params = webob.multidict.MultiDict() request.params['submit'] = True display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'submitted')
def test_after_outside_forms_context(self): import webob.multidict import schemaish from pyramid.view import render_view_to_response from pyramid_formish.zcml import FormAction from zope.configuration.config import ConfigurationMachine context = ConfigurationMachine() context.route_prefix = '' context.autocommit = True context.registry = self.config.registry request = testing.DummyRequest() request.registry = self.config.registry title = schemaish.String() factory = make_controller_factory(fields=[('title', title)]) directive = self._makeOne(context, factory) directive._actions = [FormAction('submit','title',True)] directive.after() display = render_view_to_response(None, request, '') self.assertEqual(display.body, '123') request = testing.DummyRequest() request.params = webob.multidict.MultiDict() request.params['submit'] = True display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'submitted')
def xmlconfig(s, config=config): from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.xmlconfig import string context = ConfigurationMachine() context.autocommit = True context.registry = config.registry context.route_prefix = None context.actions = config.action_state.actions registerCommonDirectives(context) string(s, context=context, execute=False) config.commit()
def test_after_render_controller_submission(self): from pyramid.view import render_view_to_response from zope.configuration.config import ConfigurationMachine context = ConfigurationMachine() context.route_prefix = '' context.registry = self.config.registry context.autocommit = True directive = self._makeOne(context, view=None) directive.forms = [DummyFormDirective()] directive.actions = [] directive.after() request = testing.DummyRequest() request.params = {'__formish_form__': 'form_id', 'submit': True} display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'submitted') self.assertEqual(len(request.forms), 1)
def test_after_render_controller_submission(self): from pyramid.view import render_view_to_response from zope.configuration.config import ConfigurationMachine context = ConfigurationMachine() context.route_prefix = '' context.registry = self.config.registry context.autocommit = True directive = self._makeOne(context, view=None) directive.forms = [DummyFormDirective()] directive.actions = [] directive.after() request = testing.DummyRequest() request.params = {'__formish_form__':'form_id', 'submit':True} display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'submitted') self.assertEqual(len(request.forms), 1)
def xmlconfig(s, config=config): from zope.configuration.config import ConfigurationMachine from zope.configuration.xmlconfig import registerCommonDirectives from zope.configuration.xmlconfig import string context = ConfigurationMachine() context.config_class = type(config) context.autocommit = True context.registry = config.registry context.route_prefix = None context.actions = config.action_state.actions context.introspection = False registerCommonDirectives(context) string(s, context=context, execute=False) config.commit()
def test_after_render_view(self): from pyramid.view import render_view_to_response from zope.configuration.config import ConfigurationMachine from pyramid.response import Response def view(context, request): return Response('response') context = ConfigurationMachine() context.route_prefix = '' context.registry = self.config.registry context.autocommit = True directive = self._makeOne(context, view=view) directive.forms = [DummyFormDirective()] directive.actions = [] directive.after() request = testing.DummyRequest() display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'response') self.assertEqual(len(request.forms), 1)
def test_after_render_controller_curriedview(self): from pyramid.view import render_view_to_response from zope.configuration.config import ConfigurationMachine from pyramid_formish import ValidationError from pyramid.response import Response def view(context, request): return Response('response') context = ConfigurationMachine() context.route_prefix = '' context.autocommit = True context.registry = self.config.registry directive = self._makeOne(context, view=view) formdirective = DummyFormDirective() formdirective.controller = make_controller_factory( exception=ValidationError) directive.forms = [formdirective] directive.actions = [] directive.after() request = testing.DummyRequest() request.params = {'__formish_form__':'form_id', 'submit':True} display = render_view_to_response(None, request, '') self.assertEqual(display.body, 'response') self.assertEqual(len(request.forms), 1)
def register_path(config, spec, discovery=False, indexes=[], request_type=None): """ Add a skin path to the current configuration state. If ``discovery`` is enabled, the path will automatically be monitored for changes. The ``indexes`` argument is an optional list of view registrations with the provided names. The ``request_type`` option decides the request type for which to make the registration. """ package_name, path = resolve_asset_spec(spec) if package_name is not None: path = pkg_resources.resource_filename(package_name, path) else: path = caller_path(path) if package_name is None: # absolute filename package = config.package else: __import__(package_name) package = sys.modules[package_name] context = ConfigurationMachine() context.registry = config.registry context.autocommit = False context.package = package context.route_prefix = getattr(config, 'route_prefix', None) directive = skins(context, path, discovery, request_type) for index in indexes: directive.view(config, index) for action in directive(): config.action(*action)
def load_zcml(self, spec='configure.zcml', lock=threading.Lock(), features=()): """ Load configuration from a :term:`ZCML` file into the current configuration state. The ``spec`` argument is an absolute filename, a relative filename, or a :term:`asset specification`, defaulting to ``configure.zcml`` (relative to the package of the method's caller). The ``features`` argument can be any iterable of strings. These are useful for conditionally including or excluding parts of a :term:`ZCML` file. """ package_name, filename = self._split_spec(spec) if package_name is None: # absolute filename package = self.package else: __import__(package_name) package = sys.modules[package_name] # To avoid breaking people's expectations of how ZCML works, we # cannot autocommit ZCML actions incrementally. If we commit actions # incrementally, configuration outcome will be controlled purely by # ZCML directive execution order, which isn't what anyone who uses # ZCML expects. So we don't autocommit each ZCML directive action # while parsing is happening, but we do make sure to commit right # after parsing if autocommit it True. context = ConfigurationMachine() for feature in features: context.provideFeature(feature) context.registry = self.registry context.autocommit = False context.package = package context.route_prefix = getattr(self, 'route_prefix', None) context.introspection = getattr(self, 'introspection', True) context.config_class = self.__class__ registerCommonDirectives(context) self.manager.push({'registry': self.registry, 'request': None}) lock.acquire() try: # old_action_state will be None for Pyramid 1.0 and 1.1, but # not for 1.2 old_action_state = getattr(self.registry, 'action_state', None) if old_action_state is not None: # For Pyramid 1.2+, we need to assign a temporary action state to # the registry, because the configurator actions must populate # the context's action list (instead of the registry action # state's action list) in order for includeOverrides to work # properly. from pyramid.config import ActionState self.registry.action_state = ActionState() self.registry.action_state.actions = context.actions xmlconfig.file(filename, package, context=context, execute=False) finally: if old_action_state is not None: # if we reassigned the action state, restore the old one (1.2 only) self.registry.action_state = old_action_state lock.release() self.manager.pop() self._ctx.actions.extend(context.actions) if self.autocommit: self.commit() return self.registry
def load_zcml(self, spec='configure.zcml', lock=threading.Lock(), features=()): """ Load configuration from a :term:`ZCML` file into the current configuration state. The ``spec`` argument is an absolute filename, a relative filename, or a :term:`asset specification`, defaulting to ``configure.zcml`` (relative to the package of the method's caller). The ``features`` argument can be any iterable of strings. These are useful for conditionally including or excluding parts of a :term:`ZCML` file. """ package_name, filename = self._split_spec(spec) if package_name is None: # absolute filename package = self.package else: __import__(package_name) package = sys.modules[package_name] # To avoid breaking people's expectations of how ZCML works, we # cannot autocommit ZCML actions incrementally. If we commit actions # incrementally, configuration outcome will be controlled purely by # ZCML directive execution order, which isn't what anyone who uses # ZCML expects. So we don't autocommit each ZCML directive action # while parsing is happening, but we do make sure to commit right # after parsing if autocommit it True. context = ConfigurationMachine() for feature in features: context.provideFeature(feature) context.registry = self.registry context.autocommit = False context.route_prefix = getattr(self, 'route_prefix', None) context.package = package registerCommonDirectives(context) self.manager.push({'registry':self.registry, 'request':None}) lock.acquire() try: # old_action_state will be None for Pyramid 1.0 and 1.1, but # not for 1.2 old_action_state = getattr(self.registry, 'action_state', None) if old_action_state is not None: # For Pyramid 1.2+, we need to assign a temporary action state to # the registry, because the configurator actions must populate # the context's action list (instead of the registry action # state's action list) in order for includeOverrides to work # properly. from pyramid.config import ActionState self.registry.action_state = ActionState() self.registry.action_state.actions = context.actions xmlconfig.file(filename, package, context=context, execute=False) finally: if old_action_state is not None: # if we reassigned the action state, restore the old one (1.2 only) self.registry.action_state = old_action_state lock.release() self.manager.pop() _ctx = self._ctx if _ctx is None: # pragma: no cover ; will never be true under 1.2a5+ _ctx = self._ctx = self._make_context(self.autocommit) _ctx.actions.extend(context.actions) if self.autocommit: self.commit() return self.registry