12345678910111213141516171819202122232425262728 |
- from django.conf import settings
- import datetime
- import json
- def set_cookie(response, key, value, days_expire = 7):
- if days_expire is None:
- max_age = 365 * 24 * 60 * 60 # one year
- else:
- max_age = days_expire * 24 * 60 * 60
- expires = datetime.datetime.strftime(
- datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age),
- '%a, %d-%b-%Y %H:%M:%S GMT'
- )
- response.set_cookie(
- key,
- json.dumps(value),
- max_age=max_age,
- expires=expires,
- domain=settings.SESSION_COOKIE_DOMAIN,
- secure=settings.SESSION_COOKIE_SECURE or None
- )
- def get_cookie(request, key, default):
- strval = request.COOKIES.get(key)
- if strval:
- return json.loads(strval)
- return default
|