<commit_before>{% extends "common/base.html" %}
{% load i18n %}

{% block title %}{% trans "Sell books" %}{% endblock %}

{% block content %}
<h1>{% trans "Sell books" %} <small>{% trans "Step 2 of 3: choose the books" %}</small></h1>

{% include "sell/books_chosen_list.html" %}
<div class="panel-group" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#bookList">{% trans "Choose from the list" %}</a></h4>
        </div>
        <div id="bookList" class="panel-collapse collapse">
            <div class="panel-body">
                {% include "sell/books_book_list.html" %}
            </div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#addBookForm">{% trans "Add new" %}</a></h4>
        </div>
        <div id="addBookForm" class="panel-collapse collapse">
            <div class="panel-body form-horizontal">
                {% include "common/default_form.html" %}
                <button class="btn btn-default pull-right" id="btn-add-new-book"><span class="glyphicon glyphicon-plus"></span> {% trans "Add" %}</button>
            </div>
        </div>
    </div>
</div>
<div class="btn-toolbar">
    <form action="{{ request.path }}" method="post">
        {% csrf_token %}
        <button class="btn btn-default" id="btn-back"><span class="glyphicon glyphicon-chevron-left"></span> {% trans "Back" %}</button>
        <button class="btn btn-primary pull-right" id="btn-next">{% trans "Next" %} <span class="glyphicon glyphicon-chevron-right"></span></button>
    </form>
</div>
{% endblock %}

{% block footer %}
{% load staticfiles %}
<script>
    var chosenBooks = {% if chosen_books %}{{ chosen_books|safe }}{% else %}[]{% endif %};

    $('.btn-add-book').on('click', function() {
        addBook($(this).closest($('tr')));
    });
    $('#btn-add-new-book').on('click', function () {
        addNewBook();
    });
    $('button#btn-next,button#btn-back').on('click', function() {
        $('form').append($('<input type="hidden" name="book_data">').attr('value', JSON.stringify(chosenBooks)))
            .append($('<input type="hidden" name="' + this.id + '">'))
            .submit();
    });

    function ExistingBook(pk) {
        this.pk = pk;
    }

    function NewBook() {
    }

    function addRetrievedBooks() {
        chosenBooks = chosenBooks.filter(function(n) { return n != null }); // remove all nulls

        for (var book in chosenBooks) {
            if (chosenBooks[book].pk != undefined) {
                addBook($('#bookList').find('table tr[data-pk="' + chosenBooks[book].pk + '"]'));
            } else {
                addBookTr(createNewBookTr(chosenBooks[book]));
            }
        }
    }

    function addBook(tr) {
        var ctr = tr.clone();
        tr.addClass('hidden');
        $('td:last-child', ctr).remove();

        addBookTr(ctr, new ExistingBook(ctr.data('pk')));
    }

    function addNewBook() {
        var ids = ['isbn', 'publisher', 'title', 'publication_year', 'price'];
        var vals = [];

        var book = new NewBook();
        var add = false;
        for (var prid in ids) {
            var input = $('input[name="' + ids[prid] + '"]');
            book[ids[prid]] = input.val();
            input.val("");

            if (book[ids[prid]] != "") {
                add = true;
            }
            vals.push(book[ids[prid]]);
        }

        if (add) {
            addBookTr(createNewBookTr(vals), book);
        }
    }

    function createNewBookTr(vals) {
        var tr = $('<tr/>');

        for (var id in vals) {
            tr = tr.append($('<td/>').append(vals[id]));
        }

        return tr;
    }

    function addBookTr(tr, book) {
        var id = chosenBooks.push(book) - 1;

        var button = $('<button class="btn btn-xs btn-link btn-remove-book"><span class="glyphicon glyphicon-remove"></span> {% trans "Remove" %}</button>');
        button.on('click', function() {
            removeBook($('#chosen-books-list').find('button.btn-remove-book').index(this))
        });
        tr.append($('<td/>').append(button));

        var chosenTable = $('#chosen-books-list');
        chosenTable.removeClass('hidden');
        chosenTable.append(tr);
    }

    function removeBook(id) {
        var book = chosenBooks[id];
        if (book.pk != undefined) {
            $('#bookList').find('table tr[data-pk="' + book.pk + '"]').removeClass('hidden');
        }

        var chosenBooksList = $('#chosen-books-list');
        chosenBooks.splice(id, 1);
        chosenBooksList.find('tbody tr:eq(' + id + ')').remove();

        if (chosenBooks.length == 0) {
            chosenBooksList.addClass('hidden');
        }
    }

    addRetrievedBooks();
</script>
<script src="{% static "js/isbnfinder.js" %}"></script>
{% endblock %}<commit_msg>Fix a bug that caused nulls to be sent as books<commit_after>{% extends "common/base.html" %}
{% load i18n %}

{% block title %}{% trans "Sell books" %}{% endblock %}

{% block content %}
<h1>{% trans "Sell books" %} <small>{% trans "Step 2 of 3: choose the books" %}</small></h1>

{% include "sell/books_chosen_list.html" %}
<div class="panel-group" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#bookList">{% trans "Choose from the list" %}</a></h4>
        </div>
        <div id="bookList" class="panel-collapse collapse">
            <div class="panel-body">
                {% include "sell/books_book_list.html" %}
            </div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#addBookForm">{% trans "Add new" %}</a></h4>
        </div>
        <div id="addBookForm" class="panel-collapse collapse">
            <div class="panel-body form-horizontal">
                {% include "common/default_form.html" %}
                <button class="btn btn-default pull-right" id="btn-add-new-book"><span class="glyphicon glyphicon-plus"></span> {% trans "Add" %}</button>
            </div>
        </div>
    </div>
</div>
<div class="btn-toolbar">
    <form action="{{ request.path }}" method="post">
        {% csrf_token %}
        <button class="btn btn-default" id="btn-back"><span class="glyphicon glyphicon-chevron-left"></span> {% trans "Back" %}</button>
        <button class="btn btn-primary pull-right" id="btn-next">{% trans "Next" %} <span class="glyphicon glyphicon-chevron-right"></span></button>
    </form>
</div>
{% endblock %}

{% block footer %}
{% load staticfiles %}
<script>
    var chosenBooks = {% if chosen_books %}{{ chosen_books|safe }}{% else %}[]{% endif %};

    $('.btn-add-book').on('click', function() {
        var tr = $(this).closest($('tr'));
        var book = new ExistingBook(tr.data('pk'));
        chosenBooks.push(book);
        addExistingBook(tr, book);
    });
    $('#btn-add-new-book').on('click', function () {
        addNewBook();
    });
    $('button#btn-next,button#btn-back').on('click', function() {
        $('form').append($('<input type="hidden" name="book_data">').attr('value', JSON.stringify(chosenBooks)))
            .append($('<input type="hidden" name="' + this.id + '">'))
            .submit();
    });

    function ExistingBook(pk) {
        this.pk = pk;
    }

    function NewBook() {
    }

    function addRetrievedBooks() {
        for (var book in chosenBooks) {
            if (chosenBooks[book].pk != undefined) {
                addExistingBook($('#bookList').find('table tr[data-pk="' + chosenBooks[book].pk + '"]'));
            } else {
                addBookTr(createNewBookTr(chosenBooks[book]));
            }
        }
    }

    function addExistingBook(tr, book) {
        var ctr = tr.clone();
        tr.addClass('hidden');
        $('td:last-child', ctr).remove();

        addBookTr(ctr, book);
    }

    function addNewBook() {
        var ids = ['isbn', 'publisher', 'title', 'publication_year', 'price'];
        var vals = [];

        var book = new NewBook();
        var add = false;
        for (var prid in ids) {
            var input = $('input[name="' + ids[prid] + '"]');
            book[ids[prid]] = input.val();
            input.val("");

            if (book[ids[prid]] != "") {
                add = true;
            }
            vals.push(book[ids[prid]]);
        }

        if (add) {
            chosenBooks.push(book);
            addBookTr(createNewBookTr(vals), book);
        }
    }

    function createNewBookTr(vals) {
        var tr = $('<tr/>');

        for (var id in vals) {
            tr = tr.append($('<td/>').append(vals[id]));
        }

        return tr;
    }

    function addBookTr(tr, book) {
        var button = $('<button class="btn btn-xs btn-link btn-remove-book"><span class="glyphicon glyphicon-remove"></span> {% trans "Remove" %}</button>');
        button.on('click', function() {
            removeBook($('#chosen-books-list').find('button.btn-remove-book').index(this))
        });
        tr.append($('<td/>').append(button));

        var chosenTable = $('#chosen-books-list');
        chosenTable.removeClass('hidden');
        chosenTable.append(tr);
    }

    function removeBook(id) {
        var book = chosenBooks[id];
        if (book.pk != undefined) {
            $('#bookList').find('table tr[data-pk="' + book.pk + '"]').removeClass('hidden');
        }

        var chosenBooksList = $('#chosen-books-list');
        chosenBooks.splice(id, 1);
        chosenBooksList.find('tbody tr:eq(' + id + ')').remove();

        if (chosenBooks.length == 0) {
            chosenBooksList.addClass('hidden');
        }
    }

    addRetrievedBooks();
</script>
<script src="{% static "js/isbnfinder.js" %}"></script>
{% endblock %}
{'fraction_non_alphanumeric': 0.19733201581027668, 'fraction_numerical': 0.0024703557312252965, 'mean_word_length': 2.4043054154053145, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5806971', 'n_tokens_mistral': 3296, 'n_tokens_neox': 3107, 'n_words': 628}