"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/roundup/cgi/timestamp.py" (29 Feb 2020, 955 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 '''Set of functions of adding/checking timestamp to be used to limit
    2    form submission for cgi actions.
    3 '''
    4 
    5 import time, struct, binascii, base64
    6 from roundup.cgi.exceptions import FormError
    7 from roundup.i18n import _
    8 from roundup.anypy.strings import b2s, s2b
    9 
   10 
   11 def pack_timestamp():
   12     return b2s(base64.b64encode(struct.pack("i", int(time.time()))).strip())
   13 
   14 
   15 def unpack_timestamp(s):
   16     try:
   17         timestamp = struct.unpack("i", base64.b64decode(s2b(s)))[0]
   18     except (struct.error, binascii.Error, TypeError):
   19         raise FormError(_("Form is corrupted."))
   20     return timestamp
   21 
   22 
   23 class Timestamped:
   24     def timecheck(self, field, delay):
   25         try:
   26             created = unpack_timestamp(self.form[field].value)
   27         except KeyError:
   28             raise FormError(_("Form is corrupted, missing: %s." % field))
   29         if time.time() - created < delay:
   30             raise FormError(_("Responding to form too quickly."))
   31         return True