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.

125 lines
3.5 KiB

10 years ago
8 years ago
8 years ago
  1. from django.contrib import admin
  2. from django.contrib.auth.admin import UserAdmin
  3. from django.contrib.auth.models import User
  4. from django.template.loader import render_to_string
  5. from django.urls import reverse
  6. from django.utils.safestring import mark_safe
  7. from hc.accounts.models import Profile
  8. from hc.api.models import Channel, Check
  9. class Fieldset:
  10. name = None
  11. fields = []
  12. @classmethod
  13. def tuple(cls):
  14. return (cls.name, {"fields": cls.fields})
  15. class ProfileFieldset(Fieldset):
  16. name = "User Profile"
  17. fields = ("email", "api_key", "current_team", "reports_allowed",
  18. "next_report_date", "token")
  19. class TeamFieldset(Fieldset):
  20. name = "Team"
  21. fields = ("team_name", "team_access_allowed", "check_limit",
  22. "ping_log_limit", "sms_limit", "sms_sent", "last_sms_date",
  23. "bill_to")
  24. @admin.register(Profile)
  25. class ProfileAdmin(admin.ModelAdmin):
  26. class Media:
  27. css = {
  28. 'all': ('css/admin/profiles.css',)
  29. }
  30. readonly_fields = ("user", "email")
  31. raw_id_fields = ("current_team", )
  32. list_select_related = ("user", )
  33. list_display = ("id", "users", "checks", "team_access_allowed",
  34. "reports_allowed", "ping_log_limit", "sms")
  35. search_fields = ["id", "user__email"]
  36. list_filter = ("team_access_allowed", "reports_allowed",
  37. "check_limit", "next_report_date")
  38. fieldsets = (ProfileFieldset.tuple(), TeamFieldset.tuple())
  39. @mark_safe
  40. def users(self, obj):
  41. if obj.member_set.count() == 0:
  42. return obj.user.email
  43. else:
  44. return render_to_string("admin/profile_list_team.html", {
  45. "profile": obj
  46. })
  47. @mark_safe
  48. def checks(self, obj):
  49. num_checks = Check.objects.filter(user=obj.user).count()
  50. pct = 100 * num_checks / max(obj.check_limit, 1)
  51. pct = min(100, int(pct))
  52. return """
  53. <span class="bar"><span style="width: %dpx"></span></span>
  54. &nbsp; %d of %d
  55. """ % (pct, num_checks, obj.check_limit)
  56. def sms(self, obj):
  57. return "%d of %d" % (obj.sms_sent, obj.sms_limit)
  58. def email(self, obj):
  59. return obj.user.email
  60. class HcUserAdmin(UserAdmin):
  61. actions = ["send_report"]
  62. list_display = ('id', 'email', 'date_joined', 'engagement',
  63. 'is_staff', 'checks')
  64. list_filter = ("last_login", "date_joined", "is_staff", "is_active")
  65. ordering = ["-id"]
  66. def engagement(self, user):
  67. result = ""
  68. num_checks = Check.objects.filter(user=user).count()
  69. num_channels = Channel.objects.filter(user=user).count()
  70. if num_checks == 0:
  71. result += "0 checks, "
  72. elif num_checks == 1:
  73. result += "1 check, "
  74. else:
  75. result += "<strong>%d checks</strong>, " % num_checks
  76. if num_channels == 0:
  77. result += "0 channels"
  78. elif num_channels == 1:
  79. result += "1 channel, "
  80. else:
  81. result += "<strong>%d channels</strong>, " % num_channels
  82. return result
  83. engagement.allow_tags = True
  84. def checks(self, user):
  85. url = reverse("hc-switch-team", args=[user.username])
  86. return "<a href='%s'>Checks</a>" % url
  87. checks.allow_tags = True
  88. def send_report(self, request, qs):
  89. for user in qs:
  90. user.profile.send_report()
  91. self.message_user(request, "%d email(s) sent" % qs.count())
  92. admin.site.unregister(User)
  93. admin.site.register(User, HcUserAdmin)