From 7d242674f30fbe697e02bff335152ecee8e17ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 25 Jun 2016 15:14:00 +0200 Subject: [PATCH] =?UTF-8?q?Retire=20des=20chemins=20hardcod=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dans la vue `partitions.views.see`, les chemins vers les fichiers ne sont plus hardcodés. --- partitions/urls.py | 2 +- partitions/views.py | 32 ++++++++++++++++------------- templates/partitions/listepart.html | 4 ++-- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/partitions/urls.py b/partitions/urls.py index 092c8d3..fe488fe 100644 --- a/partitions/urls.py +++ b/partitions/urls.py @@ -6,7 +6,7 @@ urlpatterns = patterns('partitions.views', url(r'^(?P[^/]+)/(?P[^/]+)/upload/?$', 'upload'), url(r'^(?P[^/]+)/(?P[^/]+)/delete/?$', 'delete_morc'), url(r'^(?P[^/]+)/(?P[^/]+)/conf/?$', 'conf_delete_morc'), - url(r'^(?P[^/]+)/(?P[^/]+)/see/(?P[^/]+)/?$', 'see'), + url(r'^(?P[^/]+)/(?P[^/]+)/see/(?P\d+)/?$', 'see'), url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/$', 'download'), url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/delete/?$', 'delete'), url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/conf/?$', 'conf_delete'), diff --git a/partitions/views.py b/partitions/views.py index a1950b7..8a0c0df 100644 --- a/partitions/views.py +++ b/partitions/views.py @@ -3,6 +3,7 @@ from django.shortcuts import render, redirect, HttpResponse, render_to_response, from django.contrib.auth.decorators import login_required from django.utils.encoding import smart_str from django.forms.models import modelform_factory +from django.core.files import File import os @@ -46,21 +47,24 @@ def upload(request, nom, auteur): return render(request, 'partitions/upload.html', locals()) @login_required -def see(request, nom, auteur, file_name): - if ".pdf" in file_name: - abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') - response=HttpResponse(content=abspath.read()) - response["Content-Type"]="application/pdf" - response["Content-Disposition"]="inline; filename= %s" % (nom +'-'+ auteur +'-' + smart_str(file_name)) - abspath.close() +def see(request, nom, auteur, partition_id): + partition = get_object_or_404(Partition, id=partition_id) + _, extension = os.path.splitext(partition.part.path) + if ".pdf" == extension: + with open(partition.part.path, 'rb') as f: + myfile = File(f) + response = HttpResponse(content=myfile.read()) + response["Content-Type"] = "application/pdf" + response["Content-Disposition"] = "inline; filename= %s-%s-%s" % ( + nom, auteur, smart_str(os.path.basename(myfile.name))) return response - elif ".mp3" in file_name: - abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') - response = HttpResponse() - response.write(abspath.read()) - response["Content-Type"]="audio/mp3" - response["Content-Length"]= os.path.getsize('/var/www_new/media/partitions/' + file_name) - abspath.close() + elif ".mp3" == extension: + with open(partition.part.path, 'rb') as f: + myfile = File(f) + response = HttpResponse() + response.write(myfile.read()) + response["Content-Type"] = "audio/mp3" + response["Content-Length"] = myfile.size return response else: partitions = PartitionSet.objects.get(nom=nom, auteur=auteur) diff --git a/templates/partitions/listepart.html b/templates/partitions/listepart.html index fdf0775..aa69d90 100644 --- a/templates/partitions/listepart.html +++ b/templates/partitions/listepart.html @@ -30,9 +30,9 @@
    {% for p in part %} {% if user.is_authenticated and ".pdf" in p.part.url %} -
  • {{ p.nom }} +
  • {{ p.nom }} {% elif user.is_authenticated and ".mp3" in p.part.url %} -
  • {{ p.nom }} +
  • {{ p.nom }} {% else %}
  • {{ p.nom }} {% endif %}