Example #1
0
def download(request, downloadable_id, extra_context = None, **kwargs):
    """

    Arguments:
    - `request`:
    - `downloadable_id`:
    - `extra_context`:
    - `**kwargs`:
    """

    dnldbl = get_object_or_404(Downloadable, pk = downloadable_id)
    if not request.user.has_perm('downloads.download_downloadable', dnldbl) and \
                        request.user != dnldbl.owner:
        return HttpResponseForbidden(_("You do not have permission to "
                                       "download this."))

    # Track that this download was request.
    #
    kwargs = { 'who' : request.user, 'what' : dnldbl }
    if 'REMOTE_ADDR' in request.META:
        kwargs['ip_address'] = request.META['REMOTE_ADDR']
    if 'REMOTE_HOST' in request.META:
        kwargs['dns_name'] = request.META['REMOTE_HOST']

    download = Download(**kwargs)
    download.save()

    # And then send the content to the requester.
    #
    # return send_file(request, dnldbl.content.path,
    #                  content_type = dnldbl.mimetype, blksize = 65536)
    return send_file(request, dnldbl.content.path,
                     content_type = "application/octet-stream", blksize = 65536)