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.

236 lines
6.0 KiB

10 years ago
8 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 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. @mark_safe
  11. def _format_usage(num_checks, num_channels):
  12. result = ""
  13. if num_checks == 0:
  14. result += "0 checks, "
  15. elif num_checks == 1:
  16. result += "1 check, "
  17. else:
  18. result += f"<strong>{num_checks} checks</strong>, "
  19. if num_channels == 0:
  20. result += "0 channels"
  21. elif num_channels == 1:
  22. result += "1 channel"
  23. else:
  24. result += f"<strong>{num_channels} channels</strong>"
  25. return result
  26. class Fieldset:
  27. name = None
  28. fields = []
  29. @classmethod
  30. def tuple(cls):
  31. return (cls.name, {"fields": cls.fields})
  32. class ProfileFieldset(Fieldset):
  33. name = "User Profile"
  34. fields = (
  35. "email",
  36. "reports_allowed",
  37. "next_report_date",
  38. "nag_period",
  39. "next_nag_date",
  40. "deletion_notice_date",
  41. "token",
  42. "sort",
  43. )
  44. class TeamFieldset(Fieldset):
  45. name = "Team"
  46. fields = (
  47. "team_limit",
  48. "check_limit",
  49. "ping_log_limit",
  50. "sms_limit",
  51. "sms_sent",
  52. "last_sms_date",
  53. )
  54. class NumChecksFilter(admin.SimpleListFilter):
  55. title = "Checks"
  56. parameter_name = "num_checks"
  57. def lookups(self, request, model_admin):
  58. return (
  59. ("20", "more than 20"),
  60. ("50", "more than 50"),
  61. ("100", "more than 100"),
  62. ("500", "more than 500"),
  63. ("1000", "more than 1000"),
  64. )
  65. def queryset(self, request, queryset):
  66. if not self.value():
  67. return
  68. value = int(self.value())
  69. return queryset.filter(num_checks__gt=value)
  70. @admin.register(Profile)
  71. class ProfileAdmin(admin.ModelAdmin):
  72. class Media:
  73. css = {"all": ("css/admin/profiles.css",)}
  74. readonly_fields = ("user", "email")
  75. search_fields = ["id", "user__email"]
  76. list_per_page = 30
  77. list_select_related = ("user",)
  78. list_display = (
  79. "id",
  80. "email",
  81. "date_joined",
  82. "last_active_date",
  83. "projects",
  84. "checks",
  85. "invited",
  86. "sms",
  87. "reports_allowed",
  88. )
  89. list_filter = (
  90. "user__date_joined",
  91. "last_active_date",
  92. "reports_allowed",
  93. "check_limit",
  94. NumChecksFilter,
  95. )
  96. fieldsets = (ProfileFieldset.tuple(), TeamFieldset.tuple())
  97. def get_queryset(self, request):
  98. qs = super(ProfileAdmin, self).get_queryset(request)
  99. qs = qs.prefetch_related("user__project_set")
  100. qs = qs.annotate(num_members=Count("user__project__member", distinct=True))
  101. qs = qs.annotate(num_checks=Count("user__project__check", distinct=True))
  102. qs = qs.annotate(plan=F("user__subscription__plan_name"))
  103. return qs
  104. @mark_safe
  105. def email(self, obj):
  106. s = escape(obj.user.email)
  107. if obj.plan:
  108. return "<span title='%s'>%s</span>" % (obj.plan, s)
  109. return s
  110. def date_joined(self, obj):
  111. return obj.user.date_joined
  112. @mark_safe
  113. def projects(self, obj):
  114. return render_to_string("admin/profile_list_projects.html", {"profile": obj})
  115. def checks(self, obj):
  116. return "%d of %d" % (obj.num_checks, obj.check_limit)
  117. def invited(self, obj):
  118. return "%d of %d" % (obj.num_members, obj.team_limit)
  119. def sms(self, obj):
  120. return "%d of %d" % (obj.sms_sent, obj.sms_limit)
  121. @admin.register(Project)
  122. class ProjectAdmin(admin.ModelAdmin):
  123. readonly_fields = ("code", "owner")
  124. list_select_related = ("owner",)
  125. list_display = ("id", "name_", "users", "usage", "switch")
  126. search_fields = ["id", "name", "owner__email"]
  127. class Media:
  128. css = {"all": ("css/admin/projects.css",)}
  129. def get_queryset(self, request):
  130. qs = super(ProjectAdmin, self).get_queryset(request)
  131. qs = qs.annotate(num_channels=Count("channel", distinct=True))
  132. qs = qs.annotate(num_checks=Count("check", distinct=True))
  133. qs = qs.annotate(num_members=Count("member", distinct=True))
  134. return qs
  135. def name_(self, obj):
  136. if obj.name:
  137. return obj.name
  138. return "Default Project for %s" % obj.owner.email
  139. @mark_safe
  140. def users(self, obj):
  141. if obj.num_members == 0:
  142. return obj.owner.email
  143. else:
  144. return render_to_string("admin/project_list_team.html", {"project": obj})
  145. def email(self, obj):
  146. return obj.owner.email
  147. def usage(self, obj):
  148. return _format_usage(obj.num_checks, obj.num_channels)
  149. @mark_safe
  150. def switch(self, obj):
  151. url = reverse("hc-checks", args=[obj.code])
  152. return "<a href='%s'>Show Checks</a>" % url
  153. class HcUserAdmin(UserAdmin):
  154. actions = ["send_report", "send_nag"]
  155. list_display = (
  156. "id",
  157. "email",
  158. "usage",
  159. "date_joined",
  160. "last_login",
  161. "is_staff",
  162. )
  163. list_display_links = ("id", "email")
  164. list_filter = ("last_login", "date_joined", "is_staff", "is_active")
  165. ordering = ["-id"]
  166. def get_queryset(self, request):
  167. qs = super().get_queryset(request)
  168. qs = qs.annotate(num_checks=Count("project__check", distinct=True))
  169. qs = qs.annotate(num_channels=Count("project__channel", distinct=True))
  170. return qs
  171. @mark_safe
  172. def usage(self, user):
  173. return _format_usage(user.num_checks, user.num_channels)
  174. def send_report(self, request, qs):
  175. for user in qs:
  176. user.profile.send_report()
  177. self.message_user(request, "%d email(s) sent" % qs.count())
  178. def send_nag(self, request, qs):
  179. for user in qs:
  180. user.profile.send_report(nag=True)
  181. self.message_user(request, "%d email(s) sent" % qs.count())
  182. admin.site.unregister(User)
  183. admin.site.register(User, HcUserAdmin)