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.

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