ernestophone.ens.fr/partitions/views.py

203 lines
7.6 KiB
Python
Raw Normal View History

from django.shortcuts import render, HttpResponse, get_object_or_404, redirect, reverse
from partitions.models import Category, Partition, PartitionSet
2015-03-17 19:03:51 +01:00
from django.contrib.auth.decorators import login_required
from partitions.forms import UploadFileForm, UploadMorceauForm
from django.forms.models import modelform_factory
2016-07-14 00:39:05 +02:00
from django.utils.safestring import mark_safe
from django.utils.text import slugify
from django.core.files import File
from django.utils.encoding import smart_str
from django.http import Http404
from partitions.decorators import chef_required
from django.conf import settings
from django.db.models import Q
2015-03-17 19:03:51 +01:00
import os
import zipfile
import io
from django.utils.translation import gettext_lazy as _
2015-03-29 17:50:31 +02:00
def download_musecores(request):
p = Partition.objects.filter(Q(part__contains = ".mscz") & Q(Q(morceau__category__name = "Partitions actives" )|Q(morceau__category__name = "Partitions optionnelles" )))
2015-03-17 19:03:51 +01:00
2016-07-14 00:13:30 +02:00
zip_subdir = "Ernestophone_musescores"
zip_filename = "%s.zip" % zip_subdir
# Open StringIO to grab in-memory ZIP contents
s = io.BytesIO()
# The zip compressor
zf = zipfile.ZipFile(s, "w")
for part in p :
fpath = part.part.path
typ=".mscz"
# Calculate path for file in zip
fdir, fname = os.path.split(fpath)
zip_path = os.path.join(zip_subdir, '%s_%s_%s.%s' % (
slugify(part.morceau.nom), slugify(part.morceau.auteur), slugify(part.nom), typ))
# Add file, at correct path
zf.write(fpath, zip_path)
# Must close zip for all contents to be written
zf.close()
# Grab ZIP file from in-memory, make response with correct MIME-type
resp = HttpResponse(s.getvalue())
# ..and correct content-disposition
resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
return resp
2015-03-17 19:03:51 +01:00
def liste(request):
categories = Category.objects.prefetch_related("partitionset_set").order_by("order")
return render(request, 'partitions/repertoire.html', {"categories": categories})
2015-03-17 19:03:51 +01:00
@login_required
def listepart(request, nom, auteur):
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
part = p.partition_set.all().order_by('nom')
ChefEditForm = modelform_factory(PartitionSet,
fields=("category", "infos","url","infos_en"))
if request.method == "POST" and request.user.profile.is_chef:
form = ChefEditForm(request.POST, instance=p)
if form.is_valid():
form.save()
form = ChefEditForm(instance=p)
infos = mark_safe(p.infos)
infos_en = mark_safe(p.infos_en)
return render(request, 'partitions/part.html', locals())
2016-07-14 00:13:30 +02:00
2015-03-17 19:03:51 +01:00
@login_required
def upload(request, nom, auteur):
2016-07-14 00:13:30 +02:00
if request.method == "POST":
2015-03-17 19:03:51 +01:00
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")
2015-03-17 19:03:51 +01:00
form = UploadFileForm()
return render(request, "partitions/upload.html", locals())
mor = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
2015-03-17 19:03:51 +01:00
partition.morceau = mor
try:
mor.partition_set.get(nom=partition.nom)
error = _("Un morceau du même nom existe déjà")
2015-03-17 19:03:51 +01:00
except Partition.DoesNotExist:
partition.save()
sauvegarde = True
else:
2016-07-14 00:13:30 +02:00
form = UploadFileForm()
return render(request, 'partitions/upload.html', locals())
2015-03-29 17:50:31 +02:00
@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.pdf" % (
slugify(nom), slugify(auteur), slugify(partition.nom))
2015-03-29 17:50:31 +02:00
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
2015-03-29 23:26:03 +02:00
return response
2015-03-29 17:50:31 +02:00
else:
2018-01-05 11:10:06 +01:00
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
part = p.partition_set.all()
return render(request, 'partitions/part.html', locals())
@chef_required
def delete(request, nom, auteur, id):
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
try:
part = p.partition_set.get(id=id)
except Partition.DoesNotExist:
raise Http404
part.delete()
suppression = _("Partition supprimée")
p.refresh_from_db()
part = p.partition_set.all()
return redirect('partitions:listepart',nom=nom,auteur=auteur)
2016-07-14 00:13:30 +02:00
@chef_required
2015-03-17 19:03:51 +01:00
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é")
2015-03-17 19:03:51 +01:00
form = UploadMorceauForm()
return render(request, 'partitions/new.html', locals())
try:
2016-07-14 00:13:30 +02:00
PartitionSet.objects.get(nom=partitionset.nom,
auteur=partitionset.auteur)
error = _("Un morceau du même nom existe déjà")
2015-03-17 19:03:51 +01:00
except PartitionSet.DoesNotExist:
# XXX. Hideous
cat = Category.objects.first()
try:
cat = Category.objects.get(name="Partitions à venir")
except Category.DoesNotExist:
pass
partitionset.category = cat
2015-03-17 19:03:51 +01:00
partitionset.save()
sauvegarde = True
return redirect('partitions:liste')
2015-03-17 19:03:51 +01:00
else:
form = UploadMorceauForm()
return render(request, 'partitions/new.html', locals())
2015-03-17 19:03:51 +01:00
2016-07-14 00:13:30 +02:00
2015-03-17 19:03:51 +01:00
2016-07-14 00:13:30 +02:00
2015-03-17 19:03:51 +01:00
@chef_required
def conf_delete(request, nom, auteur, id):
part = get_object_or_404(Partition, id=id)
2015-03-17 19:03:51 +01:00
return render(request, 'partitions/conf_delete.html', locals())
@chef_required
def delete_morc(request, nom, auteur):
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
2015-03-17 19:03:51 +01:00
part = p.partition_set.all()
for pa in part:
pa.delete()
p.delete()
partitions = PartitionSet.objects.all()
categories = Category.objects.prefetch_related("partitionset_set")
return redirect('partitions:liste')
2015-03-17 19:03:51 +01:00
2016-07-14 00:13:30 +02:00
2015-03-17 19:03:51 +01:00
@chef_required
def conf_delete_morc(request, nom, auteur):
return render(request, 'partitions/conf_delete_morc.html', locals())
2016-07-14 00:13:30 +02:00
2015-03-17 19:03:51 +01:00
@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.%s' % (
slugify(nom), slugify(auteur), slugify(partition.nom), typ)
return response