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.

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