def test_resolve_traverse(): reg = get_registry() lookup = get_lookup(reg) reg.register(generic.consume, [Request, Container], Traverser(traverse_container)) base = get_structure() request = get_request(path='/a', lookup=lookup) request.mounted = base obj = resolve_model(request) assert obj is base['a'] assert request.unconsumed == [] assert request.lookup is lookup request = get_request(path='/sub', lookup=lookup) request.mounted = base obj = resolve_model(request) assert obj is base['sub'] assert request.unconsumed == [] assert request.lookup is lookup request = get_request(path='/sub/b', lookup=lookup) request.mounted = base obj = resolve_model(request) assert obj is base['sub']['b'] assert request.unconsumed == [] assert request.lookup is lookup # there is no /c request = get_request(path='/c', lookup=lookup) request.mounted = base obj = resolve_model(request) assert obj is base assert request.unconsumed == ['c'] assert request.lookup is lookup # there is a sub, but no c in sub request = get_request(path='/sub/c', lookup=lookup) request.mounted = base obj = resolve_model(request) assert obj is base['sub'] assert request.unconsumed == ['c'] assert request.lookup is lookup
def load(validator, request): newreq = request.app.request_class(request.environ.copy(), request.app, path_info=urllib.parse.unquote( request.path)) context = resolve_model(newreq) context.request = request schema = context.schema.get_schema(ordered=True) form_validators = request.app.get_jslcrud_formvalidators(context.schema) params = {} validator.check_schema(schema) v = validator(schema) data = get_data(context, request) field_errors = sorted(v.iter_errors(data), key=lambda e: e.path) if field_errors: params['field_errors'] = field_errors form_errors = [] for form_validator in form_validators: e = form_validator(request, request.json) if e: form_errors.append(FormValidationError(e)) if form_errors: params['form_errors'] = form_errors if params: raise ValidationError(**params) return request.json
def test_resolve_no_consumers(): lookup = get_lookup(get_registry()) base = object() stack = parse_path(u'/a') obj, unconsumed, lookup = resolve_model(base, stack, lookup) assert obj is base assert unconsumed == [(DEFAULT, u'a')] assert lookup is lookup
def test_resolve_no_consumers(): lookup = get_lookup(get_registry()) request = get_request(path='/a', lookup=lookup) base = object() obj = resolve_model(request, base) assert obj is base assert request.unconsumed == [u'a'] assert request.lookup is lookup
def test_resolve_traverse(): reg = get_registry() lookup = get_lookup(reg) reg.register(generic.consumer, [Container], Traverser(traverse_container)) base = get_structure() assert resolve_model(base, parse_path(u'/a'), lookup) == ( base['a'], [], lookup) assert resolve_model(base, parse_path(u'/sub'), lookup) == ( base['sub'], [], lookup) assert resolve_model(base, parse_path(u'/sub/b'), lookup) == ( base['sub']['b'], [], lookup) # there is no /c assert resolve_model(base, parse_path(u'/c'), lookup) == ( base, [(DEFAULT, u'c')], lookup) # there is a sub, but no c in sub assert resolve_model(base, parse_path(u'/sub/c'), lookup) == ( base['sub'], [(DEFAULT, u'c')], lookup)
def load(validator, schema, request): newreq = request.app.request_class(request.environ.copy(), request.app.root, path_info=urllib.parse.unquote( request.path)) context = resolve_model(newreq) context.request = request if schema is None: dc = context.schema else: dc = schema data = get_data(context, request) dc.validate(request, data) return request.json
def test_resolve_no_consumers(): lookup = get_lookup(get_registry()) request = get_request(path='/a', lookup=lookup) class DummyBase(object): lookup = None def set_implicit(self): pass base = DummyBase() request.mounted = base obj = resolve_model(request) assert obj is base assert request.unconsumed == [u'a'] assert request.lookup is lookup
def render(content, request: morepath.Request): main_template = loader.load('master/main_template.pt', 'xml') load_template = functools.partial(loader.load, format='xml') context = resolve_model(request.copy(app=request.app)) def _permits(permission, request=request, context=context): return permits(request, context, permission) variables = { 'request': request, 'context': context, 'main_template': main_template, 'app': request.app, 'permits': _permits, 'settings': request.app.settings, 'load_template': load_template } variables.update(content or {}) return original_render(template.render(**variables), request)
def cors_handler(request): preflight = request.method == 'OPTIONS' if preflight: response = request.ResponseClass() else: response = handler(request) try: context = resolve_model(request) or app except HTTPUnauthorized: context = None except Exception: context = app _marker = [] if getattr(request, 'view_name', _marker) is _marker: request.view_name = get_view_name(request.unconsumed) # Access-Control-Allow-Methods requested_method = request.headers.get('Access-Control-Request-Method') if preflight: allowed_methods = app.get_cors_allowed_methods( context, request, requested_method) if not allowed_methods: return request.ResponseClass(status_code=404) response.headers.add('Access-Control-Allow-Methods', ','.join(['OPTIONS'] + allowed_methods)) # Access-Control-Allow-Headers requested_headers = request.headers.get( 'Access-Control-Request-Headers') if preflight: allowed_headers = app.get_cors_allowed_headers( context, request, requested_headers) response.headers.add('Access-Control-Allow-Headers', ','.join(allowed_headers)) # Access-Control-Allow-Origin requested_origin = request.headers.get('Origin') if requested_origin or preflight: allowed_origin = app.get_cors_allowed_origin( context, request, requested_origin) response.headers.add('Access-Control-Allow-Origin', allowed_origin) if allowed_origin and allowed_origin != '*': response.headers.add('Vary', 'Origin') # Access-Control-Expose-Headers exposed_headers = app.get_cors_expose_headers(context, request) response.headers.add('Access-Control-Expose-Headers', ','.join(exposed_headers)) # Access-Control-Allow-Credentials allow_credentials = app.get_cors_allow_credentials(context, request) if allow_credentials is True: response.headers.add('Access-Control-Allow-Credentials', 'true') if preflight: max_age = app.get_cors_max_age(context, request) response.headers.add('Access-Control-Max-Age', str(max_age)) return response