CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
1.2 KiB

  1. from django.utils.translation import ugettext, ungettext
  2. from django.template import Library
  3. from django.utils.html import avoid_wrapping
  4. from django.utils import formats
  5. register = Library()
  6. @register.filter(is_safe=True)
  7. def bwformat(bps):
  8. try:
  9. bps = float(bps)
  10. except (TypeError, ValueError, UnicodeDecodeError):
  11. value = ungettext("%(bw)d bps", "%(bw)d bps", 0) % {'bw': 0}
  12. return avoid_wrapping(value)
  13. filesize_number_format = lambda value: formats.number_format(round(value, 1), -1)
  14. K = 1 * 10 ** 3
  15. M = 1 * 10 ** 6
  16. G = 1 * 10 ** 9
  17. T = 1 * 10 ** 12
  18. P = 1 * 10 ** 15
  19. if bps < K:
  20. value = ungettext("%(size)d bps", "%(size)d bps", bps) % {'size': bps}
  21. elif bps < M:
  22. value = ugettext("%s Kbps") % filesize_number_format(bps / K)
  23. elif bps < G:
  24. value = ugettext("%s Mbps") % filesize_number_format(bps / M)
  25. elif bps < T:
  26. value = ugettext("%s Gbps") % filesize_number_format(bps / G)
  27. elif bps < P:
  28. value = ugettext("%s Tbps") % filesize_number_format(bps / T)
  29. else:
  30. value = ugettext("%s Pbps") % filesize_number_format(bps / P)
  31. return avoid_wrapping(value)