def edit_song(request, id): """ Edit song info. This is not editing a chord sequence, but only the meta-info about a song. This info can also be edited while editing a chord sequence. """ if id is None: song = Song() else: song = get_object_or_404(Song, id=id) if request.method == "POST": if 'cancel' in request.POST: return HttpResponseRedirect(reverse(song_index)) else: form = SongForm(instance=song, data=request.POST) if form.is_valid(): form.save() else: form = SongForm(instance=song) context = { 'song' : song, 'form' : form, } return render_to_response('sequences/edit_song.html', context, RequestContext(request))
def edit_song(request, id): """ Edit song info. This is not editing a chord sequence, but only the meta-info about a song. This info can also be edited while editing a chord sequence. """ if id is None: song = Song() else: song = get_object_or_404(Song, id=id) if request.method == "POST": if 'cancel' in request.POST: return HttpResponseRedirect(reverse(song_index)) else: form = SongForm(instance=song, data=request.POST) if form.is_valid(): form.save() else: form = SongForm(instance=song) context = { 'song': song, 'form': form, } return render_to_response('sequences/edit_song.html', context, RequestContext(request))
def edit_sequence(request, id): if id is None: sequence = ChordSequence() song = Song() sequence.song = song else: sequence = get_object_or_404(ChordSequence, id=id) song = sequence.song if request.method == "POST": if 'cancel' in request.POST: return HttpResponseRedirect(reverse(index)) else: # Save the data form = ChordSequenceForm(instance=sequence, data=request.POST) song_form = SongForm(instance=song, prefix="song", data=request.POST) if form.is_valid(): # See whether a song was selected if form.cleaned_data['song'] is not None and \ (sequence.song is None or form.cleaned_data['song'].id != sequence.song.id): # A song has been selected for a new sequence or # the song has been changed # Use the selected song form.save() else: if form.cleaned_data['song'] is None: # The song was set to empty, so these details # should be saved as a new song song_form = SongForm(prefix="song", data=request.POST) # We need to validate the song details if song_form.is_valid(): saved_song = song_form.save() saved_seq = form.save() saved_seq.song = saved_song saved_seq.save() # First, keep a list of all the chords that were in the sequence before old_chords = get_chord_list_from_sequence(sequence) # Handle the chord sequence data chord_dataset = json.loads(request.POST['chords']) chords = [] for chord_data in chord_dataset: chords.append(_save_chord_from_data(chord_data, sequence)) # Now they're saved, chain the sequence together for i,chord in enumerate(chords): if i < len(chords)-1: chord.next = chords[i+1] else: chord.next = None chord.save() if len(chords): sequence.first_chord = chords[0] else: sequence.first_chord = None sequence.save() # Clean up any chords that have been deleted new_chords = get_chord_list_from_sequence(sequence) for old_chord in old_chords: if old_chord not in new_chords: # Chord has been removed from the sequence - delete it to_delete = Chord.objects.get(id=old_chord) to_delete.delete() if 'save_and_exit' in request.POST: return HttpResponseRedirect(reverse(index)) else: return HttpResponseRedirect(reverse(edit_sequence, kwargs={ 'id' : sequence.id })) else: form = ChordSequenceForm(instance=sequence) song_form = SongForm(prefix="song", instance=song) chord_form = ChordForm() chord_editor_options = json.dumps({ 'bars_across' : 4, 'width' : 1000, 'bar_length' : sequence.bar_length, }) chord_data = sequence.to_json() chord_types = [( type.id, type.symbol ) for type in ChordType.objects.all()] context = { 'sequence' : sequence, 'form' : form, 'song_form' : song_form, 'chord_form' : chord_form, 'chord_data' : chord_data, 'chord_editor_options' : chord_editor_options, 'chord_roots' : _root_choices, 'chord_types' : chord_types, 'categories' : category_pairs, 'pos_tags' : pos_tags, } return render_to_response('sequences/edit_sequence.html', context, RequestContext(request))
def edit_sequence(request, id): if id is None: sequence = ChordSequence() song = Song() sequence.song = song else: sequence = get_object_or_404(ChordSequence, id=id) song = sequence.song if request.method == "POST": if 'cancel' in request.POST: return HttpResponseRedirect(reverse(index)) else: # Save the data form = ChordSequenceForm(instance=sequence, data=request.POST) song_form = SongForm(instance=song, prefix="song", data=request.POST) if form.is_valid(): # See whether a song was selected if form.cleaned_data['song'] is not None and \ (sequence.song is None or form.cleaned_data['song'].id != sequence.song.id): # A song has been selected for a new sequence or # the song has been changed # Use the selected song form.save() else: if form.cleaned_data['song'] is None: # The song was set to empty, so these details # should be saved as a new song song_form = SongForm(prefix="song", data=request.POST) # We need to validate the song details if song_form.is_valid(): saved_song = song_form.save() saved_seq = form.save() saved_seq.song = saved_song saved_seq.save() # First, keep a list of all the chords that were in the sequence before old_chords = get_chord_list_from_sequence(sequence) # Handle the chord sequence data chord_dataset = json.loads(request.POST['chords']) chords = [] for chord_data in chord_dataset: chords.append(_save_chord_from_data(chord_data, sequence)) # Now they're saved, chain the sequence together for i, chord in enumerate(chords): if i < len(chords) - 1: chord.next = chords[i + 1] else: chord.next = None chord.save() if len(chords): sequence.first_chord = chords[0] else: sequence.first_chord = None sequence.save() # Clean up any chords that have been deleted new_chords = get_chord_list_from_sequence(sequence) for old_chord in old_chords: if old_chord not in new_chords: # Chord has been removed from the sequence - delete it to_delete = Chord.objects.get(id=old_chord) to_delete.delete() if 'save_and_exit' in request.POST: return HttpResponseRedirect(reverse(index)) else: return HttpResponseRedirect( reverse(edit_sequence, kwargs={'id': sequence.id})) else: form = ChordSequenceForm(instance=sequence) song_form = SongForm(prefix="song", instance=song) chord_form = ChordForm() chord_editor_options = json.dumps({ 'bars_across': 4, 'width': 1000, 'bar_length': sequence.bar_length, }) chord_data = sequence.to_json() chord_types = [(type.id, type.symbol) for type in ChordType.objects.all()] context = { 'sequence': sequence, 'form': form, 'song_form': song_form, 'chord_form': chord_form, 'chord_data': chord_data, 'chord_editor_options': chord_editor_options, 'chord_roots': _root_choices, 'chord_types': chord_types, 'categories': category_pairs, 'pos_tags': pos_tags, } return render_to_response('sequences/edit_sequence.html', context, RequestContext(request))