111 lines
4 KiB
Python
111 lines
4 KiB
Python
|
from django.shortcuts import render, redirect, HttpResponse
|
||
|
from django.contrib.auth.decorators import login_required
|
||
|
from django.utils.encoding import smart_str
|
||
|
|
||
|
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.all().order_by("nom")
|
||
|
return render(request, 'partitions/liste.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 = 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:
|
||
|
sauvegarde = True
|
||
|
else:
|
||
|
form = UploadFileForm()
|
||
|
return render(request, 'partitions/upload.html', locals())
|
||
|
|
||
|
|
||
|
@login_required
|
||
|
def download(request, nom, auteur, file_name):
|
||
|
abspath=open('/home/rikm/Projets/Ernestophone/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))
|
||
|
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())
|
||
|
# Create your views here.
|
||
|
|
||
|
@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()
|
||
|
return render(request, 'partitions/listepart.html', locals())
|
||
|
|