示例#1
0
def create_audio_source(request):
    """
    View for dynamic creation of an audio source
    """
    if request.method == 'POST':
        audio_source = AudioSource(title=request.POST['title'], length=0)
        audio_source.save()

        add_tags_to_model(request.POST['tags'], audio_source)

        playlist_tuples = [(int(key.split('_')[-1]), int(val))
                           for key, val in request.POST.items()
                           if key.startswith('source_element_')]

        add_audiofiles_to_audiosource(playlist_tuples, audio_source)
        return JSONResponse({
            'status':'success',
            'action':'creation',
            'audiosource':audio_source.to_dict()
        })

    return JSONResponse({
        'action': 'creation',
        'tag_list': tag_list(),
        'title': 'Creation d''une nouvelle playlist',
        'form_url': reverse('create-audio-source')
    })
示例#2
0
def edit_audio_file(request, audiofile_id):
    """
    AJAX
    Returns a form to edit audiofile
    """
    # TODO : Use js templating instead of django templating
    audiofile = get_object_or_404(AudioFile, pk=audiofile_id)
    form = EditAudioFileForm(initial= {'title':audiofile.title,
                                       'artist':audiofile.artist})
    if request.method =='POST':
        to_delete_tags = [val for key, val in request.POST.items()
                          if key.startswith('to_delete_tag')]

        remaining_dict = dict([(k, v) for k, v in request.POST.items()
                               if not key.startswith('to_delete_tag')])

        form = EditAudioFileForm(remaining_dict)

        if form.is_valid():
            artist = form.cleaned_data['artist']
            title = form.cleaned_data['title']

            if artist != audiofile.artist or title != audiofile.title:
                audiofile.title = title
                audiofile.artist = artist
                audiofile.save_and_update_file()

            add_tags_to_model(form.cleaned_data['tags'], audiofile)

            audiofile.save()
            remove_tags_from_model(audiofile, to_delete_tags)
            return JSONResponse({
                'audiofile':audiofile.to_dict(),
                'status':'ok'
            })
        else:
            return JSONResponse(dict(form.errors.items()
                                     + [('status','errors')]))

    template = loader.get_template('audiosources/audiofile_edit_form.html')
    ctx = Context({'form':form, 'audiofile':audiofile})
    return JSONResponse({
        'html':template.render(ctx),
        'tag_list':tag_list(),
        'audiofile': audiofile.to_dict(with_tags=True),
        'artist_list':[a.name for a in Artist.objects.all()]
    })
示例#3
0
def edit_audio_source(request, audiosource_id):
    """
    JSON View for edition of an audio source (playlist)
    This view deletes every source element of the audio source
    every time it is edited
    """
    # TODO: Send 
    audio_source = get_object_or_404(AudioSource, id=audiosource_id)
    if request.method == 'POST':
        audio_source.title = request.POST['title']
        audio_source.description = request.POST['description']
        share = request.POST['share']
        audio_source.share = True if share == "yes" else False
        audio_source.sourceelement_set.all().delete()
        audio_source.length = 0
        add_tags_to_model(request.POST['tags'], audio_source)
        # Save to be able to add audiofiles to source
        audio_source.save()

        playlist_tuples = [(int(key.split('_')[-1]), int(val))
                           for key, val in request.POST.items()
                           if key.startswith('source_element_')]

        to_delete_tags = [val for key, val in request.POST.items()
                          if key.startswith('to_delete_tag')]

        remove_tags_from_model(audio_source, to_delete_tags)
        add_audiofiles_to_audiosource(playlist_tuples, audio_source)
        return JSONResponse({
            'status':'success',
            'action':'edition',
            'audiosource':audio_source.to_dict()
        })

    return JSONResponse({
        'action':'edition',
        'tag_list':tag_list(),
        'title': "Edition de la playlist %s" % audio_source.title,
        'audiosource':audio_source.to_dict(with_audiofiles=True,
                                           with_tags=True),
        'form_url': reverse('edit-audio-source', args=[audiosource_id])
    })