blechreiz-website/scoremanager/views.py

155 lines
5.0 KiB
Python
Raw Permalink Normal View History

2014-02-15 18:07:00 +01:00
from django.shortcuts import render
from django.http import HttpResponse
from django import forms
from django.conf import settings
from django.utils.translation import ugettext as _
from django.core.exceptions import PermissionDenied
from scoremanager.models import Piece, Score, Recording, ScoreUserMapping, YoutubeRecording
import json
import os
def manage_repertoire( request ):
context = {}
context['repertoire'] = Piece.getRepertoire()
context['allPieces' ] = Piece.objects.all().order_by( 'title' )
return render ( request, 'scoremanager/manage_repertoire.html', context )
def manage_repertoire_ajax_save( request ):
if request.is_ajax():
if request.method == 'POST':
result = json.loads( request.body )
parsedResult = { int(key): value for ( key,value ) in result.items() }
Piece.objects.all().update( repertoire_nr = None)
for piece in Piece.objects.all():
if piece.pk in parsedResult:
piece.repertoire_nr = parsedResult[piece.pk]
piece.save()
return HttpResponse("OK")
def list_repertoire( request ):
context = { 'repertoire': Piece.getRepertoire() }
return render( request, 'scoremanager/list_repertoire.html' , context )
# ----------------------------- Piece View + Ajax views + Forms ------------------------------------------
def score_user_mapping_save( request ):
if request.is_ajax():
if request.method == 'POST':
result = json.loads( request.body )
print ( "Result " + str( result ) )
print ( "Resulting score: " + str( result['myScore'] ) )
result = int(result['myScore'])
ScoreUserMapping.add_user_score_mapping( user=request.user, score= Score.objects.get(pk=result) )
return HttpResponse("OK")
def score( request, pk ):
requestedScore = Score.objects.get(pk=pk)
if request.method == 'GET':
imageFile = requestedScore.get_image_file()
image_data = open( settings.MEDIA_ROOT + imageFile, "rb").read()
return HttpResponse(image_data, content_type="image/jpeg")
2014-02-15 18:07:00 +01:00
if request.method == "DELETE":
if requestedScore.uploaded_by != request.user or request.user.has_perm('scoremanager.manage_scores'):
raise PermissionDenied
requestedScore.delete()
return HttpResponse("OK")
def youtube_link( request ):
result = json.loads( request.body )
print("Youtube link: " + str(result) )
if request.method == 'DELETE':
linkId = int( result['linkid'] )
youtubeRecording = YoutubeRecording.objects.get( pk = linkId )
if youtubeRecording.uploaded_by == request.user or request.user.has_perm('scoremanager.manage_scores'):
youtubeRecording.delete()
else:
raise PermissionDenied
return HttpResponse("OK")
elif request.method == 'POST':
link = str( result['link'] )
pieceId = int( result['pieceId'] )
newRecording = YoutubeRecording(piece=Piece.objects.get(pk=pieceId), link=link)
newRecording.uploaded_by = request.user
newRecording.save()
return HttpResponse("OK")
class UploadFileForm(forms.Form):
file = forms.FileField( max_length="80")
def clean_file(self):
f = self.cleaned_data['file']
extension = os.path.splitext( f.name )[1]
if extension != ".pdf" and extension !=".mp3" and extension != ".zip":
raise forms.ValidationError( _("Unknown extension. Allowed extension are mp3, pdf and zip"))
def piece_view( request, pk ):
currentPiece = Piece.objects.get( pk=pk )
context = {'piece': currentPiece }
for score in currentPiece.scores.all():
if score.is_active_score( request.user ):
context['activeScore'] = score
context['pictureScore'] = score
if not 'pictureScore' in context.keys() and len( currentPiece.scores.all()) > 0:
context['pictureScore'] = currentPiece.scores.all()[0]
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
f = request.FILES['file']
[basename,extension] = os.path.splitext( f.name )
print ("extension " + extension + " basename " + basename)
if extension == ".mp3":
print("Uploaded Recording")
recording = Recording( piece=currentPiece, artist=basename, file=f )
recording.uploaded_by=request.user
recording.save()
elif extension == ".pdf":
print("Uploaded Score")
score = Score( piece=currentPiece, score_type=basename, file=f )
score.uploaded_by=request.user
score.save()
elif extension == ".zip":
#TODO
print ("uploaded zip - not yet supported")
else:
form = UploadFileForm()
context['form'] = form
return render( request, 'scoremanager/piece_view.html', context )