aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/admin.py
blob: 010541a67cedb1f89253f2366ccb982c0914bac5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import json
from typing import Optional, Tuple

from django import urls
from django.contrib import admin
from django.db.models.query import QuerySet
from django.http import HttpRequest
from django.utils.html import format_html

from .models import (
    BotSetting,
    DeletedMessage,
    DocumentationLink,
    Infraction,
    LogEntry,
    MessageDeletionContext,
    Nomination,
    OffTopicChannelName,
    Role,
    Tag,
    User
)


class LogEntryAdmin(admin.ModelAdmin):
    """Allows viewing logs in the Django Admin without allowing edits."""

    actions = None
    list_display = ('timestamp', 'application', 'level', 'message')
    fieldsets = (
        ('Overview', {'fields': ('timestamp', 'application', 'logger_name')}),
        ('Metadata', {'fields': ('level', 'module', 'line')}),
        ('Contents', {'fields': ('message',)})
    )
    list_filter = ('application', 'level', 'timestamp')
    search_fields = ('message',)
    readonly_fields = (
        'application',
        'logger_name',
        'timestamp',
        'level',
        'module',
        'line',
        'message'
    )

    def has_add_permission(self, request: HttpRequest) -> bool:
        """Deny manual LogEntry creation."""
        return False

    def has_delete_permission(self, request: HttpRequest, obj: Optional[LogEntry] = None) -> bool:
        """Deny LogEntry deletion."""
        return False


class DeletedMessageAdmin(admin.ModelAdmin):
    """Admin formatting for the DeletedMessage model."""

    readonly_fields = (
        "id",
        "author",
        "channel_id",
        "content",
        "embed_data",
        "context",
        "view_full_log"
    )

    exclude = ("embeds", "deletion_context")

    search_fields = (
        "id",
        "content",
        "author__name",
        "author__id",
        "deletion_context__actor__name",
        "deletion_context__actor__id"
    )

    @staticmethod
    def embed_data(instance: DeletedMessage) -> Optional[str]:
        """Format embed data in a code block for better readability."""
        if instance.embeds:
            return format_html(
                "<pre style='word-wrap: break-word; white-space: pre-wrap; overflow-x: auto;'>"
                "<code>{0}</code></pre>",
                json.dumps(instance.embeds, indent=4)
            )

    @staticmethod
    def context(instance: DeletedMessage) -> str:
        """Provide full context info with a link through to context admin view."""
        link = urls.reverse(
            "admin:api_messagedeletioncontext_change",
            args=[instance.deletion_context.id]
        )
        details = (
            f"Deleted by {instance.deletion_context.actor} at "
            f"{instance.deletion_context.creation}"
        )
        return format_html("<a href='{0}'>{1}</a>", link, details)

    @staticmethod
    def view_full_log(instance: DeletedMessage) -> str:
        """Provide a link to the message logs for the relevant context."""
        return format_html(
            "<a href='{0}'>Click to view full context log</a>",
            instance.deletion_context.log_url
        )


class MessageDeletionContextAdmin(admin.ModelAdmin):
    """Admin formatting for the MessageDeletionContext model."""

    readonly_fields = ("actor", "creation", "message_log")

    @staticmethod
    def message_log(instance: MessageDeletionContext) -> str:
        """Provide a formatted link to the message logs for the context."""
        return format_html(
            "<a href='{0}'>Click to see deleted message log</a>",
            instance.log_url
        )


class InfractionAdmin(admin.ModelAdmin):
    """Admin formatting for the Infraction model."""

    fields = (
        "user",
        "actor",
        "type",
        "reason",
        "inserted_at",
        "expires_at",
        "active",
        "hidden"
    )
    readonly_fields = (
        "user",
        "actor",
        "type",
        "inserted_at"
    )
    list_display = (
        "type",
        "user",
        "actor",
        "inserted_at",
        "expires_at",
        "reason",
        "active",
    )
    search_fields = (
        "id",
        "user__name",
        "user__id",
        "actor__name",
        "actor__id",
        "reason",
        "type"
    )
    list_filter = (
        "type",
        "hidden",
        "active"
    )


class NominationAdmin(admin.ModelAdmin):
    """Admin formatting for the Nomination model."""

    list_display = (
        "user",
        "active",
        "reason",
        "actor",
        "inserted_at",
        "ended_at"
    )
    fields = (
        "user",
        "active",
        "actor",
        "reason",
        "inserted_at",
        "ended_at",
        "end_reason"
    )
    readonly_fields = (
        "user",
        "active",
        "actor",
        "inserted_at",
        "ended_at"
    )
    search_fields = (
        "actor__name",
        "actor__id",
        "user__name",
        "user__id",
        "reason"
    )
    list_filter = ("active",)


class OffTopicChannelNameAdmin(admin.ModelAdmin):
    """Admin formatting for the OffTopicChannelName model."""

    search_fields = ("name",)


class RoleAdmin(admin.ModelAdmin):
    """Admin formatting for the Role model."""

    exclude = ("permissions", "colour")
    readonly_fields = (
        "name",
        "id",
        "colour_with_preview",
        "permissions_with_calc_link",
        "position"
    )
    search_fields = ("name", "id")

    def colour_with_preview(self, instance: Role) -> str:
        """Show colour value in both int and hex, in bolded and coloured style."""
        return format_html(
            "<span style='color: #{0}!important; font-weight: bold;'>{1} / #{0}</span>",
            f"{instance.colour:06x}",
            instance.colour
        )

    def permissions_with_calc_link(self, instance: Role) -> str:
        """Show permissions with link to API permissions calculator page."""
        return format_html(
            "<a href='https://discordapi.com/permissions.html#{0}' target='_blank'>{0}</a>",
            instance.permissions
        )

    colour_with_preview.short_description = "Colour"
    permissions_with_calc_link.short_description = "Permissions"


class TagAdmin(admin.ModelAdmin):
    """Admin formatting for the Tag model."""

    fields = ("title", "embed", "preview")
    readonly_fields = ("preview",)
    search_fields = ("title", "embed")

    @staticmethod
    def preview(instance: Tag) -> Optional[str]:
        """Render tag markdown contents to preview actual appearance."""
        if instance.embed:
            import markdown
            return format_html(
                markdown.markdown(
                    instance.embed["description"],
                    extensions=[
                        "markdown.extensions.nl2br",
                        "markdown.extensions.extra"
                    ]
                )
            )


class StaffRolesFilter(admin.SimpleListFilter):
    """Filter options for Staff Roles."""

    title = "Staff Role"
    parameter_name = "staff_role"

    @staticmethod
    def lookups(*_) -> Tuple[Tuple[str, str], ...]:
        """Available filter options."""
        return (
            ("Owners", "Owners"),
            ("Admins", "Admins"),
            ("Moderators", "Moderators"),
            ("Core Developers", "Core Developers"),
            ("Helpers", "Helpers"),
        )

    def queryset(self, request: HttpRequest, queryset: QuerySet) -> Optional[QuerySet]:
        """Returned data filter based on selected option."""
        value = self.value()
        if value:
            return queryset.filter(roles__name=value)


class UserAdmin(admin.ModelAdmin):
    """Admin formatting for the User model."""

    search_fields = ("name", "id", "roles__name", "roles__id")
    list_filter = ("in_guild", StaffRolesFilter)
    exclude = ("name", "discriminator")
    readonly_fields = (
        "__str__",
        "id",
        "avatar_hash",
        "top_role",
        "roles",
        "in_guild",
    )


admin.site.register(BotSetting)
admin.site.register(DeletedMessage, DeletedMessageAdmin)
admin.site.register(DocumentationLink)
admin.site.register(Infraction, InfractionAdmin)
admin.site.register(LogEntry, LogEntryAdmin)
admin.site.register(MessageDeletionContext, MessageDeletionContextAdmin)
admin.site.register(Nomination, NominationAdmin)
admin.site.register(OffTopicChannelName, OffTopicChannelNameAdmin)
admin.site.register(Role, RoleAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(User, UserAdmin)