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.

197 lines
6.0 KiB

10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from django.contrib import admin
  2. from django.core.paginator import Paginator
  3. from django.db import connection
  4. from hc.api.models import Channel, Check, Notification, Ping
  5. class OwnershipListFilter(admin.SimpleListFilter):
  6. title = "Ownership"
  7. parameter_name = 'ownership'
  8. def lookups(self, request, model_admin):
  9. return (
  10. ('assigned', "Assigned"),
  11. )
  12. def queryset(self, request, queryset):
  13. if self.value() == 'assigned':
  14. return queryset.filter(user__isnull=False)
  15. return queryset
  16. @admin.register(Check)
  17. class ChecksAdmin(admin.ModelAdmin):
  18. class Media:
  19. css = {
  20. 'all': ('css/admin/checks.css',)
  21. }
  22. search_fields = ["name", "user__email", "code"]
  23. list_display = ("id", "name_tags", "created", "code", "status", "email",
  24. "last_ping", "n_pings")
  25. list_select_related = ("user", )
  26. list_filter = ("status", OwnershipListFilter, "last_ping")
  27. actions = ["send_alert"]
  28. def email(self, obj):
  29. return obj.user.email if obj.user else None
  30. def name_tags(self, obj):
  31. if not obj.tags:
  32. return obj.name
  33. return "%s [%s]" % (obj.name, obj.tags)
  34. def send_alert(self, request, qs):
  35. for check in qs:
  36. check.send_alert()
  37. self.message_user(request, "%d alert(s) sent" % qs.count())
  38. send_alert.short_description = "Send Alert"
  39. class SchemeListFilter(admin.SimpleListFilter):
  40. title = "Scheme"
  41. parameter_name = 'scheme'
  42. def lookups(self, request, model_admin):
  43. return (
  44. ('http', "HTTP"),
  45. ('https', "HTTPS"),
  46. ('email', "Email"),
  47. )
  48. def queryset(self, request, queryset):
  49. if self.value():
  50. queryset = queryset.filter(scheme=self.value())
  51. return queryset
  52. class MethodListFilter(admin.SimpleListFilter):
  53. title = "Method"
  54. parameter_name = 'method'
  55. methods = ["HEAD", "GET", "POST", "PUT", "DELETE"]
  56. def lookups(self, request, model_admin):
  57. return zip(self.methods, self.methods)
  58. def queryset(self, request, queryset):
  59. if self.value():
  60. queryset = queryset.filter(method=self.value())
  61. return queryset
  62. # Adapted from: https://djangosnippets.org/snippets/2593/
  63. class LargeTablePaginator(Paginator):
  64. """ Overrides the count method to get an estimate instead of actual count
  65. when not filtered
  66. """
  67. def _get_estimate(self):
  68. try:
  69. cursor = connection.cursor()
  70. cursor.execute("SELECT reltuples FROM pg_class WHERE relname = %s",
  71. [self.object_list.query.model._meta.db_table])
  72. return int(cursor.fetchone()[0])
  73. except:
  74. return 0
  75. def _get_count(self):
  76. """
  77. Changed to use an estimate if the estimate is greater than 10,000
  78. Returns the total number of objects, across all pages.
  79. """
  80. if self._count is None:
  81. try:
  82. estimate = 0
  83. if not self.object_list.query.where:
  84. estimate = self._get_estimate()
  85. if estimate < 10000:
  86. self._count = self.object_list.count()
  87. else:
  88. self._count = estimate
  89. except (AttributeError, TypeError):
  90. # AttributeError if object_list has no count() method.
  91. # TypeError if object_list.count() requires arguments
  92. # (i.e. is of type list).
  93. self._count = len(self.object_list)
  94. return self._count
  95. count = property(_get_count)
  96. @admin.register(Ping)
  97. class PingsAdmin(admin.ModelAdmin):
  98. search_fields = ("owner__name", "owner__code", "owner__user__email")
  99. list_select_related = ("owner", "owner__user")
  100. list_display = ("id", "created", "check_name", "email", "scheme", "method",
  101. "ua")
  102. list_filter = ("created", SchemeListFilter, MethodListFilter)
  103. paginator = LargeTablePaginator
  104. def check_name(self, obj):
  105. return obj.owner.name if obj.owner.name else obj.owner.code
  106. def email(self, obj):
  107. return obj.owner.user.email if obj.owner.user else None
  108. @admin.register(Channel)
  109. class ChannelsAdmin(admin.ModelAdmin):
  110. search_fields = ["value", "user__email"]
  111. list_select_related = ("user", )
  112. list_display = ("id", "code", "email", "formatted_kind", "value",
  113. "num_notifications")
  114. list_filter = ("kind", )
  115. def email(self, obj):
  116. return obj.user.email if obj.user else None
  117. def formatted_kind(self, obj):
  118. if obj.kind == "pd":
  119. return "PagerDuty"
  120. elif obj.kind == "victorops":
  121. return "VictorOps"
  122. elif obj.kind == "po":
  123. return "Pushover"
  124. elif obj.kind == "webhook":
  125. return "Webhook"
  126. elif obj.kind == "slack":
  127. return "Slack"
  128. elif obj.kind == "hipchat":
  129. return "HipChat"
  130. elif obj.kind == "email" and obj.email_verified:
  131. return "Email"
  132. elif obj.kind == "email" and not obj.email_verified:
  133. return "Email <i>(unverified)</i>"
  134. else:
  135. raise NotImplementedError("Bad channel kind: %s" % obj.kind)
  136. formatted_kind.short_description = "Kind"
  137. formatted_kind.allow_tags = True
  138. def num_notifications(self, obj):
  139. return Notification.objects.filter(channel=obj).count()
  140. num_notifications.short_description = "# Notifications"
  141. @admin.register(Notification)
  142. class NotificationsAdmin(admin.ModelAdmin):
  143. search_fields = ["owner__name", "owner__code", "channel__value"]
  144. list_select_related = ("owner", "channel")
  145. list_display = ("id", "created", "check_status", "check_name",
  146. "channel_kind", "channel_value")
  147. list_filter = ("created", "check_status", "channel__kind")
  148. def check_name(self, obj):
  149. return obj.owner.name_then_code()
  150. def channel_kind(self, obj):
  151. return obj.channel.kind
  152. def channel_value(self, obj):
  153. return obj.channel.value