jazzband / django-downloadview

Serve files with Django.
https://django-downloadview.readthedocs.io
Other
364 stars 58 forks source link

Signal/event once file has been downloaded? #200

Closed devidw closed 9 months ago

devidw commented 1 year ago

When applications want to indicate to users whether they have seen/downloaded a document or not, this would probably happen with the help of a boolean has_been_downloaded model field on the corresponding model.

To actually turn this field true once a document has been downloaded, it would be really helpful if the package could send a signal, offer a hook, that gets triggered, when a document was shipped successfully.

I guess this could be implemented with some custom code in a custom view that wraps one of the package views.

DRF example:

    @action(detail=True)
    def download(self, request, pk):
        document = self.get_object()

        # When the owner downloads the document, we want to update the
        # has_been_downloaded field correspondingly
        if document.has_been_downloaded is False:
            document.has_been_downloaded = True
            document.save()

        return ObjectDownloadView.as_view(
            model=Document,
        )(request, pk=pk)

But this would always happen before the actual download code runs and therefore, when the download somehow fails, data would wrongly be changed in the model.

sevdog commented 11 months ago

When the whole download process is handled by django you can use the request-finished.

However if the download is handled by the reverse proxy (Apache or Nginx or other) there is no way to handle it in django since the process is not handled by the application server.

devidw commented 9 months ago

I see, thanks @sevdog