from django.shortcuts import render, HttpResponse, get_object_or_404, redirect, reverse from partitions.models import Category, Partition, PartitionSet from gestion.models import Photo from django.contrib.auth.decorators import login_required from partitions.forms import UploadFileForm, UploadMorceauForm from django.forms.models import modelform_factory 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 import os import zipfile import io from django.utils.translation import gettext_lazy as _ 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" ))) 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 def liste(request): categories = Category.objects.prefetch_related("partitionset_set").order_by("order") photo = Photo.objects.filter(cat='part').order_by('?').first() return render(request, 'partitions/repertoire.html', {"categories": categories,"photo":photo}) @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","download_unlogged", "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()) @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 = get_object_or_404(PartitionSet, 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()) def see(request, nom, auteur, partition_id): partition = get_object_or_404(Partition, id=partition_id) _, extension = os.path.splitext(partition.part.path) 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') @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) @chef_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: # XXX. Hideous cat = Category.objects.first() try: cat = Category.objects.get(name="Partitions à venir") except Category.DoesNotExist: pass partitionset.category = cat partitionset.save() sauvegarde = True return redirect('partitions:liste') else: form = UploadMorceauForm() return render(request, 'partitions/new.html', locals()) @chef_required def conf_delete(request, nom, auteur, id): part = get_object_or_404(Partition, id=id) 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) 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') @chef_required def conf_delete_morc(request, nom, auteur): return render(request, 'partitions/conf_delete_morc.html', locals()) def download(request, nom, auteur, partition_id): partition = get_object_or_404(Partition, id=partition_id) 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')