Move CORS headers into base class, refs #1922

This commit is contained in:
Simon Willison 2022-11-30 15:48:32 -08:00
commit 9a1536b52a

View file

@ -78,15 +78,10 @@ class BaseView:
) )
else: else:
response = Response.text("Method not allowed", status=405) response = Response.text("Method not allowed", status=405)
if self.ds.cors:
add_cors_headers(response.headers)
return response return response
async def options(self, request, *args, **kwargs): async def options(self, request, *args, **kwargs):
r = Response.text("ok") return Response.text("ok")
if self.ds.cors:
add_cors_headers(r.headers)
return r
async def get(self, request, *args, **kwargs): async def get(self, request, *args, **kwargs):
return await self.method_not_allowed(request) return await self.method_not_allowed(request)
@ -107,7 +102,10 @@ class BaseView:
if self.ds: if self.ds:
await self.ds.refresh_schemas() await self.ds.refresh_schemas()
handler = getattr(self, request.method.lower(), None) handler = getattr(self, request.method.lower(), None)
return await handler(request) response = await handler(request)
if self.ds.cors:
add_cors_headers(response.headers)
return response
async def render(self, templates, request, context=None): async def render(self, templates, request, context=None):
context = context or {} context = context or {}