kopia lustrzana https://github.com/simonw/datasette
rodzic
7e03394734
commit
687907aa2b
|
@ -1,6 +1,6 @@
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
from itsdangerous import BadSignature
|
from itsdangerous import BadSignature
|
||||||
import baseconv
|
from datasette.utils import baseconv
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
"""
|
||||||
|
Convert numbers from base 10 integers to base X strings and back again.
|
||||||
|
|
||||||
|
Sample usage:
|
||||||
|
|
||||||
|
>>> base20 = BaseConverter('0123456789abcdefghij')
|
||||||
|
>>> base20.from_decimal(1234)
|
||||||
|
'31e'
|
||||||
|
>>> base20.to_decimal('31e')
|
||||||
|
1234
|
||||||
|
|
||||||
|
Originally shared here: https://www.djangosnippets.org/snippets/1431/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class BaseConverter(object):
|
||||||
|
decimal_digits = "0123456789"
|
||||||
|
|
||||||
|
def __init__(self, digits):
|
||||||
|
self.digits = digits
|
||||||
|
|
||||||
|
def from_decimal(self, i):
|
||||||
|
return self.convert(i, self.decimal_digits, self.digits)
|
||||||
|
|
||||||
|
def to_decimal(self, s):
|
||||||
|
return int(self.convert(s, self.digits, self.decimal_digits))
|
||||||
|
|
||||||
|
def convert(number, fromdigits, todigits):
|
||||||
|
# Based on http://code.activestate.com/recipes/111286/
|
||||||
|
if str(number)[0] == "-":
|
||||||
|
number = str(number)[1:]
|
||||||
|
neg = 1
|
||||||
|
else:
|
||||||
|
neg = 0
|
||||||
|
|
||||||
|
# make an integer out of the number
|
||||||
|
x = 0
|
||||||
|
for digit in str(number):
|
||||||
|
x = x * len(fromdigits) + fromdigits.index(digit)
|
||||||
|
|
||||||
|
# create the result in base 'len(todigits)'
|
||||||
|
if x == 0:
|
||||||
|
res = todigits[0]
|
||||||
|
else:
|
||||||
|
res = ""
|
||||||
|
while x > 0:
|
||||||
|
digit = x % len(todigits)
|
||||||
|
res = todigits[digit] + res
|
||||||
|
x = int(x / len(todigits))
|
||||||
|
if neg:
|
||||||
|
res = "-" + res
|
||||||
|
return res
|
||||||
|
|
||||||
|
convert = staticmethod(convert)
|
||||||
|
|
||||||
|
|
||||||
|
bin = BaseConverter("01")
|
||||||
|
hexconv = BaseConverter("0123456789ABCDEF")
|
||||||
|
base62 = BaseConverter("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz")
|
|
@ -401,12 +401,12 @@ Including an expiry time
|
||||||
|
|
||||||
``ds_actor`` cookies can optionally include a signed expiry timestamp, after which the cookies will no longer be valid. Authentication plugins may chose to use this mechanism to limit the lifetime of the cookie. For example, if a plugin implements single-sign-on against another source it may decide to set short-lived cookies so that if the user is removed from the SSO system their existing Datasette cookies will stop working shortly afterwards.
|
``ds_actor`` cookies can optionally include a signed expiry timestamp, after which the cookies will no longer be valid. Authentication plugins may chose to use this mechanism to limit the lifetime of the cookie. For example, if a plugin implements single-sign-on against another source it may decide to set short-lived cookies so that if the user is removed from the SSO system their existing Datasette cookies will stop working shortly afterwards.
|
||||||
|
|
||||||
To include an expiry, add a ``"e"`` key to the cookie value containing a `base62-encoded integer <https://pypi.org/project/python-baseconv/>`__ representing the timestamp when the cookie should expire. For example, here's how to set a cookie that expires after 24 hours:
|
To include an expiry, add a ``"e"`` key to the cookie value containing a base62-encoded integer representing the timestamp when the cookie should expire. For example, here's how to set a cookie that expires after 24 hours:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import baseconv
|
from datasette.utils import baseconv
|
||||||
|
|
||||||
expires_at = int(time.time()) + (24 * 60 * 60)
|
expires_at = int(time.time()) + (24 * 60 * 60)
|
||||||
|
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -57,7 +57,6 @@ setup(
|
||||||
"PyYAML>=5.3,<7.0",
|
"PyYAML>=5.3,<7.0",
|
||||||
"mergedeep>=1.1.1,<1.4.0",
|
"mergedeep>=1.1.1,<1.4.0",
|
||||||
"itsdangerous>=1.1,<3.0",
|
"itsdangerous>=1.1,<3.0",
|
||||||
"python-baseconv==1.2.2",
|
|
||||||
],
|
],
|
||||||
entry_points="""
|
entry_points="""
|
||||||
[console_scripts]
|
[console_scripts]
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from .fixtures import app_client
|
from .fixtures import app_client
|
||||||
import baseconv
|
from datasette.utils import baseconv
|
||||||
import pytest
|
import pytest
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue