def remove_orphaned_chords():
    # Compile a list of all chord ids that are in sequences
    used_chord_ids = set()
    for sequence in ChordSequence.objects.all():
        used_chord_ids |= set(get_chord_list_from_sequence(sequence))
    all_chord_ids = set( [v['id'] for v in Chord.objects.all().values('id')] )
    unused_chords = all_chord_ids - used_chord_ids
    if len(unused_chords) > 0:
        print "Deleting chords: %s" % ", ".join(["%s" % c for c in unused_chords])
        # Delete all these unused chords
        Chord.objects.filter(id__in=unused_chords).delete()
        print "Done"
    else:
        print "No unused chords to delete"
    return 0
def remove_orphaned_chords():
    # Compile a list of all chord ids that are in sequences
    used_chord_ids = set()
    for sequence in ChordSequence.objects.all():
        used_chord_ids |= set(get_chord_list_from_sequence(sequence))
    all_chord_ids = set([v['id'] for v in Chord.objects.all().values('id')])
    unused_chords = all_chord_ids - used_chord_ids
    if len(unused_chords) > 0:
        print "Deleting chords: %s" % ", ".join(
            ["%s" % c for c in unused_chords])
        # Delete all these unused chords
        Chord.objects.filter(id__in=unused_chords).delete()
        print "Done"
    else:
        print "No unused chords to delete"
    return 0
Exemplo n.º 3
0
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))
Exemplo n.º 4
0
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))