from django.shortcuts import render, redirect, HttpResponse, render_to_response,\ 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 import os from partitions.forms import UploadFileForm, UploadMorceauForm from partitions.models import Partition, PartitionSet, PARTITION_TYPES 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, 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()) @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) return render(request, 'partitions/listepart.html', locals())