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 all 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
65 changes: 51 additions & 14 deletions src/open_inwoner/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from django.forms import ValidationError
from django.http.request import HttpRequest
from django.urls import reverse, reverse_lazy
from django.utils.html import format_html
from django.utils.translation import ngettext, ugettext_lazy as _
Expand All @@ -15,6 +16,32 @@
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 get_fields(self, request, obj=None):
fields = super().get_fields(request, obj=obj)
if "display_file_url" not in fields and "file" in fields:
fields[fields.index("file")] = "display_file_url"
return fields

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 +168,9 @@ class _UserAdmin(ImageCroppingMixin, UserAdmin):


@admin.register(Action)
class ActionAdmin(UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin):
readonly_fields = ("uuid",)
class ActionAdmin(
ReadOnlyFileMixin, UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin
):
list_display = ("name", "status", "plan", "created_on", "created_by", "is_deleted")
list_filter = (
"is_deleted",
Expand All @@ -157,6 +185,12 @@ class ActionAdmin(UUIDAdminFirstInOrder, PrivateMediaMixin, admin.ModelAdmin):
"plan",
]

def has_add_permission(self, request):
return False

def has_change_permission(self, request, obj=None):
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 +221,18 @@ 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
):
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,
)
def has_add_permission(self, request):
return False

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


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


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

def has_add_permission(self, request):
return False

def has_change_permission(self, request, obj=None):
return False


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