Skip to content

Commit f8a64de

Browse files
committed
basic admin config + admin tests sanity checks
1 parent 3c04d17 commit f8a64de

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

intbot/core/admin.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,71 @@
1+
import json
2+
3+
from core.models import DiscordMessage, Webhook
14
from django.contrib import admin
5+
from django.utils.html import format_html
6+
7+
8+
class WebhookAdmin(admin.ModelAdmin):
9+
list_display = [
10+
"uuid",
11+
"source",
12+
"event",
13+
"created_at",
14+
"modified_at",
15+
]
16+
list_filter = ["created_at"]
17+
readonly_fields = fields = [
18+
"uuid",
19+
"source",
20+
"event",
21+
"signature",
22+
"pretty_meta",
23+
"pretty_content",
24+
"created_at",
25+
"modified_at",
26+
"processed_at",
27+
]
28+
29+
def pretty_meta(self, obj):
30+
return format_html("<pre>{}</pre>", json.dumps(obj.meta, indent=4))
31+
32+
pretty_meta.short_description = "Meta"
33+
34+
def pretty_content(self, obj):
35+
return format_html("<pre>{}</pre>", json.dumps(obj.content, indent=4))
36+
37+
pretty_content.short_description = "Content"
38+
39+
40+
class DiscordMessageAdmin(admin.ModelAdmin):
41+
list_display = [
42+
"uuid",
43+
"channel_name",
44+
"content_short",
45+
"channel_id",
46+
"created_at",
47+
"modified_at",
48+
"sent_at",
49+
]
50+
list_filter = [
51+
"created_at",
52+
"sent_at",
53+
"channel_name",
54+
]
55+
readonly_fields = fields = [
56+
"uuid",
57+
"channel_id",
58+
"content",
59+
"created_at",
60+
"modified_at",
61+
"sent_at",
62+
]
63+
64+
def content_short(self, obj):
65+
# NOTE(artcz) This can create false shortcuts, but for most messages is
66+
# good enough, because most of them are longer than 20 chars
67+
return f"{obj.content[:10]}...{obj.content[-10:]}"
268

3-
from core.models import Webhook, DiscordMessage
469

5-
admin.site.register(Webhook)
6-
admin.site.register(DiscordMessage)
70+
admin.site.register(Webhook, WebhookAdmin)
71+
admin.site.register(DiscordMessage, DiscordMessageAdmin)

intbot/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Webhook(models.Model):
1414

1515
# Extra information about the webhook, that might be provided in the
1616
# headers or similar
17-
meta = models.JSONField(default={})
17+
meta = models.JSONField(default=dict)
1818

1919
content = models.JSONField()
2020

intbot/tests/test_admin.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Sanity checks (mostly) if the admin resources are available
3+
"""
4+
5+
from core.models import DiscordMessage, Webhook
6+
7+
8+
def test_admin_for_webhooks_sanity_check(admin_client):
9+
url = "/admin/core/webhook/"
10+
wh = Webhook.objects.create(content={})
11+
assert wh.uuid
12+
13+
response = admin_client.get(url)
14+
15+
assert response.status_code == 200
16+
assert str(wh.uuid).encode() in response.content
17+
assert wh.source.encode() in response.content
18+
assert wh.event.encode() in response.content
19+
20+
21+
def test_admin_for_discordmessages_sanity_check(admin_client):
22+
url = "/admin/core/discordmessage/"
23+
dm = DiscordMessage.objects.create(
24+
channel_id="12345",
25+
channel_name="#test",
26+
)
27+
assert dm.uuid
28+
29+
response = admin_client.get(url)
30+
31+
assert response.status_code == 200
32+
assert str(dm.uuid).encode() in response.content
33+
assert dm.channel_id.encode() in response.content
34+
assert dm.channel_name.encode() in response.content

0 commit comments

Comments
 (0)