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.

115 lines
2.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. from django.conf import settings
  2. from django.core.signing import base64_hmac
  3. from django.template.loader import render_to_string
  4. from django.urls import reverse
  5. WIDTHS = {
  6. "a": 7,
  7. "b": 7,
  8. "c": 6,
  9. "d": 7,
  10. "e": 6,
  11. "f": 4,
  12. "g": 7,
  13. "h": 7,
  14. "i": 3,
  15. "j": 3,
  16. "k": 7,
  17. "l": 3,
  18. "m": 10,
  19. "n": 7,
  20. "o": 7,
  21. "p": 7,
  22. "q": 7,
  23. "r": 4,
  24. "s": 6,
  25. "t": 5,
  26. "u": 7,
  27. "v": 7,
  28. "w": 9,
  29. "x": 6,
  30. "y": 7,
  31. "z": 7,
  32. "0": 7,
  33. "1": 6,
  34. "2": 7,
  35. "3": 7,
  36. "4": 7,
  37. "5": 7,
  38. "6": 7,
  39. "7": 7,
  40. "8": 7,
  41. "9": 7,
  42. "A": 8,
  43. "B": 7,
  44. "C": 8,
  45. "D": 8,
  46. "E": 7,
  47. "F": 6,
  48. "G": 9,
  49. "H": 8,
  50. "I": 3,
  51. "J": 4,
  52. "K": 7,
  53. "L": 6,
  54. "M": 10,
  55. "N": 8,
  56. "O": 9,
  57. "P": 6,
  58. "Q": 9,
  59. "R": 7,
  60. "S": 7,
  61. "T": 7,
  62. "U": 8,
  63. "V": 8,
  64. "W": 11,
  65. "X": 7,
  66. "Y": 7,
  67. "Z": 7,
  68. "-": 4,
  69. "_": 6,
  70. }
  71. COLORS = {"up": "#4c1", "late": "#fe7d37", "down": "#e05d44"}
  72. def get_width(s):
  73. total = 0
  74. for c in s:
  75. total += WIDTHS.get(c, 7)
  76. return total
  77. def get_badge_svg(tag, status):
  78. w1 = get_width(tag) + 10
  79. w2 = get_width(status) + 10
  80. ctx = {
  81. "width": w1 + w2,
  82. "tag_width": w1,
  83. "status_width": w2,
  84. "tag_center_x": w1 / 2,
  85. "status_center_x": w1 + w2 / 2,
  86. "tag": tag,
  87. "status": status,
  88. "color": COLORS[status],
  89. }
  90. return render_to_string("badge.svg", ctx)
  91. def check_signature(username, tag, sig):
  92. ours = base64_hmac(str(username), tag, settings.SECRET_KEY)
  93. return ours[:8] == sig[:8]
  94. def get_badge_url(username, tag, fmt="svg", with_late=False):
  95. sig = base64_hmac(str(username), tag, settings.SECRET_KEY)[:8]
  96. if not with_late:
  97. sig += "-2"
  98. if tag == "*":
  99. url = reverse("hc-badge-all", args=[username, sig, fmt])
  100. else:
  101. url = reverse("hc-badge", args=[username, sig, tag, fmt])
  102. return settings.SITE_ROOT + url