File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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+ def pack_timestamp ():
11+ return b2s (base64 .b64encode (struct .pack ("i" , int (time .time ()))).strip ())
12+
13+ def unpack_timestamp (s ):
14+ try :
15+ timestamp = struct .unpack ("i" ,base64 .b64decode (s2b (s )))[0 ]
16+ except (struct .error , binascii .Error , TypeError ) as e :
17+ raise FormError (_ ("Form is corrupted." ))
18+ return timestamp
19+
20+ class Timestamped :
21+ def timecheck (self ,field ,delay ):
22+ try :
23+ created = unpack_timestamp (self .form [field ].value )
24+ except KeyError :
25+ raise FormError (_ ("Form is corrupted, missing: %s." % field ))
26+ if time .time () - created < delay :
27+ raise FormError (_ ("Responding to form too quickly." ))
28+ return True
You can’t perform that action at this time.
0 commit comments