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.

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