2016-06-22 14:09:41 +02:00
|
|
|
from django.shortcuts import render, redirect, HttpResponse, render_to_response,\
|
|
|
|
get_object_or_404
|
2015-03-17 19:03:51 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
|
|
|
|
import os
|
2015-03-29 17:50:31 +02:00
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
|
|
|
|
from partitions.forms import UploadFileForm, UploadMorceauForm
|
2016-06-22 14:09:41 +02:00
|
|
|
from partitions.models import Partition, PartitionSet, PARTITION_TYPES
|
2015-03-17 19:03:51 +01:00
|
|
|
from partitions.decorators import chef_required
|
|
|
|
|
|
|
|
def liste(request):
|
2016-06-21 09:51:11 +02:00
|
|
|
partitions = PartitionSet.objects.order_by("nom")
|
|
|
|
context = {
|
|
|
|
"request": request,
|
2016-06-22 14:09:41 +02:00
|
|
|
"active_partitions": partitions.filter(category="active").all(),
|
2016-06-21 09:51:11 +02:00
|
|
|
"incoming_partitions": partitions.filter(category="incoming").all(),
|
|
|
|
"old_partitions": partitions.filter(category="old").all()
|
|
|
|
}
|
|
|
|
return render(request, 'partitions/liste.html', context)
|
2015-03-17 19:03:51 +01:00
|
|
|
|
|
|
|
@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())
|
|
|
|
|
2015-03-29 17:50:31 +02:00
|
|
|
@login_required
|
|
|
|
def see(request, nom, auteur, file_name):
|
|
|
|
if ".pdf" in file_name:
|
2016-06-21 00:33:32 +02:00
|
|
|
abspath=open('/var/www_new/media/partitions/' + file_name, 'rb')
|
2015-03-29 17:50:31 +02:00
|
|
|
response=HttpResponse(content=abspath.read())
|
|
|
|
response["Content-Type"]="application/pdf"
|
|
|
|
response["Content-Disposition"]="inline; filename= %s" % (nom +'-'+ auteur +'-' + smart_str(file_name))
|
|
|
|
abspath.close()
|
|
|
|
return response
|
2015-03-29 23:26:03 +02:00
|
|
|
elif ".mp3" in file_name:
|
2016-06-21 00:33:32 +02:00
|
|
|
abspath=open('/var/www_new/media/partitions/' + file_name, 'rb')
|
2015-03-29 23:26:03 +02:00
|
|
|
response = HttpResponse()
|
|
|
|
response.write(abspath.read())
|
|
|
|
response["Content-Type"]="audio/mp3"
|
2016-06-21 00:33:32 +02:00
|
|
|
response["Content-Length"]= os.path.getsize('/var/www_new/media/partitions/' + file_name)
|
2015-03-29 23:26:03 +02:00
|
|
|
abspath.close()
|
|
|
|
return response
|
2015-03-29 17:50:31 +02:00
|
|
|
else:
|
|
|
|
partitions = PartitionSet.objects.get(nom=nom, auteur=auteur)
|
|
|
|
part = partitions.partition_set.all()
|
|
|
|
return render(request, 'partitions/listepart.html', locals())
|
2015-03-17 19:03:51 +01:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def download(request, nom, auteur, file_name):
|
2016-06-21 00:33:32 +02:00
|
|
|
abspath=open('/var/www_new/media/partitions/' + file_name, 'rb')
|
2015-03-17 19:03:51 +01:00
|
|
|
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))
|
2015-03-29 17:50:31 +02:00
|
|
|
abspath.close()
|
2015-03-17 19:03:51 +01:00
|
|
|
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()
|
|
|
|
return render(request, 'partitions/listepart.html', locals())
|
|
|
|
|
2016-06-22 14:09:41 +02:00
|
|
|
@chef_required
|
|
|
|
def change_category(request, nom, auteur, categorie):
|
|
|
|
part_set = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
|
|
|
|
part = part_set.partition_set.all()
|
|
|
|
context = {
|
|
|
|
'nom': nom,
|
|
|
|
'auteur': auteur,
|
|
|
|
'p': part_set,
|
|
|
|
'part': part,
|
|
|
|
}
|
|
|
|
if categorie in [id for (id, alias) in PARTITION_TYPES]:
|
|
|
|
part_set.category = categorie
|
|
|
|
part_set.save()
|
|
|
|
return render(request, 'partitions/listepart.html', context)
|
|
|
|
|
|
|
|
|