From 5c2e750b6ff19248aaafbb0a1c12f8c7523b7273 Mon Sep 17 00:00:00 2001 From: scragly <29337040+scragly@users.noreply.github.com> Date: Fri, 18 Sep 2020 14:59:30 +1000 Subject: Update DeletedMessage and LogEntry Admin models, add verbose names for Message --- pydis_site/apps/api/admin.py | 167 ++++++++++++++++-------------- pydis_site/apps/api/models/bot/message.py | 6 +- 2 files changed, 95 insertions(+), 78 deletions(-) (limited to 'pydis_site/apps/api') diff --git a/pydis_site/apps/api/admin.py b/pydis_site/apps/api/admin.py index a85b4cac..ca97512f 100644 --- a/pydis_site/apps/api/admin.py +++ b/pydis_site/apps/api/admin.py @@ -23,34 +23,77 @@ from .models import ( User ) +admin.site.site_header = "Python Discord | Administration" +admin.site.site_title = "Python Discord" + + +@admin.register(Infraction) +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" + ) + @admin.register(LogEntry) class LogEntryAdmin(admin.ModelAdmin): """Allows viewing logs in the Django Admin without allowing edits.""" actions = None - list_display = ('timestamp', 'application', 'level', 'message') + list_display = ('timestamp', 'level', 'message') fieldsets = ( ('Overview', {'fields': ('timestamp', 'application', 'logger_name')}), ('Metadata', {'fields': ('level', 'module', 'line')}), ('Contents', {'fields': ('message',)}) ) - list_filter = ('application', 'level', 'timestamp') + list_filter = ('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_change_permission(self, *args) -> bool: + """Prevent editing from django admin.""" + return False + def has_delete_permission(self, request: HttpRequest, obj: Optional[LogEntry] = None) -> bool: """Deny LogEntry deletion.""" return False @@ -60,7 +103,7 @@ class LogEntryAdmin(admin.ModelAdmin): class DeletedMessageAdmin(admin.ModelAdmin): """Admin formatting for the DeletedMessage model.""" - readonly_fields = ( + fields = ( "id", "author", "channel_id", @@ -81,96 +124,68 @@ class DeletedMessageAdmin(admin.ModelAdmin): "deletion_context__actor__id" ) - @staticmethod - def embed_data(instance: DeletedMessage) -> Optional[str]: + def embed_data(self, message: DeletedMessage) -> Optional[str]: """Format embed data in a code block for better readability.""" - if instance.embeds: + if message.embeds: return format_html( "
"
"{0}
",
- json.dumps(instance.embeds, indent=4)
+ json.dumps(message.embeds, indent=4)
)
+ embed_data.short_description = "Embeds"
+
@staticmethod
- def context(instance: DeletedMessage) -> str:
+ def context(message: 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]
+ args=[message.deletion_context.id]
)
details = (
- f"Deleted by {instance.deletion_context.actor} at "
- f"{instance.deletion_context.creation}"
+ f"Deleted by {message.deletion_context.actor} at "
+ f"{message.deletion_context.creation}"
)
return format_html("{1}", link, details)
@staticmethod
- def view_full_log(instance: DeletedMessage) -> str:
+ def view_full_log(message: DeletedMessage) -> str:
"""Provide a link to the message logs for the relevant context."""
return format_html(
"Click to view full context log",
- instance.deletion_context.log_url
+ message.deletion_context.log_url
)
+ def has_add_permission(self, *args) -> bool:
+ """Prevent adding from django admin."""
+ return False
+
+ def has_change_permission(self, *args) -> bool:
+ """Prevent editing from django admin."""
+ return False
+
+
+class DeletedMessageInline(admin.TabularInline):
+ """Tabular Inline Admin model for Deleted Message to be viewed within Context."""
+
+ model = DeletedMessage
+
@admin.register(MessageDeletionContext)
class MessageDeletionContextAdmin(admin.ModelAdmin):
"""Admin formatting for the MessageDeletionContext model."""
- readonly_fields = ("actor", "creation", "message_log")
+ fields = ("actor", "creation")
+ list_display = ("id", "creation", "actor")
+ inlines = (DeletedMessageInline,)
- @staticmethod
- def message_log(instance: MessageDeletionContext) -> str:
- """Provide a formatted link to the message logs for the context."""
- return format_html(
- "Click to see deleted message log",
- instance.log_url
- )
-
-
-@admin.register(Infraction)
-class InfractionAdmin(admin.ModelAdmin):
- """Admin formatting for the Infraction model."""
+ def has_add_permission(self, *args) -> bool:
+ """Prevent adding from django admin."""
+ return False
- 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"
- )
+ def has_change_permission(self, *args) -> bool:
+ """Prevent editing from django admin."""
+ return False
class NominationActorFilter(admin.SimpleListFilter):
@@ -179,7 +194,7 @@ class NominationActorFilter(admin.SimpleListFilter):
title = "Actor"
parameter_name = "actor"
- def lookups(self, request: HttpRequest, model_admin: NominationAdmin) -> Iterable[Tuple[int, str]]:
+ def lookups(self, request: HttpRequest, model: NominationAdmin) -> Iterable[Tuple[int, str]]:
"""Selectable values for viewer to filter by."""
actor_ids = Nomination.objects.order_by().values_list("actor").distinct()
actors = User.objects.filter(id__in=actor_ids)
@@ -322,7 +337,7 @@ class UserTopRoleFilter(admin.SimpleListFilter):
title = "Role"
parameter_name = "role"
- def lookups(self, request: HttpRequest, model_admin: UserAdmin) -> Iterable[Tuple[str, str]]:
+ def lookups(self, request: HttpRequest, model: UserAdmin) -> Iterable[Tuple[str, str]]:
"""Selectable values for viewer to filter by."""
roles = Role.objects.all()
return ((r.name, r.name) for r in roles)
diff --git a/pydis_site/apps/api/models/bot/message.py b/pydis_site/apps/api/models/bot/message.py
index f6ae55a5..ff06de21 100644
--- a/pydis_site/apps/api/models/bot/message.py
+++ b/pydis_site/apps/api/models/bot/message.py
@@ -21,7 +21,8 @@ class Message(ModelReprMixin, models.Model):
limit_value=0,
message="Message IDs cannot be negative."
),
- )
+ ),
+ verbose_name="ID"
)
author = models.ForeignKey(
User,
@@ -38,7 +39,8 @@ class Message(ModelReprMixin, models.Model):
limit_value=0,
message="Channel IDs cannot be negative."
),
- )
+ ),
+ verbose_name="Channel ID"
)
content = models.CharField(
max_length=2_000,
--
cgit v1.2.3