def download(request, pk, perm_callback=PERMS["download"]):
    attachment = get_object_or_404(Attachment, pk=pk)

    if not perm_callback(request, attachment):
        return HttpResponseForbidden("Forbidden attachment.")

    attachment.download_count = F("download_count") + 1
    attachment.save()
    return serve_file(request, attachment.file, attachment.name, attachment.mime_type)
def thumbnail(request, attachment_pk, width, height, perm_callback=PERMS["download"]):
    attachment = get_object_or_404(Attachment, pk=attachment_pk)

    if not perm_callback(request, attachment):
        return HttpResponseForbidden("Forbidden attachment.")

    thumb = Thumbnail.objects.get_or_create(attachment, width, height)
    if thumb is None:
        raise Http404("Thumbnail can not be created")
    return serve_file(request, thumb.file, None, thumb.mime_type)