|
| 1 | +from django.utils.translation import gettext_lazy as _ |
| 2 | + |
| 3 | +from wagtail.admin.ui.tables import Column |
| 4 | +from wagtail.admin.views.generic.chooser import ( |
| 5 | + ChooseResultsView, |
| 6 | + ChooseView, |
| 7 | + ChosenView, |
| 8 | +) |
| 9 | +from wagtail.admin.viewsets.chooser import ChooserViewSet |
| 10 | + |
| 11 | +from ..forms import get_media_form |
| 12 | +from ..models import get_media_model |
| 13 | + |
| 14 | + |
| 15 | +Media = get_media_model() |
| 16 | + |
| 17 | + |
| 18 | +class MediaChooserMixin: |
| 19 | + def get_object_list(self): |
| 20 | + return Media.objects.select_related("collection").only( |
| 21 | + "title", "file", "type", "collection__name", "created_at" |
| 22 | + ) |
| 23 | + |
| 24 | + @property |
| 25 | + def columns(self): |
| 26 | + return super().columns + [ |
| 27 | + Column("file", label="File", accessor="file"), |
| 28 | + Column("type", label="Type", accessor="type"), |
| 29 | + Column("collection", label="Collection", accessor="collection"), |
| 30 | + Column("created_at", label="Uploaded", accessor="created_at"), |
| 31 | + ] |
| 32 | + |
| 33 | + |
| 34 | +class MediaChooseView(MediaChooserMixin, ChooseView): |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +class MediaChooseResultsView(MediaChooserMixin, ChooseResultsView): |
| 39 | + pass |
| 40 | + |
| 41 | + |
| 42 | +class MediaChosenView(ChosenView): |
| 43 | + def get_object(self, pk): |
| 44 | + return Media.objects.only("title", "thumbnail").get(pk=pk) |
| 45 | + |
| 46 | + |
| 47 | +class MediaChooserViewSet(ChooserViewSet): |
| 48 | + choose_view_class = MediaChooseView |
| 49 | + chosen_view_class = MediaChosenView |
| 50 | + choose_results_view_class = MediaChooseResultsView |
| 51 | + creation_form_class = get_media_form(Media) |
| 52 | + |
| 53 | + icon = "media" |
| 54 | + choose_one_text = _("Choose a media item") |
| 55 | + choose_another_text = _("Choose another media item") |
| 56 | + edit_item_text = _("Edit this media item") |
| 57 | + creation_tab_label = _("Upload") |
| 58 | + create_action_label = _("Upload") |
| 59 | + create_action_clicked_label = _("Uploading…") |
| 60 | + form_fields = ["type", "title", "file", "collection", "thumbnail"] |
| 61 | + |
| 62 | + |
| 63 | +media_chooser_viewset = MediaChooserViewSet("media_chooser", model=Media) |
0 commit comments