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.

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