def setCookie(self, name, value, path='/', expires='ONCLOSE', secure=False): """Set a cookie. You can also set the path (which defaults to /). You can also set when it expires. It can expire: - 'NOW': this is the same as trying to delete it, but it doesn't really seem to work in IE - 'ONCLOSE': the default behavior for cookies (expires when the browser closes) - 'NEVER': some time in the far, far future. - integer: a timestamp value - tuple or struct_time: a tuple, as created by the time module - datetime: a datetime.datetime object for the time (if without time zone, assumed to be *local*, not GMT time) - timedelta: a duration counted from the present, e.g., datetime.timedelta(days=14) (2 weeks in the future) - '+...': a time in the future, '...' should be something like 1w (1 week), 3h46m (3:45), etc. You can use y (year), b (month), w (week), d (day), h (hour), m (minute), s (second). This is done by the MiscUtils.DateInterval. """ cookie = Cookie(name, value) t = expires if isinstance(t, str): if t == 'ONCLOSE': t = None elif t == 'NOW': cookie.delete() return elif t == 'NEVER': t = gmtime() t = (t[0] + 10, ) + t[1:] elif t.startswith('+'): t = time() + timeDecode(t[1:]) if t: if isinstance(t, (int, float)): t = gmtime(t) if isinstance(t, (tuple, struct_time)): t = strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) if isinstance(t, timedelta): t = datetime.now() + t if isinstance(t, datetime): d = t.utcoffset() if d is None: d = localTimeDelta() t -= d t = t.strftime("%a, %d-%b-%Y %H:%M:%S GMT") cookie.setExpires(t) if path: cookie.setPath(path) if secure: cookie.setSecure(secure) self.addCookie(cookie)
def setCookie(self, name, value, path='/', expires='ONCLOSE', secure=False): """Set a cookie. You can also set the path (which defaults to /). You can also set when it expires. It can expire: 'NOW': this is the same as trying to delete it, but it doesn't really seem to work in IE 'ONCLOSE': the default behavior for cookies (expires when the browser closes) 'NEVER': some time in the far, far future. integer: a timestamp value tuple: a tuple, as created by the time module DateTime: an mxDateTime object for the time (assumed to be *local*, not GMT time) DateTimeDelta: a interval from the present, e.g., DateTime.DateTimeDelta(month=1) (1 month in the future) '+...': a time in the future, '...' should be something like 1w (1 week), 3h46m (3:45), etc. You can use y (year), b (month), w (week), d (day), h (hour), m (minute), s (second). This is done by the MiscUtils.DateInterval. """ cookie = Cookie(name, value) if expires == 'ONCLOSE' or not expires: pass # this is already default behavior elif expires == 'NOW': cookie.delete() return elif expires == 'NEVER': t = time.gmtime(time.time()) if expires == 'NEVER': t = (t[0] + 10,) + t[1:] t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) cookie.setExpires(t) else: t = expires if type(t) is StringType and t and t[0] == '+': interval = timeDecode(t[1:]) t = time.time() + interval if type(t) in (IntType, LongType, FloatType): t = time.gmtime(t) if type(t) in (TupleType, TimeTupleType): t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) if DateTime and \ (type(t) is DateTime.DateTimeDeltaType or isinstance(t, DateTime.RelativeDateTime)): t = DateTime.now() + t if DateTime and type(t) is DateTime.DateTimeType: t = (t - t.gmtoffset()).strftime("%a, %d-%b-%Y %H:%M:%S GMT") cookie.setExpires(t) if path: cookie.setPath(path) if secure: cookie.setSecure(secure) self.addCookie(cookie)
def setCookie(self, name, value, path='/', expires='ONCLOSE', secure=False): """ Set a cookie. You can also set the path (which defaults to /), You can also set when it expires. It can expire: 'NOW': this is the same as trying to delete it, but it doesn't really seem to work in IE 'ONCLOSE': the default behavior for cookies (expires when the browser closes) 'NEVER': some time in the far, far future. integer: a timestamp value tuple: a tuple, as created by the time module @@ sgd 2/5/2003 - removed optional DateTime for 0.8 release. Use of DateTime in this module has been broken for 3 months and the fix has not been in any of the beta releases. Support may be implemented in a future release. DateTime: an mxDateTime object for the time DeltaDateTime: a interval from the present, e.g., DateTime.DeltaDateTime(month=1) (1 month in the future) '+...': a time in the future, '...' should be something like 1w (1 week), 3h46m (3:45), etc. You can use y (year), b (month), w (week), d (day), h (hour), m (minute), s (second). This is done by the MiscUtils.DateInterval. """ cookie = Cookie(name, value) if expires == 'ONCLOSE' or not expires: pass # this is already default behavior elif expires == 'NOW' or expires == 'NEVER': t = time.gmtime(time.time()) if expires == 'NEVER': t = (t[0] + 10,) + t[1:] t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) cookie.setExpires(t) else: t = expires if type(t) is StringType and t and t[0] == '+': interval = timeDecode(t[1:]) t = time.time() + interval if type(t) in (IntType, LongType,FloatType): t = time.gmtime(t) if type(t) in (TupleType, TimeTupleType): t = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) if DateTime and type(t) in \ (DateTime.DeltaDateTimeType, DateTime.RelativeDateTimeType): t = DateTime.now() + t if DateTime and type(t) is DateTime.DateTimeType: t = t.strftime("%a, %d-%b-%Y %H:%M:%S GMT") cookie.setExpires(t) if path: cookie.setPath(path) if secure: cookie.setSecure(secure) self.addCookie(cookie)
def setCookie(self, name, value, path='/', expires='ONCLOSE', secure=False): """Set a cookie. You can also set the path (which defaults to /). You can also set when it expires. It can expire: 'NOW': this is the same as trying to delete it, but it doesn't really seem to work in IE 'ONCLOSE': the default behavior for cookies (expires when the browser closes) 'NEVER': some time in the far, far future. integer: a timestamp value tuple or struct_time: a tuple, as created by the time module datetime: a datetime.datetime object for the time (if without time zone, assumed to be *local*, not GMT time) timedelta: a duration counted from the present, e.g., datetime.timedelta(days=14) (2 weeks in the future) '+...': a time in the future, '...' should be something like 1w (1 week), 3h46m (3:45), etc. You can use y (year), b (month), w (week), d (day), h (hour), m (minute), s (second). This is done by the MiscUtils.DateInterval. """ cookie = Cookie(name, value) t = expires if isinstance(t, basestring): if t == 'ONCLOSE': t = None elif t == 'NOW': cookie.delete() return elif t == 'NEVER': t = gmtime() t = (t[0] + 10,) + t[1:] elif t.startswith('+'): t = time() + timeDecode(t[1:]) if t: if isinstance(t, (int, long, float)): t = gmtime(t) if isinstance(t, (tuple, struct_time)): t = strftime("%a, %d-%b-%Y %H:%M:%S GMT", t) if isinstance(t, timedelta): t = datetime.now() + t if isinstance(t, datetime): d = t.utcoffset() if d is None: d = localTimeDelta() t -= d t = t.strftime("%a, %d-%b-%Y %H:%M:%S GMT") cookie.setExpires(t) if path: cookie.setPath(path) if secure: cookie.setSecure(secure) self.addCookie(cookie)
def testTimeDecode(self): self.assertEqual(timeDecode('1s'), 1) self.assertEqual(timeDecode('1h2d3s'), 176403) self.assertEqual(timeDecode('2d1h3s'), 176403) self.assertEqual(timeDecode('1h4d3m'), 349380) self.assertEqual(timeDecode('3m4d1h'), 349380) self.assertEqual(timeDecode('1y2b3w4d5h6m7s'), 38898367) self.assertEqual(timeDecode('0y1b2w3d4h5m6s'), 4075506) self.assertEqual(timeDecode('6s5m4h3d2w1b0y'), 4075506) self.assertEqual(timeDecode('(3s-2d-1h)'), 176403) try: timeDecode('1h5n') except ValueError as e: self.assertEqual(str(e), 'Invalid unit of time: n')