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.

114 lines
3.4 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
9 years ago
  1. from django.contrib import admin
  2. from hc.api.models import Channel, Check, Notification, Ping
  3. class OwnershipListFilter(admin.SimpleListFilter):
  4. title = "Ownership"
  5. parameter_name = 'ownership'
  6. def lookups(self, request, model_admin):
  7. return (
  8. ('assigned', "Assigned"),
  9. )
  10. def queryset(self, request, queryset):
  11. if self.value() == 'assigned':
  12. return queryset.filter(user__isnull=False)
  13. return queryset
  14. @admin.register(Check)
  15. class ChecksAdmin(admin.ModelAdmin):
  16. class Media:
  17. css = {
  18. 'all': ('css/admin/checks.css',)
  19. }
  20. search_fields = ["name", "user__email"]
  21. list_display = ("id", "name", "created", "code", "status", "email",
  22. "last_ping")
  23. list_select_related = ("user", )
  24. list_filter = ("status", OwnershipListFilter, "last_ping")
  25. actions = ["send_alert"]
  26. def email(self, obj):
  27. return obj.user.email if obj.user else None
  28. def send_alert(self, request, qs):
  29. for check in qs:
  30. check.send_alert()
  31. self.message_user(request, "%d alert(s) sent" % qs.count())
  32. send_alert.short_description = "Send Alert"
  33. @admin.register(Ping)
  34. class PingsAdmin(admin.ModelAdmin):
  35. search_fields = ("owner__name", "owner__code", "owner__user__email")
  36. list_select_related = ("owner", "owner__user")
  37. list_display = ("id", "created", "check_name", "email", "scheme", "method",
  38. "ua")
  39. list_filter = ("created", "scheme", "method")
  40. def check_name(self, obj):
  41. return obj.owner.name if obj.owner.name else obj.owner.code
  42. def email(self, obj):
  43. return obj.owner.user.email if obj.owner.user else None
  44. @admin.register(Channel)
  45. class ChannelsAdmin(admin.ModelAdmin):
  46. search_fields = ["value", "user__email"]
  47. list_select_related = ("user", )
  48. list_display = ("id", "code", "email", "formatted_kind", "value",
  49. "num_notifications")
  50. list_filter = ("kind", )
  51. def email(self, obj):
  52. return obj.user.email if obj.user else None
  53. def formatted_kind(self, obj):
  54. if obj.kind == "pd":
  55. return "PagerDuty"
  56. elif obj.kind == "webhook":
  57. return "Webhook"
  58. elif obj.kind == "slack":
  59. return "Slack"
  60. elif obj.kind == "hipchat":
  61. return "HipChat"
  62. elif obj.kind == "email" and obj.email_verified:
  63. return "Email"
  64. elif obj.kind == "email" and not obj.email_verified:
  65. return "Email <i>(unverified)</i>"
  66. else:
  67. raise NotImplementedError("Bad channel kind: %s" % obj.kind)
  68. formatted_kind.short_description = "Kind"
  69. formatted_kind.allow_tags = True
  70. def num_notifications(self, obj):
  71. return Notification.objects.filter(channel=obj).count()
  72. num_notifications.short_description = "# Notifications"
  73. @admin.register(Notification)
  74. class NotificationsAdmin(admin.ModelAdmin):
  75. search_fields = ["owner__name", "owner__code", "channel__value"]
  76. list_select_related = ("owner", "channel")
  77. list_display = ("id", "created", "check_status", "check_name",
  78. "channel_kind", "channel_value", "status")
  79. list_filter = ("created", "check_status", "channel__kind")
  80. def check_name(self, obj):
  81. return obj.owner.name_then_code()
  82. def channel_kind(self, obj):
  83. return obj.channel.kind
  84. def channel_value(self, obj):
  85. return obj.channel.value