Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

response.set_cookie() method #795

Closed
simonw opened this issue Jun 2, 2020 · 2 comments
Closed

response.set_cookie() method #795

simonw opened this issue Jun 2, 2020 · 2 comments

Comments

@simonw
Copy link
Owner

simonw commented Jun 2, 2020

Mainly to clean up this code:

datasette/datasette/app.py

Lines 439 to 454 in 4fa7cf6

def _write_messages_to_response(self, request, response):
if getattr(request, "_messages", None):
# Set those messages
cookie = SimpleCookie()
cookie["ds_messages"] = self.sign(request._messages, "messages")
cookie["ds_messages"]["path"] = "/"
# TODO: Co-exist with existing set-cookie headers
assert "set-cookie" not in response.headers
response.headers["set-cookie"] = cookie.output(header="").lstrip()
elif getattr(request, "_messages_should_clear", False):
cookie = SimpleCookie()
cookie["ds_messages"] = ""
cookie["ds_messages"]["path"] = "/"
# TODO: Co-exist with existing set-cookie headers
assert "set-cookie" not in response.headers
response.headers["set-cookie"] = cookie.output(header="").lstrip()

@simonw
Copy link
Owner Author

simonw commented Jun 9, 2020

Also:

if secrets.compare_digest(token, self.ds._root_token):
self.ds._root_token = None
cookie = SimpleCookie()
cookie["ds_actor"] = self.ds.sign({"id": "root"}, "actor")
cookie["ds_actor"]["path"] = "/"
response = Response(
body="",
status=302,
headers={
"Location": "/",
"set-cookie": cookie.output(header="").lstrip(),
},
)
return response

@simonw simonw added this to the Datasette 0.44 milestone Jun 9, 2020
@simonw simonw closed this as completed in 008e2f6 Jun 9, 2020
@simonw
Copy link
Owner Author

simonw commented Jun 9, 2020

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant