2022-11-29 04:41:36 +00:00
|
|
|
from asgiref.sync import async_to_sync
|
2022-11-12 05:02:43 +00:00
|
|
|
from django.contrib import admin
|
2023-01-16 22:54:49 +00:00
|
|
|
from django.db import models
|
2022-12-15 07:50:54 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2022-12-08 17:29:51 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-12 05:02:43 +00:00
|
|
|
|
2022-11-17 06:00:10 +00:00
|
|
|
from activities.models import (
|
2022-12-15 07:50:54 +00:00
|
|
|
Emoji,
|
2022-11-17 06:00:10 +00:00
|
|
|
FanOut,
|
2022-11-29 04:41:36 +00:00
|
|
|
Hashtag,
|
2022-11-17 06:00:10 +00:00
|
|
|
Post,
|
|
|
|
PostAttachment,
|
|
|
|
PostInteraction,
|
|
|
|
TimelineEvent,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-08 17:29:51 +00:00
|
|
|
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 04:41:36 +00:00
|
|
|
@admin.register(Hashtag)
|
|
|
|
class HashtagAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["hashtag", "name_override", "state", "stats_updated", "created"]
|
2022-12-08 17:29:51 +00:00
|
|
|
list_filter = ("public", "state", "stats_updated")
|
|
|
|
search_fields = ["hashtag", "aliases"]
|
2022-11-29 04:41:36 +00:00
|
|
|
|
|
|
|
readonly_fields = ["created", "updated", "stats_updated"]
|
|
|
|
|
2022-12-28 02:42:38 +00:00
|
|
|
actions = ["force_state_outdated", "force_state_updated"]
|
2022-11-29 04:41:36 +00:00
|
|
|
|
2022-12-28 02:42:38 +00:00
|
|
|
@admin.action(description="Force State: outdated")
|
|
|
|
def force_state_outdated(self, request, queryset):
|
2022-11-29 04:41:36 +00:00
|
|
|
for instance in queryset:
|
|
|
|
instance.transition_perform("outdated")
|
|
|
|
|
2022-12-28 02:42:38 +00:00
|
|
|
@admin.action(description="Force State: updated")
|
|
|
|
def force_state_updated(self, request, queryset):
|
|
|
|
for instance in queryset:
|
|
|
|
instance.transition_perform("updated")
|
|
|
|
|
2022-11-29 04:41:36 +00:00
|
|
|
|
2022-12-15 07:50:54 +00: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 19:07:13 +00:00
|
|
|
readonly_fields = ["preview", "created", "updated", "to_ap_tag"]
|
2022-12-15 07:50:54 +00:00
|
|
|
|
2023-01-14 17:35:20 +00:00
|
|
|
actions = ["force_execution", "approve_emoji", "reject_emoji", "copy_to_local"]
|
|
|
|
|
|
|
|
def delete_queryset(self, request, queryset):
|
|
|
|
for instance in queryset:
|
|
|
|
# individual deletes to ensure file is deleted
|
|
|
|
instance.delete()
|
|
|
|
|
|
|
|
def delete_model(self, request, obj):
|
|
|
|
super().delete_model(request, obj)
|
2022-12-15 07:50:54 +00:00
|
|
|
|
|
|
|
@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">'
|
|
|
|
)
|
|
|
|
|
2023-01-14 17:35:20 +00:00
|
|
|
@admin.action(description="Copy Emoji to Local")
|
|
|
|
def copy_to_local(self, request, queryset):
|
|
|
|
emojis = {}
|
|
|
|
for instance in queryset:
|
|
|
|
emoji = instance.copy_to_local(save=False)
|
|
|
|
if emoji:
|
|
|
|
emojis[emoji.shortcode] = emoji
|
|
|
|
|
|
|
|
Emoji.objects.bulk_create(emojis.values(), batch_size=50, ignore_conflicts=True)
|
|
|
|
Emoji.locals = Emoji.load_locals()
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
|
2022-12-02 01:46:49 +00:00
|
|
|
@admin.register(PostAttachment)
|
|
|
|
class PostAttachmentAdmin(admin.ModelAdmin):
|
2023-01-16 22:54:49 +00:00
|
|
|
list_display = ["id", "post", "state", "created"]
|
|
|
|
list_filter = ["state", "mimetype"]
|
|
|
|
search_fields = ["name", "remote_url", "search_handle", "search_service_handle"]
|
|
|
|
raw_id_fields = ["post"]
|
|
|
|
|
|
|
|
actions = ["guess_mimetypes"]
|
|
|
|
|
|
|
|
def get_search_results(self, request, queryset, search_term):
|
|
|
|
from django.db.models.functions import Concat
|
|
|
|
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
search_handle=Concat(
|
|
|
|
"post__author__username", models.Value("@"), "post__author__domain_id"
|
|
|
|
),
|
|
|
|
search_service_handle=Concat(
|
|
|
|
"post__author__username",
|
|
|
|
models.Value("@"),
|
|
|
|
"post__author__domain__service_domain",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return super().get_search_results(request, queryset, search_term)
|
|
|
|
|
|
|
|
@admin.action(description="Update mimetype based upon filename")
|
|
|
|
def guess_mimetypes(self, request, queryset):
|
|
|
|
import mimetypes
|
|
|
|
|
|
|
|
for instance in queryset:
|
|
|
|
if instance.remote_url:
|
|
|
|
mimetype, _ = mimetypes.guess_type(instance.remote_url)
|
|
|
|
if not mimetype:
|
|
|
|
mimetype = "application/octet-stream"
|
|
|
|
instance.mimetype = mimetype
|
|
|
|
instance.save()
|
2022-12-02 01:46:49 +00:00
|
|
|
|
|
|
|
|
2022-11-17 06:00:10 +00:00
|
|
|
class PostAttachmentInline(admin.StackedInline):
|
|
|
|
model = PostAttachment
|
|
|
|
extra = 0
|
2022-11-12 05:02:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Post)
|
|
|
|
class PostAdmin(admin.ModelAdmin):
|
2022-12-18 16:09:25 +00:00
|
|
|
list_display = ["id", "type", "author", "state", "created"]
|
|
|
|
list_filter = ("type", "local", "visibility", "state", "created")
|
2023-01-26 17:01:21 +00:00
|
|
|
raw_id_fields = ["emojis"]
|
|
|
|
autocomplete_fields = ["to", "mentions", "author"]
|
2022-12-19 02:47:35 +00:00
|
|
|
actions = ["reparse_hashtags"]
|
2023-01-16 22:54:49 +00:00
|
|
|
search_fields = ["content", "search_handle", "search_service_handle"]
|
2022-11-17 06:00:10 +00:00
|
|
|
inlines = [PostAttachmentInline]
|
2022-12-17 04:51:16 +00:00
|
|
|
readonly_fields = ["created", "updated", "state_changed", "object_json"]
|
2022-11-14 02:03:43 +00:00
|
|
|
|
2023-01-16 22:54:49 +00:00
|
|
|
def get_search_results(self, request, queryset, search_term):
|
|
|
|
from django.db.models.functions import Concat
|
|
|
|
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
search_handle=Concat(
|
|
|
|
"author__username", models.Value("@"), "author__domain_id"
|
|
|
|
),
|
|
|
|
search_service_handle=Concat(
|
|
|
|
"author__username", models.Value("@"), "author__domain__service_domain"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return super().get_search_results(request, queryset, search_term)
|
|
|
|
|
2022-11-29 04:41:36 +00: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)()
|
|
|
|
|
2022-11-16 01:30:30 +00:00
|
|
|
@admin.display(description="ActivityPub JSON")
|
|
|
|
def object_json(self, instance):
|
|
|
|
return instance.to_ap()
|
|
|
|
|
2022-11-20 18:18:42 +00:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
"""
|
|
|
|
Disables admin creation of posts as it will skip steps
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2022-11-12 05:02:43 +00:00
|
|
|
|
|
|
|
@admin.register(TimelineEvent)
|
|
|
|
class TimelineEventAdmin(admin.ModelAdmin):
|
2022-12-18 16:44:56 +00:00
|
|
|
list_display = ["id", "identity", "published", "type"]
|
2022-12-08 17:29:51 +00:00
|
|
|
list_filter = (IdentityLocalFilter, "type")
|
2022-11-18 01:52:00 +00:00
|
|
|
readonly_fields = ["created"]
|
2023-01-26 17:01:21 +00:00
|
|
|
autocomplete_fields = ["identity"]
|
2022-11-16 01:30:30 +00:00
|
|
|
raw_id_fields = [
|
|
|
|
"subject_post",
|
|
|
|
"subject_identity",
|
|
|
|
"subject_post_interaction",
|
|
|
|
]
|
2022-11-12 06:04:43 +00:00
|
|
|
|
2022-11-28 00:16:09 +00:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
2022-11-12 06:04:43 +00:00
|
|
|
|
|
|
|
@admin.register(FanOut)
|
|
|
|
class FanOutAdmin(admin.ModelAdmin):
|
2022-12-17 04:51:16 +00:00
|
|
|
list_display = ["id", "state", "created", "state_attempted", "type", "identity"]
|
2022-12-08 17:29:51 +00:00
|
|
|
list_filter = (IdentityLocalFilter, "type", "state", "state_attempted")
|
2023-01-26 17:01:21 +00:00
|
|
|
raw_id_fields = ["subject_post", "subject_post_interaction"]
|
|
|
|
autocomplete_fields = ["identity"]
|
2022-12-17 04:51:16 +00:00
|
|
|
readonly_fields = ["created", "updated", "state_changed"]
|
2022-11-16 01:30:30 +00:00
|
|
|
actions = ["force_execution"]
|
2022-12-17 04:51:16 +00:00
|
|
|
search_fields = ["identity__username"]
|
2022-11-16 01:30:30 +00:00
|
|
|
|
|
|
|
@admin.action(description="Force Execution")
|
|
|
|
def force_execution(self, request, queryset):
|
|
|
|
for instance in queryset:
|
|
|
|
instance.transition_perform("new")
|
2022-11-14 01:42:47 +00:00
|
|
|
|
2022-11-28 00:16:09 +00:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
2022-11-14 01:42:47 +00:00
|
|
|
|
|
|
|
@admin.register(PostInteraction)
|
|
|
|
class PostInteractionAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ["id", "state", "state_attempted", "type", "identity", "post"]
|
2022-12-08 17:29:51 +00:00
|
|
|
list_filter = (IdentityLocalFilter, "type", "state")
|
2023-01-26 17:01:21 +00:00
|
|
|
raw_id_fields = ["post"]
|
|
|
|
autocomplete_fields = ["identity"]
|
2022-11-28 00:16:09 +00:00
|
|
|
|
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return False
|