Download link in dashboard widget

Questions, comments, discussions. Over time certain topics might be moved to their own category.
Post Reply
lsmoker
Posts: 37
Joined: Wed Sep 05, 2018 3:52 pm

Download link in dashboard widget

Post by lsmoker »

I have hacked a way to add a document download link to a dashboard widget (Recently accessed, for instance), but I'm sure there is a better way to do it...

I've added this to the my_overrides/apps.py file:

Code: Select all

        SourceColumn(
            func=lambda context: mark_safe(
                '<a href=' + reverse(
                    viewname='documents:document_file_download',
                    kwargs={'document_file_id': context['object'].files.last().pk}
                ) + '>Download</a>'
            ), name='download_link', source=Document
        )
Downsides:
- no checking of the document download permission
- the URL in the browser address bar changes to the download link and doesn't change back to the home URL

Is there a cleaner way to do this?
---
LeVon Smoker
User avatar
michael
Developer
Developer
Posts: 286
Joined: Sun Apr 19, 2020 6:21 am

Re: Download link in dashboard widget

Post by michael »

This would be how I would implemented such a feature, with the disclaimer that this is not the intended use for the SourceColumn service.

Code: Select all

# html_widgets.py

from django.apps import apps
from django.core.exceptions import PermissionDenied
from django.urls import reverse

from mayan.apps.documents.permissions import permission_document_file_download
from mayan.apps.views.html_widgets import ObjectLinkWidget


class DocumentDownloadSourceWidget(SourceColumnWidget):
    template_string = '<a target="_blank" href="{{ url }}">{{ label }}</a>'

    def get_extra_context(self):
        AccessControlList = apps.get_model(
            app_label='acls', model_name='AccessControlList'
        )

        document_file = self.value.file_latest

        try:
            AccessControlList.objects.check_access(
                obj=document_file,
                permissions=(permission_document_file_download,),
                user=self.request.user
            )
        except PermissionDenied:
            url = '#'
        else:
            url = reverse(
                viewname='documents:document_file_download',
                kwargs={'document_file_id': document_file.pk}
            )

        return {
            'label': 'Download', 'url': url
        }

# apps.py
        from .html_widgets import DocumentDownloadSourceWidget
        SourceColumn(
            label=_('Download'), source=Document,
            widget=DocumentDownloadSourceWidget
        )

Code: Select all

target="_blank" 
causes a new tab to open to handle the download. The URL in the main tab will still change, but will not trigger a navigation action.

Code: Select all

Document.file_latest
guarantees the latest file and not just the last in the queryset obtained using

Code: Select all

Document.files.last()
lsmoker
Posts: 37
Joined: Wed Sep 05, 2018 3:52 pm

Re: Download link in dashboard widget

Post by lsmoker »

Thanks!
---
LeVon Smoker
Post Reply