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.

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