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.

138 lines
3.9 KiB

10 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. 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", "nag_period", "next_nag_date",
  19. "token", "sort")
  20. class TeamFieldset(Fieldset):
  21. name = "Team"
  22. fields = ("team_name", "team_limit", "check_limit",
  23. "ping_log_limit", "sms_limit", "sms_sent", "last_sms_date")
  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", "invited",
  34. "reports_allowed", "ping_log_limit", "sms")
  35. search_fields = ["id", "user__email"]
  36. list_filter = ("team_limit", "reports_allowed",
  37. "check_limit", "next_report_date")
  38. fieldsets = (ProfileFieldset.tuple(), TeamFieldset.tuple())
  39. def get_queryset(self, request):
  40. qs = super(ProfileAdmin, self).get_queryset(request)
  41. qs = qs.annotate(Count("member", distinct=True))
  42. qs = qs.annotate(Count("user__check", distinct=True))
  43. return qs
  44. @mark_safe
  45. def users(self, obj):
  46. if obj.member__count == 0:
  47. return obj.user.email
  48. else:
  49. return render_to_string("admin/profile_list_team.html", {
  50. "profile": obj
  51. })
  52. @mark_safe
  53. def checks(self, obj):
  54. num_checks = obj.user__check__count
  55. pct = 100 * num_checks / max(obj.check_limit, 1)
  56. pct = min(100, int(pct))
  57. return """
  58. <span class="bar"><span style="width: %dpx"></span></span>
  59. &nbsp; %d of %d
  60. """ % (pct, num_checks, obj.check_limit)
  61. def invited(self, obj):
  62. return "%d of %d" % (obj.member__count, obj.team_limit)
  63. def sms(self, obj):
  64. return "%d of %d" % (obj.sms_sent, obj.sms_limit)
  65. def email(self, obj):
  66. return obj.user.email
  67. class HcUserAdmin(UserAdmin):
  68. actions = ["send_report"]
  69. list_display = ('id', 'email', 'date_joined', 'last_login', 'engagement',
  70. 'is_staff', 'checks')
  71. list_display_links = ("id", "email")
  72. list_filter = ("last_login", "date_joined", "is_staff", "is_active")
  73. ordering = ["-id"]
  74. def get_queryset(self, request):
  75. qs = super().get_queryset(request)
  76. qs = qs.annotate(Count("check", distinct=True))
  77. qs = qs.annotate(Count("channel", distinct=True))
  78. return qs
  79. @mark_safe
  80. def engagement(self, user):
  81. result = ""
  82. if user.check__count == 0:
  83. result += "0 checks, "
  84. elif user.check__count == 1:
  85. result += "1 check, "
  86. else:
  87. result += "<strong>%d checks</strong>, " % user.check__count
  88. if user.channel__count == 0:
  89. result += "0 channels"
  90. elif user.channel__count == 1:
  91. result += "1 channel, "
  92. else:
  93. result += "<strong>%d channels</strong>, " % user.channel__count
  94. return result
  95. @mark_safe
  96. def checks(self, user):
  97. url = reverse("hc-switch-team", args=[user.username])
  98. return "<a href='%s'>Checks</a>" % url
  99. def send_report(self, request, qs):
  100. for user in qs:
  101. user.profile.send_report()
  102. self.message_user(request, "%d email(s) sent" % qs.count())
  103. admin.site.unregister(User)
  104. admin.site.register(User, HcUserAdmin)