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.

240 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. )
  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. "date_joined",
  83. "last_active_date",
  84. "projects",
  85. "checks",
  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 = "<span title='%s'>%s</span>" % (obj.plan, s)
  110. v = min(30, obj.num_checks) * 8
  111. s += """<div class="nchecks"><div style="width: %dpx"></div>""" % v
  112. return s
  113. def date_joined(self, obj):
  114. return obj.user.date_joined
  115. @mark_safe
  116. def projects(self, obj):
  117. return render_to_string("admin/profile_list_projects.html", {"profile": obj})
  118. def checks(self, obj):
  119. return "%d of %d" % (obj.num_checks, obj.check_limit)
  120. def invited(self, obj):
  121. return "%d of %d" % (obj.num_members, obj.team_limit)
  122. def sms(self, obj):
  123. return "%d of %d" % (obj.sms_sent, obj.sms_limit)
  124. @admin.register(Project)
  125. class ProjectAdmin(admin.ModelAdmin):
  126. readonly_fields = ("code", "owner")
  127. list_select_related = ("owner",)
  128. list_display = ("id", "name_", "users", "usage", "switch")
  129. search_fields = ["id", "name", "owner__email"]
  130. class Media:
  131. css = {"all": ("css/admin/projects.css",)}
  132. def get_queryset(self, request):
  133. qs = super(ProjectAdmin, self).get_queryset(request)
  134. qs = qs.annotate(num_channels=Count("channel", distinct=True))
  135. qs = qs.annotate(num_checks=Count("check", distinct=True))
  136. qs = qs.annotate(num_members=Count("member", distinct=True))
  137. return qs
  138. def name_(self, obj):
  139. if obj.name:
  140. return obj.name
  141. return "Default Project for %s" % obj.owner.email
  142. @mark_safe
  143. def users(self, obj):
  144. if obj.num_members == 0:
  145. return obj.owner.email
  146. else:
  147. return render_to_string("admin/project_list_team.html", {"project": obj})
  148. def email(self, obj):
  149. return obj.owner.email
  150. def usage(self, obj):
  151. return _format_usage(obj.num_checks, obj.num_channels)
  152. @mark_safe
  153. def switch(self, obj):
  154. url = reverse("hc-checks", args=[obj.code])
  155. return "<a href='%s'>Show Checks</a>" % url
  156. class HcUserAdmin(UserAdmin):
  157. actions = ["send_report", "send_nag"]
  158. list_display = (
  159. "id",
  160. "email",
  161. "usage",
  162. "date_joined",
  163. "last_login",
  164. "is_staff",
  165. )
  166. list_display_links = ("id", "email")
  167. list_filter = ("last_login", "date_joined", "is_staff", "is_active")
  168. ordering = ["-id"]
  169. def get_queryset(self, request):
  170. qs = super().get_queryset(request)
  171. qs = qs.annotate(num_checks=Count("project__check", distinct=True))
  172. qs = qs.annotate(num_channels=Count("project__channel", distinct=True))
  173. return qs
  174. @mark_safe
  175. def usage(self, user):
  176. return _format_usage(user.num_checks, user.num_channels)
  177. def send_report(self, request, qs):
  178. for user in qs:
  179. user.profile.send_report()
  180. self.message_user(request, "%d email(s) sent" % qs.count())
  181. def send_nag(self, request, qs):
  182. for user in qs:
  183. user.profile.send_report(nag=True)
  184. self.message_user(request, "%d email(s) sent" % qs.count())
  185. admin.site.unregister(User)
  186. admin.site.register(User, HcUserAdmin)