mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-25 08:41:00 +00:00
Admin improvements for Post and PostAttachment (#428)
This commit is contained in:
parent
a18940aacf
commit
a34cab6dfb
1 changed files with 48 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
||||||
from asgiref.sync import async_to_sync
|
from asgiref.sync import async_to_sync
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.db import models
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
@ -118,7 +119,39 @@ class EmojiAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
@admin.register(PostAttachment)
|
@admin.register(PostAttachment)
|
||||||
class PostAttachmentAdmin(admin.ModelAdmin):
|
class PostAttachmentAdmin(admin.ModelAdmin):
|
||||||
list_display = ["id", "post", "created"]
|
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()
|
||||||
|
|
||||||
|
|
||||||
class PostAttachmentInline(admin.StackedInline):
|
class PostAttachmentInline(admin.StackedInline):
|
||||||
|
@ -132,10 +165,23 @@ class PostAdmin(admin.ModelAdmin):
|
||||||
list_filter = ("type", "local", "visibility", "state", "created")
|
list_filter = ("type", "local", "visibility", "state", "created")
|
||||||
raw_id_fields = ["to", "mentions", "author", "emojis"]
|
raw_id_fields = ["to", "mentions", "author", "emojis"]
|
||||||
actions = ["reparse_hashtags"]
|
actions = ["reparse_hashtags"]
|
||||||
search_fields = ["content"]
|
search_fields = ["content", "search_handle", "search_service_handle"]
|
||||||
inlines = [PostAttachmentInline]
|
inlines = [PostAttachmentInline]
|
||||||
readonly_fields = ["created", "updated", "state_changed", "object_json"]
|
readonly_fields = ["created", "updated", "state_changed", "object_json"]
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
@admin.action(description="Reprocess content for hashtags")
|
@admin.action(description="Reprocess content for hashtags")
|
||||||
def reparse_hashtags(self, request, queryset):
|
def reparse_hashtags(self, request, queryset):
|
||||||
for instance in queryset:
|
for instance in queryset:
|
||||||
|
|
Loading…
Reference in a new issue