ernestophone.ens.fr/partitions/views.py
Martin Pépin deee2168f9 Modification de la vue : liste des partitions
Elles sont triées par categories
2016-06-21 09:51:11 +02:00

137 lines
5.3 KiB
Python

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
import os
from partitions.forms import UploadFileForm, UploadMorceauForm
from partitions.models import Partition, PartitionSet
from partitions.decorators import chef_required
def liste(request):
partitions = PartitionSet.objects.order_by("nom")
context = {
"request": request,
"active_partitions": partitions.filter(category="old").all(),
"incoming_partitions": partitions.filter(category="incoming").all(),
"old_partitions": partitions.filter(category="old").all()
}
return render(request, 'partitions/liste.html', context)
@login_required
def upload(request, nom, auteur):
if request.method =="POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
partition = Partition()
partition.part = form.cleaned_data['file']
partition.nom = form.cleaned_data['title']
if '/' in partition.nom:
error = "Le caractère / n'est pas autorisé dans le nom"
form = UploadFileForm()
return render(request, "partitions/upload.html", locals())
mor = PartitionSet.objects.get(nom=nom, auteur=auteur)
partition.morceau = mor
try:
mor.partition_set.get(nom=partition.nom)
error="Un morceau du même nom existe déjà"
except Partition.DoesNotExist:
partition.save()
sauvegarde = True
else:
form = UploadFileForm()
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()
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()
return response
else:
partitions = PartitionSet.objects.get(nom=nom, auteur=auteur)
part = partitions.partition_set.all()
return render(request, 'partitions/listepart.html', locals())
@login_required
def download(request, nom, auteur, file_name):
abspath=open('/var/www_new/media/partitions/' + file_name, 'rb')
response = HttpResponse(content=abspath.read())
typ = file_name.split(".")
n = len(typ)
typ = typ[n-1]
response['Content-Type'] = 'application/' + typ
response['Content-Disposition'] = 'attachment; filename= %s ' % (nom +'-'+ auteur + '-' + smart_str(file_name))
abspath.close()
return response
@login_required
def ajouter_morceau(request):
if request.method == "POST":
form = UploadMorceauForm(request.POST)
if form.is_valid():
partitionset = PartitionSet()
partitionset.nom = form.cleaned_data['titre']
partitionset.auteur = form.cleaned_data['auteur']
if '/' in partitionset.auteur or '/' in partitionset.nom:
error = "Le caractère / n'est pas autorisé"
form = UploadMorceauForm()
return render(request, 'partitions/new.html', locals())
try:
PartitionSet.objects.get(nom=partitionset.nom, auteur=partitionset.auteur)
error = "Un morceau du même nom existe déjà"
except PartitionSet.DoesNotExist:
partitionset.save()
sauvegarde = True
else:
form = UploadMorceauForm()
return render(request, 'partitions/new.html', locals())
# Create your views here.
@chef_required
def delete(request, nom, auteur, id):
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
part = p.partition_set.get(id=id)
part.delete()
suppression = "Partition supprimée"
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
part = p.partition_set.all()
return render(request, 'partitions/listepart.html', locals())
@chef_required
def conf_delete(request, nom, auteur, id):
part = Partition.objects.get(id=id)
return render(request, 'partitions/conf_delete.html', locals())
@chef_required
def delete_morc(request, nom, auteur):
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
part = p.partition_set.all()
for pa in part:
pa.delete()
p.delete()
partitions = PartitionSet.objects.all()
return render(request, "partitions/liste.html", locals())
@chef_required
def conf_delete_morc(request, nom, auteur):
return render(request, 'partitions/conf_delete_morc.html', locals())
@login_required
def listepart(request, nom, auteur):
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
part = p.partition_set.all()
return render(request, 'partitions/listepart.html', locals())