Mercurial > p > roundup > code
comparison roundup/cgi/timestamp.py @ 6045:5ec3171580a6
flake whitespace changes.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 07 Jan 2020 21:50:57 -0500 |
| parents | 59842a3e8108 |
| children | 07ce4e4110f5 |
comparison
equal
deleted
inserted
replaced
| 6044:32a5a54536b5 | 6045:5ec3171580a6 |
|---|---|
| 5 import time, struct, binascii, base64 | 5 import time, struct, binascii, base64 |
| 6 from roundup.cgi.exceptions import FormError | 6 from roundup.cgi.exceptions import FormError |
| 7 from roundup.i18n import _ | 7 from roundup.i18n import _ |
| 8 from roundup.anypy.strings import b2s, s2b | 8 from roundup.anypy.strings import b2s, s2b |
| 9 | 9 |
| 10 | |
| 10 def pack_timestamp(): | 11 def pack_timestamp(): |
| 11 return b2s(base64.b64encode(struct.pack("i", int(time.time()))).strip()) | 12 return b2s(base64.b64encode(struct.pack("i", int(time.time()))).strip()) |
| 12 | 13 |
| 14 | |
| 13 def unpack_timestamp(s): | 15 def unpack_timestamp(s): |
| 14 try: | 16 try: |
| 15 timestamp = struct.unpack("i",base64.b64decode(s2b(s)))[0] | 17 timestamp = struct.unpack("i", base64.b64decode(s2b(s)))[0] |
| 16 except (struct.error, binascii.Error, TypeError) as e: | 18 except (struct.error, binascii.Error, TypeError): |
| 17 raise FormError(_("Form is corrupted.")) | 19 raise FormError(_("Form is corrupted.")) |
| 18 return timestamp | 20 return timestamp |
| 19 | 21 |
| 22 | |
| 20 class Timestamped: | 23 class Timestamped: |
| 21 def timecheck(self,field,delay): | 24 def timecheck(self, field, delay): |
| 22 try: | 25 try: |
| 23 created = unpack_timestamp(self.form[field].value) | 26 created = unpack_timestamp(self.form[field].value) |
| 24 except KeyError: | 27 except KeyError: |
| 25 raise FormError(_("Form is corrupted, missing: %s."%field)) | 28 raise FormError(_("Form is corrupted, missing: %s." % field)) |
| 26 if time.time() - created < delay: | 29 if time.time() - created < delay: |
| 27 raise FormError(_("Responding to form too quickly.")) | 30 raise FormError(_("Responding to form too quickly.")) |
| 28 return True | 31 return True |
