2020-01-21 06:35:58 +00:00
|
|
|
from django.forms import ModelForm
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.utils.translation import gettext_lazy as _, ungettext
|
|
|
|
from reversion.admin import VersionAdmin
|
|
|
|
|
|
|
|
from judge.models import Comment
|
|
|
|
from judge.widgets import AdminHeavySelect2Widget, HeavyPreviewAdminPageDownWidget
|
|
|
|
|
|
|
|
|
|
|
|
class CommentForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
widgets = {
|
2022-05-14 17:57:27 +00:00
|
|
|
"author": AdminHeavySelect2Widget(data_view="profile_select2"),
|
|
|
|
"parent": AdminHeavySelect2Widget(data_view="comment_select2"),
|
2020-01-21 06:35:58 +00:00
|
|
|
}
|
|
|
|
if HeavyPreviewAdminPageDownWidget is not None:
|
2022-05-14 17:57:27 +00:00
|
|
|
widgets["body"] = HeavyPreviewAdminPageDownWidget(
|
|
|
|
preview=reverse_lazy("comment_preview")
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CommentAdmin(VersionAdmin):
|
|
|
|
fieldsets = (
|
2022-05-14 17:57:27 +00:00
|
|
|
(None, {"fields": ("author", "page", "parent", "score", "hidden")}),
|
|
|
|
("Content", {"fields": ("body",)}),
|
2020-01-21 06:35:58 +00:00
|
|
|
)
|
2022-05-14 17:57:27 +00:00
|
|
|
list_display = ["author", "linked_page", "time"]
|
|
|
|
search_fields = ["author__user__username", "page", "body"]
|
|
|
|
readonly_fields = ["score"]
|
|
|
|
actions = ["hide_comment", "unhide_comment"]
|
|
|
|
list_filter = ["hidden"]
|
2020-01-21 06:35:58 +00:00
|
|
|
actions_on_top = True
|
|
|
|
actions_on_bottom = True
|
|
|
|
form = CommentForm
|
2022-05-14 17:57:27 +00:00
|
|
|
date_hierarchy = "time"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_queryset(self, request):
|
2022-05-14 17:57:27 +00:00
|
|
|
return Comment.objects.order_by("-time")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def hide_comment(self, request, queryset):
|
|
|
|
count = queryset.update(hidden=True)
|
2022-05-14 17:57:27 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ungettext(
|
|
|
|
"%d comment successfully hidden.",
|
|
|
|
"%d comments successfully hidden.",
|
|
|
|
count,
|
|
|
|
)
|
|
|
|
% count,
|
|
|
|
)
|
|
|
|
|
|
|
|
hide_comment.short_description = _("Hide comments")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def unhide_comment(self, request, queryset):
|
|
|
|
count = queryset.update(hidden=False)
|
2022-05-14 17:57:27 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ungettext(
|
|
|
|
"%d comment successfully unhidden.",
|
|
|
|
"%d comments successfully unhidden.",
|
|
|
|
count,
|
|
|
|
)
|
|
|
|
% count,
|
|
|
|
)
|
|
|
|
|
|
|
|
unhide_comment.short_description = _("Unhide comments")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def linked_page(self, obj):
|
|
|
|
link = obj.link
|
|
|
|
if link is not None:
|
|
|
|
return format_html('<a href="{0}">{1}</a>', link, obj.page)
|
|
|
|
else:
|
2022-05-14 17:57:27 +00:00
|
|
|
return format_html("{0}", obj.page)
|
|
|
|
|
|
|
|
linked_page.short_description = _("Associated page")
|
|
|
|
linked_page.admin_order_field = "page"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
super(CommentAdmin, self).save_model(request, obj, form, change)
|
|
|
|
if obj.hidden:
|
|
|
|
obj.get_descendants().update(hidden=obj.hidden)
|