This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/activities/admin.py

180 lines
5.4 KiB
Python
Raw Normal View History

2022-11-29 05:41:36 +01:00
from asgiref.sync import async_to_sync
2022-11-12 06:02:43 +01:00
from django.contrib import admin
2022-12-15 08:50:54 +01:00
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
2022-11-12 06:02:43 +01:00
2022-11-17 07:00:10 +01:00
from activities.models import (
2022-12-15 08:50:54 +01:00
Emoji,
2022-11-17 07:00:10 +01:00
FanOut,
2022-11-29 05:41:36 +01:00
Hashtag,
2022-11-17 07:00:10 +01:00
Post,
PostAttachment,
PostInteraction,
TimelineEvent,
)
class IdentityLocalFilter(admin.SimpleListFilter):
title = _("Local Identity")
parameter_name = "islocal"
identity_field_name = "identity"
def lookups(self, request, model_admin):
return (
("1", _("Yes")),
("0", _("No")),
)
def queryset(self, request, queryset):
match self.value():
case "1":
return queryset.filter(**{f"{self.identity_field_name}__local": True})
case "0":
return queryset.filter(**{f"{self.identity_field_name}__local": False})
case _:
return queryset
2022-11-29 05:41:36 +01:00
@admin.register(Hashtag)
class HashtagAdmin(admin.ModelAdmin):
list_display = ["hashtag", "name_override", "state", "stats_updated", "created"]
list_filter = ("public", "state", "stats_updated")
search_fields = ["hashtag", "aliases"]
2022-11-29 05:41:36 +01:00
readonly_fields = ["created", "updated", "stats_updated"]
actions = ["force_execution"]
@admin.action(description="Force Execution")
def force_execution(self, request, queryset):
for instance in queryset:
instance.transition_perform("outdated")
2022-12-15 08:50:54 +01:00
@admin.register(Emoji)
class EmojiAdmin(admin.ModelAdmin):
list_display = (
"shortcode",
"preview",
"local",
"domain",
"public",
"state",
"created",
)
list_filter = ("local", "public", "state")
search_fields = ("shortcode",)
2022-12-17 20:07:13 +01:00
readonly_fields = ["preview", "created", "updated", "to_ap_tag"]
2022-12-15 08:50:54 +01:00
actions = ["force_execution", "approve_emoji", "reject_emoji"]
@admin.action(description="Force Execution")
def force_execution(self, request, queryset):
for instance in queryset:
instance.transition_perform("outdated")
@admin.action(description="Approve Emoji")
def approve_emoji(self, request, queryset):
queryset.update(public=True)
@admin.action(description="Reject Emoji")
def reject_emoji(self, request, queryset):
queryset.update(public=False)
@admin.display(description="Emoji Preview")
def preview(self, instance):
if instance.public is False:
return mark_safe(f'<a href="{instance.full_url().relative}">Preview</a>')
return mark_safe(
f'<img src="{instance.full_url().relative}" style="height: 22px">'
)
2022-12-02 02:46:49 +01:00
@admin.register(PostAttachment)
class PostAttachmentAdmin(admin.ModelAdmin):
list_display = ["id", "post", "created"]
2022-11-17 07:00:10 +01:00
class PostAttachmentInline(admin.StackedInline):
model = PostAttachment
extra = 0
2022-11-12 06:02:43 +01:00
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
2022-11-12 07:04:43 +01:00
list_display = ["id", "state", "author", "created"]
list_filter = ("local", "visibility", "state", "created")
2022-12-15 19:56:48 +01:00
raw_id_fields = ["to", "mentions", "author", "emojis"]
2022-11-29 05:41:36 +01:00
actions = ["force_fetch", "reparse_hashtags"]
2022-11-17 07:00:10 +01:00
search_fields = ["content"]
inlines = [PostAttachmentInline]
2022-12-17 05:51:16 +01:00
readonly_fields = ["created", "updated", "state_changed", "object_json"]
2022-11-14 03:03:43 +01:00
@admin.action(description="Force Fetch")
def force_fetch(self, request, queryset):
for instance in queryset:
instance.debug_fetch()
2022-11-12 06:02:43 +01:00
2022-11-29 05:41:36 +01:00
@admin.action(description="Reprocess content for hashtags")
def reparse_hashtags(self, request, queryset):
for instance in queryset:
instance.hashtags = Hashtag.hashtags_from_content(instance.content) or None
instance.save()
async_to_sync(instance.ensure_hashtags)()
@admin.display(description="ActivityPub JSON")
def object_json(self, instance):
return instance.to_ap()
2022-11-20 19:18:42 +01:00
def has_add_permission(self, request, obj=None):
"""
Disables admin creation of posts as it will skip steps
"""
return False
2022-11-12 06:02:43 +01:00
@admin.register(TimelineEvent)
class TimelineEventAdmin(admin.ModelAdmin):
list_display = ["id", "identity", "created", "type"]
list_filter = (IdentityLocalFilter, "type")
2022-11-18 02:52:00 +01:00
readonly_fields = ["created"]
raw_id_fields = [
"identity",
"subject_post",
"subject_identity",
"subject_post_interaction",
]
2022-11-12 07:04:43 +01:00
2022-11-28 01:16:09 +01:00
def has_add_permission(self, request, obj=None):
return False
2022-11-12 07:04:43 +01:00
@admin.register(FanOut)
class FanOutAdmin(admin.ModelAdmin):
2022-12-17 05:51:16 +01:00
list_display = ["id", "state", "created", "state_attempted", "type", "identity"]
list_filter = (IdentityLocalFilter, "type", "state", "state_attempted")
raw_id_fields = ["identity", "subject_post", "subject_post_interaction"]
2022-12-17 05:51:16 +01:00
readonly_fields = ["created", "updated", "state_changed"]
actions = ["force_execution"]
2022-12-17 05:51:16 +01:00
search_fields = ["identity__username"]
@admin.action(description="Force Execution")
def force_execution(self, request, queryset):
for instance in queryset:
instance.transition_perform("new")
2022-11-14 02:42:47 +01:00
2022-11-28 01:16:09 +01:00
def has_add_permission(self, request, obj=None):
return False
2022-11-14 02:42:47 +01:00
@admin.register(PostInteraction)
class PostInteractionAdmin(admin.ModelAdmin):
list_display = ["id", "state", "state_attempted", "type", "identity", "post"]
list_filter = (IdentityLocalFilter, "type", "state")
2022-11-14 02:42:47 +01:00
raw_id_fields = ["identity", "post"]
2022-11-28 01:16:09 +01:00
def has_add_permission(self, request, obj=None):
return False