django-downloadview
django-downloadview copied to clipboard
Signal/event once file has been downloaded?
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.