0

I'm writing a custom StreamBlock that is designed to render a table based on the data in a given CSV file. The CSV files will be stored in the usual Document store, so the block needs to present the Document chooser to let the user pick a CSV file.

I would like to restrict this block's Document chooser to documents whose filenames end in .csv, without affecting any other Document chooser. I dug into the code to try to figure out a way to do that, but after 30 minutes of poking around, it doesn't look like there's any obvious way to determine the source of a request to the admin/documents/chooser/ view, which is what actually renders the Document listing.

My best guess at the moment for how to do this would be to subclass DocumentChooserBlock and AdminDocumentChooser, so that I can tell it to use a custom version of wagtaildocs/js/document-chooser-modal.js. I'd change it to send an extra GET argument, or something like that, whenever CSVTableBlock's Document chooser sends a request to admin/documents/chooser/. Then I could implement construct_document_chooser_queryset to look for that custom GET arg, so it knows when to filter the documents queryset to only .csv files.

But I'm really not at all sure that this is the right way to go about it, or if it's even possible for that strategy to work. Is there a better/possible way to do this?

coredumperror
  • 8,471
  • 6
  • 33
  • 42
  • 1
    Does this answer your question? [Limiting file types for a specific DocumentChooserBlock() Block in Wagtail Steamfield](https://stackoverflow.com/questions/69042417/limiting-file-types-for-a-specific-documentchooserblock-block-in-wagtail-steam) - specifically the answer here https://stackoverflow.com/a/69060326/8070948 – LB Ben Johnston Oct 03 '21 at 21:23
  • That's super cool! Thanks @LBBenJohnston! – coredumperror Oct 04 '21 at 18:25

1 Answers1

0

you actually no need to do anything, almost all use cases for such filtering scenarios, an editor can input the ".csv" in the search box of the chooser modal

Distroyer
  • 11
  • 6
  • Well yes, that's obvious. I just want that filtering to be done automatically, so the users don't have to worry about doing it themselves. – coredumperror Oct 02 '21 at 21:18
  • what about you try to change the requested url to include the arg (?q=.csv&collection_id=), in a modified/customized modal template file or it's caller??? – Distroyer Oct 03 '21 at 09:35
  • That's essentially a more streamlined version of what I proposed in my third paragraph. I was hoping to find something more elegant than having to override a bunch of classes to load custom javascript. But that *is* probably the best way to do this. – coredumperror Oct 03 '21 at 16:00