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.

55 lines
1.7 KiB

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.core.urlresolvers import reverse
  5. WIDTHS = {"a": 7, "b": 7, "c": 6, "d": 7, "e": 6, "f": 4, "g": 7, "h": 7,
  6. "i": 3, "j": 3, "k": 7, "l": 3, "m": 10, "n": 7, "o": 7, "p": 7,
  7. "q": 7, "r": 4, "s": 6, "t": 5, "u": 7, "v": 7, "w": 9, "x": 6,
  8. "y": 7, "z": 7, "0": 7, "1": 6, "2": 7, "3": 7, "4": 7, "5": 7,
  9. "6": 7, "7": 7, "8": 7, "9": 7, "A": 8, "B": 7, "C": 8, "D": 8,
  10. "E": 7, "F": 6, "G": 9, "H": 8, "I": 3, "J": 4, "K": 7, "L": 6,
  11. "M": 10, "N": 8, "O": 9, "P": 6, "Q": 9, "R": 7, "S": 7, "T": 7,
  12. "U": 8, "V": 8, "W": 11, "X": 7, "Y": 7, "Z": 7, "-": 4, "_": 6}
  13. COLORS = {
  14. "up": "#4c1",
  15. "late": "#fe7d37",
  16. "down": "#e05d44"
  17. }
  18. def get_width(s):
  19. total = 0
  20. for c in s:
  21. total += WIDTHS.get(c, 7)
  22. return total
  23. def get_badge_svg(tag, status):
  24. w1 = get_width(tag) + 10
  25. w2 = get_width(status) + 10
  26. ctx = {
  27. "width": w1 + w2,
  28. "tag_width": w1,
  29. "status_width": w2,
  30. "tag_center_x": w1 / 2,
  31. "status_center_x": w1 + w2 / 2,
  32. "tag": tag,
  33. "status": status,
  34. "color": COLORS[status]
  35. }
  36. return render_to_string("badge.svg", ctx)
  37. def check_signature(username, tag, sig):
  38. ours = base64_hmac(str(username), tag, settings.SECRET_KEY)
  39. ours = ours[:8].decode("utf-8")
  40. return ours == sig
  41. def get_badge_url(username, tag):
  42. sig = base64_hmac(str(username), tag, settings.SECRET_KEY)
  43. url = reverse("hc-badge", args=[username, sig[:8], tag])
  44. return settings.SITE_ROOT + url