2020-09-10 15:06:53 +02:00
|
|
|
from django.shortcuts import render, HttpResponse, get_object_or_404, redirect, reverse
|
|
|
|
from partitions.models import Category, Partition, PartitionSet
|
2021-03-31 16:05:26 +02:00
|
|
|
from gestion.models import Photo
|
2015-03-17 19:03:51 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2020-09-10 15:06:53 +02:00
|
|
|
from partitions.forms import UploadFileForm, UploadMorceauForm
|
2016-06-22 15:33:48 +02:00
|
|
|
from django.forms.models import modelform_factory
|
2016-07-14 00:39:05 +02:00
|
|
|
from django.utils.safestring import mark_safe
|
2020-09-10 15:06:53 +02:00
|
|
|
from django.utils.text import slugify
|
|
|
|
from django.core.files import File
|
|
|
|
from django.utils.encoding import smart_str
|
2016-09-24 00:55:01 +02:00
|
|
|
from django.http import Http404
|
2020-09-10 15:06:53 +02:00
|
|
|
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
|
2020-09-10 15:06:53 +02:00
|
|
|
import zipfile
|
|
|
|
import io
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-03-29 17:50:31 +02:00
|
|
|
|
2020-09-10 15:06:53 +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
|
|
|
|
2020-09-10 15:06:53 +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):
|
2020-09-10 15:06:53 +02:00
|
|
|
categories = Category.objects.prefetch_related("partitionset_set").order_by("order")
|
2021-03-31 16:05:26 +02:00
|
|
|
photo = Photo.objects.filter(cat='part').order_by('?').first()
|
|
|
|
return render(request, 'partitions/repertoire.html', {"categories": categories,"photo":photo})
|
2015-03-17 19:03:51 +01:00
|
|
|
|
2020-09-10 15:06:53 +02: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,
|
2021-03-31 16:05:26 +02:00
|
|
|
fields=("category","download_unlogged", "infos","url","infos_en"))
|
2020-09-10 15:06:53 +02:00
|
|
|
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:
|
2020-09-10 15:06:53 +02:00
|
|
|
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())
|
2016-09-24 00:55:01 +02:00
|
|
|
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)
|
2020-09-10 15:06:53 +02:00
|
|
|
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())
|
|
|
|
|
2021-03-31 16:05:26 +02:00
|
|
|
|
2016-06-25 15:14:00 +02:00
|
|
|
def see(request, nom, auteur, partition_id):
|
|
|
|
partition = get_object_or_404(Partition, id=partition_id)
|
|
|
|
_, extension = os.path.splitext(partition.part.path)
|
2021-03-31 16:05:26 +02:00
|
|
|
download_unlogged = partition.morceau.download_unlogged
|
|
|
|
if(download_unlogged == 'o' or request.user.is_authenticated):
|
|
|
|
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))
|
|
|
|
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:
|
|
|
|
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
|
|
|
|
part = p.partition_set.all()
|
|
|
|
return render(request, 'partitions/part.html', locals())
|
|
|
|
else :
|
|
|
|
return redirect('login')
|
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
@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
|
|
|
|
2020-09-10 15:06:53 +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:
|
2020-09-10 15:06:53 +02:00
|
|
|
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)
|
2020-09-10 15:06:53 +02:00
|
|
|
error = _("Un morceau du même nom existe déjà")
|
2015-03-17 19:03:51 +01:00
|
|
|
except PartitionSet.DoesNotExist:
|
2018-01-04 22:11:27 +01:00
|
|
|
# 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
|
2020-09-10 15:06:53 +02:00
|
|
|
return redirect('partitions:liste')
|
2015-03-17 19:03:51 +01:00
|
|
|
else:
|
|
|
|
form = UploadMorceauForm()
|
2020-09-10 15:06:53 +02:00
|
|
|
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):
|
2016-09-24 00:55:01 +02:00
|
|
|
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):
|
2016-09-24 00:55:01 +02:00
|
|
|
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()
|
2020-09-10 15:06:53 +02:00
|
|
|
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
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
|
2021-03-31 16:05:26 +02:00
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
def download(request, nom, auteur, partition_id):
|
2021-03-31 16:05:26 +02:00
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
partition = get_object_or_404(Partition, id=partition_id)
|
2021-03-31 16:05:26 +02:00
|
|
|
download_unlogged = partition.morceau.download_unlogged
|
|
|
|
if(download_unlogged == 'o' or request.user.is_authenticated):
|
|
|
|
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
|
|
|
|
else :
|
|
|
|
return redirect('login')
|