ernestophone.ens.fr/partitions/views.py
Martin Pépin 85a74fc0c9 Fixes
2016-07-14 00:39:05 +02:00

160 lines
5.9 KiB
Python

from django.shortcuts import render, HttpResponse, get_object_or_404
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
from django.utils.safestring import mark_safe
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="active").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, 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" == 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)
part = partitions.partition_set.all()
return render(request, 'partitions/listepart.html', locals())
@login_required
def download(request, nom, auteur, partition_id):
partition = get_object_or_404(Partition, id=partition_id)
with open(partition.part.path, 'rb') as f:
myfile = File(f)
response = HttpResponse(content=myfile.read())
typ = os.path.splitext(myfile.name)[1][1:]
response['Content-Type'] = 'application/%s' % (typ, )
response['Content-Disposition'] = 'attachment; filename=%s-%s-%s' % (
nom, auteur, smart_str(os.path.basename(myfile.name)))
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())
@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()
ChefEditForm = modelform_factory(PartitionSet,
fields=("category", "infos"))
if request.method == "POST" and request.user.profile.is_chef:
form = ChefEditForm(request.POST, instance=p)
if form.is_valid():
form.save()
else:
form = ChefEditForm(instance=p)
infos = mark_safe(p.infos)
return render(request, 'partitions/listepart.html', locals())