Mercurial > kallithea
comparison rhodecode/tests/functional/test_admin.py @ 3074:09cef303962b beta
python2.5 safe parsing of datetime string with microseconds
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 07 Dec 2012 02:15:34 +0100 |
parents | 86e087bd75ce |
children | 7e5f8c12a3fc |
comparison
equal
deleted
inserted
replaced
3073:e54ec8211944 | 3074:09cef303962b |
---|---|
15 | 15 |
16 @classmethod | 16 @classmethod |
17 def setup_class(cls): | 17 def setup_class(cls): |
18 UserLog.query().delete() | 18 UserLog.query().delete() |
19 Session().commit() | 19 Session().commit() |
20 | |
21 def strptime(val): | |
22 fmt = '%Y-%m-%d %H:%M:%S' | |
23 if '.' not in val: | |
24 return datetime.datetime.strptime(val, fmt) | |
25 | |
26 nofrag, frag = val.split(".") | |
27 date = datetime.datetime.strptime(nofrag, fmt) | |
28 | |
29 frag = frag[:6] # truncate to microseconds | |
30 frag += (6 - len(frag)) * '0' # add 0s | |
31 return date.replace(microsecond=int(frag)) | |
32 | |
20 with open(os.path.join(FIXTURES, 'journal_dump.csv')) as f: | 33 with open(os.path.join(FIXTURES, 'journal_dump.csv')) as f: |
21 for row in csv.DictReader(f): | 34 for row in csv.DictReader(f): |
22 ul = UserLog() | 35 ul = UserLog() |
23 for k, v in row.iteritems(): | 36 for k, v in row.iteritems(): |
24 v = safe_unicode(v) | 37 v = safe_unicode(v) |
25 if k == 'action_date': | 38 if k == 'action_date': |
26 v = datetime.datetime.strptime(v, '%Y-%m-%d %H:%M:%S.%f') | 39 v = strptime(v) |
27 if k in ['user_id', 'repository_id']: | 40 if k in ['user_id', 'repository_id']: |
28 #nullable due to FK problems | 41 # nullable due to FK problems |
29 v = None | 42 v = None |
30 setattr(ul, k, v) | 43 setattr(ul, k, v) |
31 Session().add(ul) | 44 Session().add(ul) |
32 Session().commit() | 45 Session().commit() |
33 | 46 |