You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.2 KiB
57 lines
2.2 KiB
from django.contrib import admin
|
|
from django.utils.translation import gettext_lazy as _
|
|
from unfold.admin import ModelAdmin, TabularInline
|
|
from unfold.decorators import display
|
|
|
|
from apps.chat.models.chat import ChatRoom, ChatMessage
|
|
from utils.admin import project_admin_site
|
|
|
|
|
|
class ChatMessageInline(TabularInline):
|
|
model = ChatMessage
|
|
extra = 0
|
|
fields = ('sender', 'sender_institution', 'content', 'attachment', 'created_at')
|
|
readonly_fields = ('created_at',)
|
|
|
|
|
|
@admin.register(ChatRoom, site=project_admin_site)
|
|
class ChatRoomAdmin(ModelAdmin):
|
|
list_display = ('id', 'title_display', 'room_type_badge', 'messages_count', 'created_at', 'updated_at')
|
|
list_display_links = ('id', 'title_display')
|
|
list_filter = ('room_type', 'created_at')
|
|
search_fields = ('title', 'participant_users__email', 'participant_institutions__name')
|
|
filter_horizontal = ('participant_users', 'participant_institutions')
|
|
inlines = [ChatMessageInline]
|
|
readonly_fields = ('created_at', 'updated_at')
|
|
|
|
@display(description=_('Conversation'))
|
|
def title_display(self, obj):
|
|
return obj.title or f"Room #{obj.id}"
|
|
|
|
@display(description=_('Type'), label={
|
|
ChatRoom.RoomType.DIRECT: "info",
|
|
ChatRoom.RoomType.GROUP: "success",
|
|
})
|
|
def room_type_badge(self, obj):
|
|
return obj.get_room_type_display()
|
|
|
|
@display(description=_('Total Messages'))
|
|
def messages_count(self, obj):
|
|
return obj.messages.count()
|
|
|
|
|
|
@admin.register(ChatMessage, site=project_admin_site)
|
|
class ChatMessageAdmin(ModelAdmin):
|
|
list_display = ('id', 'room', 'sender', 'sender_institution', 'content_preview', 'has_attachment', 'created_at')
|
|
list_display_links = ('id', 'room', 'sender', 'content_preview')
|
|
list_filter = ('created_at', 'sender_institution')
|
|
search_fields = ('content', 'sender__email', 'sender__fullname', 'room__title')
|
|
readonly_fields = ('created_at',)
|
|
|
|
@display(description=_('Content'))
|
|
def content_preview(self, obj):
|
|
return obj.content[:40] if obj.content else "(Attachment)"
|
|
|
|
@display(description=_('Attachment'), boolean=True)
|
|
def has_attachment(self, obj):
|
|
return bool(obj.attachment)
|