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.

110 lines
3.3 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 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", )
  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. def email(self, obj):
  51. return obj.user.email if obj.user else None
  52. def formatted_kind(self, obj):
  53. if obj.kind == "pd":
  54. return "PagerDuty"
  55. elif obj.kind == "webhook":
  56. return "Webhook"
  57. elif obj.kind == "email" and obj.email_verified:
  58. return "Email"
  59. elif obj.kind == "email" and not obj.email_verified:
  60. return "Email <i>(unverified)</i>"
  61. else:
  62. raise NotImplementedError("Bad channel kind: %s" % obj.kind)
  63. formatted_kind.short_description = "Kind"
  64. formatted_kind.allow_tags = True
  65. def num_notifications(self, obj):
  66. return Notification.objects.filter(channel=obj).count()
  67. num_notifications.short_description = "# Notifications"
  68. @admin.register(Notification)
  69. class NotificationsAdmin(admin.ModelAdmin):
  70. search_fields = ["owner__name", "owner__code", "channel__value"]
  71. list_select_related = ("owner", "channel")
  72. list_display = ("id", "created", "check_status", "check_name",
  73. "channel_kind", "channel_value", "status")
  74. list_filter = ("created", "check_status", "channel__kind")
  75. def check_name(self, obj):
  76. return obj.owner.name_then_code()
  77. def channel_kind(self, obj):
  78. return obj.channel.kind
  79. def channel_value(self, obj):
  80. return obj.channel.value