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.

241 lines
6.1 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. ("10", "more than 10"),
  60. ("20", "more than 20"),
  61. ("50", "more than 50"),
  62. ("100", "more than 100"),
  63. ("500", "more than 500"),
  64. ("1000", "more than 1000"),
  65. )
  66. def queryset(self, request, queryset):
  67. if not self.value():
  68. return
  69. value = int(self.value())
  70. return queryset.filter(num_checks__gt=value)
  71. @admin.register(Profile)
  72. class ProfileAdmin(admin.ModelAdmin):
  73. class Media:
  74. css = {"all": ("css/admin/profiles.css",)}
  75. readonly_fields = ("user", "email")
  76. search_fields = ["id", "user__email"]
  77. list_per_page = 30
  78. list_select_related = ("user",)
  79. list_display = (
  80. "id",
  81. "email",
  82. "checks",
  83. "date_joined",
  84. "last_active_date",
  85. "projects",
  86. "invited",
  87. "sms",
  88. "reports_allowed",
  89. )
  90. list_filter = (
  91. "user__date_joined",
  92. "last_active_date",
  93. "reports_allowed",
  94. "check_limit",
  95. NumChecksFilter,
  96. )
  97. fieldsets = (ProfileFieldset.tuple(), TeamFieldset.tuple())
  98. def get_queryset(self, request):
  99. qs = super(ProfileAdmin, self).get_queryset(request)
  100. qs = qs.prefetch_related("user__project_set")
  101. qs = qs.annotate(num_members=Count("user__project__member", distinct=True))
  102. qs = qs.annotate(num_checks=Count("user__project__check", distinct=True))
  103. qs = qs.annotate(plan=F("user__subscription__plan_name"))
  104. return qs
  105. @mark_safe
  106. def email(self, obj):
  107. s = escape(obj.user.email)
  108. if obj.plan:
  109. s = "%s <span>%s</span>" % (s, obj.plan)
  110. return s
  111. def date_joined(self, obj):
  112. return obj.user.date_joined
  113. @mark_safe
  114. def projects(self, obj):
  115. return render_to_string("admin/profile_list_projects.html", {"profile": obj})
  116. @mark_safe
  117. def checks(self, obj):
  118. s = "%d of %d" % (obj.num_checks, obj.check_limit)
  119. if obj.num_checks > 10:
  120. s = "<b>%s</b>" % s
  121. return s
  122. def invited(self, obj):
  123. return "%d of %d" % (obj.num_members, obj.team_limit)
  124. def sms(self, obj):
  125. return "%d of %d" % (obj.sms_sent, obj.sms_limit)
  126. @admin.register(Project)
  127. class ProjectAdmin(admin.ModelAdmin):
  128. readonly_fields = ("code", "owner")
  129. list_select_related = ("owner",)
  130. list_display = ("id", "name_", "users", "usage", "switch")
  131. search_fields = ["id", "name", "owner__email"]
  132. class Media:
  133. css = {"all": ("css/admin/projects.css",)}
  134. def get_queryset(self, request):
  135. qs = super(ProjectAdmin, self).get_queryset(request)
  136. qs = qs.annotate(num_channels=Count("channel", distinct=True))
  137. qs = qs.annotate(num_checks=Count("check", distinct=True))
  138. qs = qs.annotate(num_members=Count("member", distinct=True))
  139. return qs
  140. def name_(self, obj):
  141. if obj.name:
  142. return obj.name
  143. return "Default Project for %s" % obj.owner.email
  144. @mark_safe
  145. def users(self, obj):
  146. if obj.num_members == 0:
  147. return obj.owner.email
  148. else:
  149. return render_to_string("admin/project_list_team.html", {"project": obj})
  150. def email(self, obj):
  151. return obj.owner.email
  152. def usage(self, obj):
  153. return _format_usage(obj.num_checks, obj.num_channels)
  154. @mark_safe
  155. def switch(self, obj):
  156. url = reverse("hc-checks", args=[obj.code])
  157. return "<a href='%s'>Show Checks</a>" % url
  158. class HcUserAdmin(UserAdmin):
  159. actions = ["send_report", "send_nag"]
  160. list_display = (
  161. "id",
  162. "email",
  163. "usage",
  164. "date_joined",
  165. "last_login",
  166. "is_staff",
  167. )
  168. list_display_links = ("id", "email")
  169. list_filter = ("last_login", "date_joined", "is_staff", "is_active")
  170. ordering = ["-id"]
  171. def get_queryset(self, request):
  172. qs = super().get_queryset(request)
  173. qs = qs.annotate(num_checks=Count("project__check", distinct=True))
  174. qs = qs.annotate(num_channels=Count("project__channel", distinct=True))
  175. return qs
  176. @mark_safe
  177. def usage(self, user):
  178. return _format_usage(user.num_checks, user.num_channels)
  179. def send_report(self, request, qs):
  180. for user in qs:
  181. user.profile.send_report()
  182. self.message_user(request, "%d email(s) sent" % qs.count())
  183. def send_nag(self, request, qs):
  184. for user in qs:
  185. user.profile.send_report(nag=True)
  186. self.message_user(request, "%d email(s) sent" % qs.count())
  187. admin.site.unregister(User)
  188. admin.site.register(User, HcUserAdmin)