allura
Revision | ac3417de459c4571ecac593833c7edc33b4171a1 (tree) |
---|---|
Time | 2012-05-17 22:42:16 |
Author | Dave Brondsema <dbrondsema@geek...> |
Commiter | Dave Brondsema |
show attachment details on its own line, more readable
@@ -589,3 +589,35 @@ def render_any_markup(name, text, code_mode=False, linenumbers_style=TABLE): | ||
589 | 589 | else: |
590 | 590 | text = '<pre>%s</pre>' % text |
591 | 591 | return Markup(text) |
592 | + | |
593 | +# copied from jinja2 dev | |
594 | +# latest release, 2.6, implements this incorrectly | |
595 | +# can remove and use jinja2 implementation after upgrading to 2.7 | |
596 | +def do_filesizeformat(value, binary=False): | |
597 | + """Format the value like a 'human-readable' file size (i.e. 13 kB, | |
598 | +4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega, | |
599 | +Giga, etc.), if the second parameter is set to `True` the binary | |
600 | +prefixes are used (Mebi, Gibi). | |
601 | +""" | |
602 | + bytes = float(value) | |
603 | + base = binary and 1024 or 1000 | |
604 | + prefixes = [ | |
605 | + (binary and 'KiB' or 'kB'), | |
606 | + (binary and 'MiB' or 'MB'), | |
607 | + (binary and 'GiB' or 'GB'), | |
608 | + (binary and 'TiB' or 'TB'), | |
609 | + (binary and 'PiB' or 'PB'), | |
610 | + (binary and 'EiB' or 'EB'), | |
611 | + (binary and 'ZiB' or 'ZB'), | |
612 | + (binary and 'YiB' or 'YB') | |
613 | + ] | |
614 | + if bytes == 1: | |
615 | + return '1 Byte' | |
616 | + elif bytes < base: | |
617 | + return '%d Bytes' % bytes | |
618 | + else: | |
619 | + for i, prefix in enumerate(prefixes): | |
620 | + unit = base ** (i + 2) | |
621 | + if bytes < unit: | |
622 | + return '%.1f %s' % ((base * bytes / unit), prefix) | |
623 | + return '%.1f %s' % ((base * bytes / unit), prefix) |
@@ -121,7 +121,7 @@ class Notification(MappedClass): | ||
121 | 121 | file_info.file.seek(0, 2) |
122 | 122 | bytecount = file_info.file.tell() |
123 | 123 | file_info.file.seek(0) |
124 | - text = "%s\n%s (%s bytes in %s)" % (text, file_info.filename, bytecount, file_info.type) | |
124 | + text = "%s\n\n\nAttachment: %s (%s; %s)" % (text, file_info.filename, h.do_filesizeformat(bytecount), file_info.type) | |
125 | 125 | |
126 | 126 | subject = post.subject or '' |
127 | 127 | if post.parent_id and not subject.lower().startswith('re:'): |
@@ -167,7 +167,7 @@ def test_attachment_methods(): | ||
167 | 167 | p = t.post(text=u'test message', forum= None, subject= '', file_info=fs) |
168 | 168 | ThreadLocalORMSession.flush_all() |
169 | 169 | n = M.Notification.query.get(subject=u'[test:wiki] Test comment notification') |
170 | - assert u'test message\nfake.txt (37 bytes in text/plain)'==n.text | |
170 | + assert_equals(u'test message\n\n\nAttachment: fake.txt (37 Bytes; text/plain)', n.text) | |
171 | 171 | |
172 | 172 | @with_setup(setUp, tearDown) |
173 | 173 | def test_discussion_delete(): |