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.

215 lines
6.2 KiB

10 years ago
8 years ago
8 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, F
  5. from django.template.loader import render_to_string
  6. from django.urls import reverse
  7. from django.utils.html import escape
  8. from django.utils.safestring import mark_safe
  9. from hc.accounts.models import Profile, Project
  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", "current_project", "reports_allowed",
  19. "next_report_date", "nag_period", "next_nag_date",
  20. "token", "sort")
  21. class TeamFieldset(Fieldset):
  22. name = "Team"
  23. fields = ("team_limit", "check_limit", "ping_log_limit", "sms_limit",
  24. "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_project", )
  33. list_select_related = ("user", )
  34. list_display = ("id", "email", "engagement", "date_joined", "last_login",
  35. "projects", "invited", "sms", "reports_allowed")
  36. search_fields = ["id", "user__email"]
  37. list_filter = ("user__date_joined", "user__last_login",
  38. "team_limit", "reports_allowed", "check_limit")
  39. fieldsets = (ProfileFieldset.tuple(), TeamFieldset.tuple())
  40. def get_queryset(self, request):
  41. qs = super(ProfileAdmin, self).get_queryset(request)
  42. qs = qs.annotate(num_members=Count("user__project__member", distinct=True))
  43. qs = qs.annotate(num_checks=Count("user__project__check", distinct=True))
  44. qs = qs.annotate(num_channels=Count("user__project__channel", distinct=True))
  45. qs = qs.annotate(plan=F("user__subscription__plan_name"))
  46. return qs
  47. @mark_safe
  48. def engagement(self, obj):
  49. result = ""
  50. if obj.num_checks == 0:
  51. result += "0 checks, "
  52. elif obj.num_checks == 1:
  53. result += "1 check, "
  54. else:
  55. result += "<strong>%d checks</strong>, " % obj.num_checks
  56. if obj.num_channels == 0:
  57. result += "0 channels"
  58. elif obj.num_channels == 1:
  59. result += "1 channel, "
  60. else:
  61. result += "<strong>%d channels</strong>, " % obj.num_channels
  62. return result
  63. @mark_safe
  64. def email(self, obj):
  65. s = escape(obj.user.email)
  66. if obj.plan:
  67. return "<span title='%s'>%s</span>" % (obj.plan, s)
  68. return s
  69. def last_login(self, obj):
  70. return obj.user.last_login
  71. def date_joined(self, obj):
  72. return obj.user.date_joined
  73. @mark_safe
  74. def projects(self, obj):
  75. return render_to_string("admin/profile_list_projects.html", {
  76. "profile": obj
  77. })
  78. def invited(self, obj):
  79. return "%d of %d" % (obj.num_members, obj.team_limit)
  80. def sms(self, obj):
  81. return "%d of %d" % (obj.sms_sent, obj.sms_limit)
  82. @admin.register(Project)
  83. class ProjectAdmin(admin.ModelAdmin):
  84. readonly_fields = ("code", "owner")
  85. list_select_related = ("owner", )
  86. list_display = ("id", "name_", "users", "engagement", "switch")
  87. class Media:
  88. css = {
  89. 'all': ('css/admin/projects.css',)
  90. }
  91. def get_queryset(self, request):
  92. qs = super(ProjectAdmin, self).get_queryset(request)
  93. qs = qs.annotate(num_channels=Count("channel", distinct=True))
  94. qs = qs.annotate(num_checks=Count("check", distinct=True))
  95. qs = qs.annotate(num_members=Count("member", distinct=True))
  96. return qs
  97. def name_(self, obj):
  98. if obj.name:
  99. return obj.name
  100. return "Default Project for %s" % obj.owner.email
  101. @mark_safe
  102. def users(self, obj):
  103. if obj.num_members == 0:
  104. return obj.owner.email
  105. else:
  106. return render_to_string("admin/project_list_team.html", {
  107. "project": obj
  108. })
  109. def email(self, obj):
  110. return obj.owner.email
  111. @mark_safe
  112. def engagement(self, obj):
  113. result = ""
  114. if obj.num_checks == 0:
  115. result += "0 checks, "
  116. elif obj.num_checks == 1:
  117. result += "1 check, "
  118. else:
  119. result += "<strong>%d checks</strong>, " % obj.num_checks
  120. if obj.num_channels == 0:
  121. result += "0 channels"
  122. elif obj.num_channels == 1:
  123. result += "1 channel, "
  124. else:
  125. result += "<strong>%d channels</strong>, " % obj.num_channels
  126. return result
  127. @mark_safe
  128. def switch(self, obj):
  129. url = reverse("hc-checks", args=[obj.code])
  130. return "<a href='%s'>Show Checks</a>" % url
  131. class HcUserAdmin(UserAdmin):
  132. actions = ["send_report"]
  133. list_display = ('id', 'email', 'engagement', 'date_joined', 'last_login',
  134. 'is_staff')
  135. list_display_links = ("id", "email")
  136. list_filter = ("last_login", "date_joined", "is_staff", "is_active")
  137. ordering = ["-id"]
  138. def get_queryset(self, request):
  139. qs = super().get_queryset(request)
  140. qs = qs.annotate(num_checks=Count("project__check", distinct=True))
  141. qs = qs.annotate(num_channels=Count("project__channel", distinct=True))
  142. return qs
  143. @mark_safe
  144. def engagement(self, user):
  145. result = ""
  146. if user.num_checks == 0:
  147. result += "0 checks, "
  148. elif user.num_checks == 1:
  149. result += "1 check, "
  150. else:
  151. result += "<strong>%d checks</strong>, " % user.num_checks
  152. if user.num_channels == 0:
  153. result += "0 channels"
  154. elif user.num_channels == 1:
  155. result += "1 channel, "
  156. else:
  157. result += "<strong>%d channels</strong>, " % user.num_channels
  158. return result
  159. def send_report(self, request, qs):
  160. for user in qs:
  161. user.profile.send_report()
  162. self.message_user(request, "%d email(s) sent" % qs.count())
  163. admin.site.unregister(User)
  164. admin.site.register(User, HcUserAdmin)