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.

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