Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1885] Changed document admin to be readonly #887

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 95 additions & 15 deletions src/open_inwoner/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@
from .models import Action, Appointment, Document, Invite, Message, User


class ReadOnlyFileMixin:
"""
By default, private media fields do not display the correct URL when readonly
"""

def display_file_url(self, obj):
view_name = "%(app_label)s_%(model_name)s_%(field)s" % {
"app_label": self.opts.app_label,
"model_name": self.opts.model_name,
"field": "file",
}
return format_html(
_("<a href='{url}'>{text}</a>"),
url=reverse(f"admin:{view_name}", kwargs={"pk": obj.pk}),
text=obj.file.name,
)

display_file_url.short_description = _("File")


class ActionInlineAdmin(UUIDAdminFirstInOrder, admin.StackedInline):
model = Action
extra = 1
Expand Down Expand Up @@ -141,8 +161,39 @@ class _UserAdmin(ImageCroppingMixin, UserAdmin):


@admin.register(Action)
class ActionAdmin(UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin):
readonly_fields = ("uuid",)
class ActionAdmin(
ReadOnlyFileMixin, UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin
):
fields = [
"uuid",
"name",
"description",
"status",
"type",
"end_date",
"display_file_url",
"is_for",
"created_on",
"updated_on",
"created_by",
"plan",
"is_deleted",
]
readonly_fields = (
"uuid",
"name",
"description",
"status",
"type",
"end_date",
"display_file_url",
"is_for",
"created_on",
"updated_on",
"created_by",
"plan",
"is_deleted",
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not very DRY to repeat the same list? You could assign then same:

readonly_fields = fields

Also even more robust and less manual configuration to use has_change_permission() instead of explicitly listing everything. For example, if a new field gets added we need to remember it has to be added here (twice!) instead of being readonly by default. It is also more correct to make the model as a whole readonly instead of manually marking all fields.

This goes for all the changes that do similar.

list_display = ("name", "status", "plan", "created_on", "created_by", "is_deleted")
list_filter = (
"is_deleted",
Expand All @@ -157,6 +208,9 @@ class ActionAdmin(UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin):
"plan",
]

def has_add_permission(self, request):
return False

@admin.action(description=_("Mark selected actions as soft-deleted by user."))
def mark_deleted(self, request, queryset):
updated = queryset.update(is_deleted=True)
Expand Down Expand Up @@ -187,20 +241,24 @@ def mark_not_deleted(self, request, queryset):


@admin.register(Document)
class DocumentAdmin(UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin):
readonly_fields = ("uuid",)
list_display = ("name", "preview", "created_on", "owner")
class DocumentAdmin(
ReadOnlyFileMixin, UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin
):
fields = ["uuid", "name", "display_file_url", "created_on", "plan", "owner"]
readonly_fields = (
"uuid",
"name",
"display_file_url",
"plan",
"created_on",
"owner",
)
list_display = ("name", "display_file_url", "created_on", "owner")
list_filter = ("owner",)
private_media_fields = ("file",)

def preview(self, obj):
return format_html(
_("<a href='{url}'>{text}</a>"),
url=reverse("admin:accounts_document_file", kwargs={"pk": obj.pk}),
text=obj.file.name,
)

preview.short_description = "Preview file"
def has_add_permission(self, request):
return False


@admin.register(Appointment)
Expand All @@ -211,12 +269,34 @@ class AppointmentAdmin(UUIDAdminFirstInOrder, admin.ModelAdmin):


@admin.register(Message)
class MessageAdmin(PrivateMediaMixin, admin.ModelAdmin):
readonly_fields = ("uuid",)
class MessageAdmin(ReadOnlyFileMixin, PrivateMediaMixin, admin.ModelAdmin):
fields = (
"uuid",
"sender",
"receiver",
"created_on",
"content",
"seen",
"sent",
"display_file_url",
)
readonly_fields = (
"uuid",
"sender",
"receiver",
"created_on",
"content",
"seen",
"sent",
"display_file_url",
)
list_display = ("sender", "receiver", "created_on", "seen", "sent")
list_filter = ("sender", "receiver")
private_media_fields = ("file",)

def has_add_permission(self, request):
return False


@admin.register(Invite)
class InviteAdmin(admin.ModelAdmin):
Expand Down
Loading