Retire des chemins hardcodés

Dans la vue `partitions.views.see`, les chemins vers les fichiers
ne sont plus hardcodés.
This commit is contained in:
Martin Pépin 2016-06-25 15:14:00 +02:00
parent 8a08d92210
commit 7d242674f3
3 changed files with 21 additions and 17 deletions

View file

@ -6,7 +6,7 @@ urlpatterns = patterns('partitions.views',
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/upload/?$', 'upload'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/upload/?$', 'upload'),
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/delete/?$', 'delete_morc'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/delete/?$', 'delete_morc'),
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/conf/?$', 'conf_delete_morc'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/conf/?$', 'conf_delete_morc'),
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/see/(?P<file_name>[^/]+)/?$', 'see'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/see/(?P<partition_id>\d+)/?$', 'see'),
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/(?P<file_name>[^/]+)/$', 'download'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/(?P<file_name>[^/]+)/$', 'download'),
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/(?P<id>[^/]+)/delete/?$', 'delete'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/(?P<id>[^/]+)/delete/?$', 'delete'),
url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/(?P<id>[^/]+)/conf/?$', 'conf_delete'), url(r'^(?P<nom>[^/]+)/(?P<auteur>[^/]+)/(?P<id>[^/]+)/conf/?$', 'conf_delete'),

View file

@ -3,6 +3,7 @@ from django.shortcuts import render, redirect, HttpResponse, render_to_response,
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from django.forms.models import modelform_factory from django.forms.models import modelform_factory
from django.core.files import File
import os import os
@ -46,21 +47,24 @@ def upload(request, nom, auteur):
return render(request, 'partitions/upload.html', locals()) return render(request, 'partitions/upload.html', locals())
@login_required @login_required
def see(request, nom, auteur, file_name): def see(request, nom, auteur, partition_id):
if ".pdf" in file_name: partition = get_object_or_404(Partition, id=partition_id)
abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') _, extension = os.path.splitext(partition.part.path)
response=HttpResponse(content=abspath.read()) if ".pdf" == extension:
response["Content-Type"]="application/pdf" with open(partition.part.path, 'rb') as f:
response["Content-Disposition"]="inline; filename= %s" % (nom +'-'+ auteur +'-' + smart_str(file_name)) myfile = File(f)
abspath.close() 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 return response
elif ".mp3" in file_name: elif ".mp3" == extension:
abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') with open(partition.part.path, 'rb') as f:
response = HttpResponse() myfile = File(f)
response.write(abspath.read()) response = HttpResponse()
response["Content-Type"]="audio/mp3" response.write(myfile.read())
response["Content-Length"]= os.path.getsize('/var/www_new/media/partitions/' + file_name) response["Content-Type"] = "audio/mp3"
abspath.close() response["Content-Length"] = myfile.size
return response return response
else: else:
partitions = PartitionSet.objects.get(nom=nom, auteur=auteur) partitions = PartitionSet.objects.get(nom=nom, auteur=auteur)

View file

@ -30,9 +30,9 @@
<ul class="filelist"> <ul class="filelist">
{% for p in part %} {% for p in part %}
{% if user.is_authenticated and ".pdf" in p.part.url %} {% if user.is_authenticated and ".pdf" in p.part.url %}
<li><a href="{% url "partitions.views.see" nom auteur p.part.url|cuturl %}" class="fichier">{{ p.nom }}</a> <li><a href="{% url "partitions.views.see" nom auteur p.id %}" class="fichier">{{ p.nom }}</a>
{% elif user.is_authenticated and ".mp3" in p.part.url %} {% elif user.is_authenticated and ".mp3" in p.part.url %}
<li><a href="{% url "partitions.views.see" nom auteur p.part.url|cuturl %}" class="fichier">{{ p.nom }}</a> <li><a href="{% url "partitions.views.see" nom auteur p.id %}" class="fichier">{{ p.nom }}</a>
{% else %} {% else %}
<li>{{ p.nom }} <li>{{ p.nom }}
{% endif %} {% endif %}