def document_download(request, docid): document = get_object_or_404(Document, pk=docid) if request.user.is_authenticated(): action.send(request.user, verb='downloaded', action_object=document) DownloadTracker( actor=Profile.objects.get(username=request.user), title=str(document.title), resource_type=str( ResourceBase.objects.get(document__id=docid).csw_type), keywords=Document.objects.get(id=docid).keywords.slugs(), dl_type="document").save() print request.user.has_perm('base.download_resourcebase', obj=document.get_self_resource()) if not request.user.has_perm('base.download_resourcebase', obj=document.get_self_resource()): return HttpResponse(loader.render_to_string( '401.html', RequestContext(request, { 'error_message': _("You are not allowed to view this document.") })), status=401) return DownloadResponse(document.doc_file)
def get_download_response(request, docid, attachment=False): """ Returns a download response if user has access to download the document of a given id, and an http response if they have no permissions to download it. """ document = get_object_or_404(Document, pk=docid) if not request.user.has_perm('base.download_resourcebase', obj=document.get_self_resource()): return HttpResponse(loader.render_to_string( '401.html', context={ 'error_message': _("You are not allowed to view this document.") }, request=request), status=401) if attachment: register_event(request, EventType.EVENT_DOWNLOAD, document) filename = slugify(os.path.splitext(os.path.basename(document.title))[0]) if document.files and storage_manager.exists(document.files[0]): return DownloadResponse(storage_manager.open(document.files[0]).file, basename=f'{filename}.{document.extension}', attachment=attachment) return HttpResponse("File is not available", status=404)
def test_content_disposition_encoding(self): """Content-Disposition header is encoded.""" response = DownloadResponse( "fake file", attachment=True, basename="espacé .txt", ) headers = response.default_headers self.assertIn('filename="espace_.txt"', headers["Content-Disposition"]) self.assertIn( "filename*=UTF-8''espac%C3%A9%20.txt", headers["Content-Disposition"] )
def document_download(request, docid): document = get_object_or_404(Document, pk=docid) if not request.user.has_perm('documents.view_document', obj=document): return HttpResponse(loader.render_to_string( '401.html', RequestContext(request, { 'error_message': _("You are not allowed to view this document.") })), status=401) return DownloadResponse(document.doc_file)
def document_download(request, docid): document = get_object_or_404(Document, pk=docid) if not request.user.has_perm( 'base.download_resourcebase', obj=document.get_self_resource()): return HttpResponse( loader.render_to_string( '401.html', context={ 'error_message': _("You are not allowed to view this document.")}, request=request), status=401) register_event(request, EventType.EVENT_DOWNLOAD, document) return DownloadResponse(document.doc_file)
def document_download(request, slug): document = get_object_or_404(WFPDocument, slug=slug) if not request.user.has_perm('base.download_resourcebase', obj=document.get_self_resource()): return HttpResponse(loader.render_to_string( '401.html', render(request, { 'error_message': _("You are not allowed to view this document.") })), status=401) return DownloadResponse(document.doc_file)
def document_download(request, docid): document = get_object_or_404(Document, pk=docid) if settings.MONITORING_ENABLED and document: if hasattr(document, 'alternate'): request.add_resource('document', document.alternate) if not request.user.has_perm( 'base.download_resourcebase', obj=document.get_self_resource()): return HttpResponse( loader.render_to_string( '401.html', context={ 'error_message': _("You are not allowed to view this document.")}, request=request), status=401) return DownloadResponse(document.doc_file)
def video_download(request, vidid): video = get_object_or_404(Video, pk=vidid) if settings.MONITORING_ENABLED and video: if hasattr(video, 'alternate'): request.add_resource('video', video.alternate) if not request.user.has_perm('base.download_resourcebase', obj=video.get_self_resource()): return HttpResponse(loader.render_to_string( '401.html', context={ 'error_message': _("You are not allowed to view this video.") }, request=request), status=401) return DownloadResponse(video.vid_file)
def document_detail(request, docid): """ The view that show details of each document """ document = None try: document = _resolve_document(request, docid, 'base.view_resourcebase', _PERMISSION_MSG_VIEW) except Http404: return HttpResponse(loader.render_to_string( '404.html', RequestContext(request, {})), status=404) except PermissionDenied: return HttpResponse(loader.render_to_string( '401.html', RequestContext(request, { 'error_message': _("You are not allowed to view this document.") })), status=403) if document is None: return HttpResponse('An unknown error has occured.', mimetype="text/plain", status=401) else: try: related = document.content_type.get_object_for_this_type( id=document.object_id) except: related = '' # Update count for popularity ranking, # but do not includes admins or resource owners if request.user != document.owner and not request.user.is_superuser: Document.objects.filter(id=document.id).update( popular_count=F('popular_count') + 1) metadata = document.link_set.metadata().filter( name__in=settings.DOWNLOAD_FORMATS_METADATA) context_dict = { 'perms_list': get_perms(request.user, document.get_self_resource()), 'permissions_json': _perms_info_json(document), 'resource': document, 'metadata': metadata, 'imgtypes': IMGTYPES, 'related': related } if settings.SOCIAL_ORIGINS: context_dict["social_links"] = build_social_links( request, document) if request.method == 'POST': form = AnonDownloaderForm(request.POST) out = {} if form.is_valid(): out['success'] = True pprint(form.cleaned_data) anondownload = form.save() # anondownload.anon_document = Document.objects.get(id = docid) anondownload.anon_document = Document.objects.get( pk=docid).title anondownload.save() else: errormsgs = [] for e in form.errors.values(): errormsgs.extend([escape(v) for v in e]) out['success'] = False out['errors'] = form.errors out['errormsgs'] = errormsgs if out['success']: status_code = 200 document = get_object_or_404(Document, pk=docid) return DownloadResponse(document.doc_file) else: status_code = 400 # return HttpResponse(status=status_code) #Handle form # return HttpResponse(status=status_code) # url = reverse('document_download',kwargs={'docid': docid}) # return HttpResponseRedirect(url) else: #Render form form = AnonDownloaderForm() context_dict["anon_form"] = form return render_to_response("documents/document_detail.html", RequestContext(request, context_dict))