Ticket #12692: 12692.2.diff

File 12692.2.diff, 5.3 KB (added by Jannis Leidel, 14 years ago)

A patch without console.log calls

  • django/contrib/admin/media/js/inlines.js

    diff --git a/django/contrib/admin/media/js/inlines.js b/django/contrib/admin/media/js/inlines.js
    index c45ce58..69ae74b 100644
    a b  
    2626                };
    2727                var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
    2828                var initialForms = $("#id_" + options.prefix + "-INITIAL_FORMS");
    29                 var maxForms = parseInt(totalForms.val());
     29                var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS");
    3030                // only show the add button if we are allowed to add more items
    31                 var showAddButton = (maxForms - parseInt(initialForms.val())) > 0;
     31                var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-initialForms.val()) > 0));
    3232                var selectedItems = this;
    3333                $(this).each(function(i) {
    3434                        $(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
    35                         // hide the extras, but only if there were no form errors
    36                         if (!$(".errornote").html()) {
    37                                 var relatedItems = $(selectedItems).not("." + options.emptyCssClass);
    38                                 extraRows = relatedItems.length;
    39                                 if (parseInt(initialForms.val()) >= 0) {
    40                                         $(relatedItems).slice(initialForms.val()).remove();
    41                                 } else {
    42                                         $(relatedItems).remove();
    43                                 }
    44                                 totalForms.val(parseInt(initialForms.val()));
    45                         }
    4635                });
    4736                if ($(this).length && showAddButton) {
    4837                        var addButton;
     
    5847                                addButton = $(this).filter(":last").next().find("a");
    5948                        }
    6049                        addButton.click(function() {
    61                                 var totalForms = parseInt($("#id_" + options.prefix + "-TOTAL_FORMS").val());
    62                                 var initialForms = parseInt($("#id_" + options.prefix + "-INITIAL_FORMS").val());
    63                                 var nextIndex = totalForms + 1;
     50                                var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
     51                                var nextIndex = parseInt(totalForms.val()) + 1;
    6452                                var template = $("#" + options.prefix + "-empty");
    6553                                var row = template.clone(true).get(0);
    6654                                $(row).removeClass(options.emptyCssClass).removeAttr("id").insertBefore($(template));
     
    7967                                        // last child element of the form's container:
    8068                                        $(row).children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");
    8169                                }
     70                                $(row).find("input,select,textarea,label").each(function() {
     71                                        updateElementIndex(this, options.prefix, totalForms.val());
     72                                });
    8273                                // Update number of total forms
    83                                 $("#id_" + options.prefix + "-TOTAL_FORMS").val(nextIndex);
    84                                 // Hide add button in case we've hit the max
    85                                 if (maxForms <= nextIndex) {
     74                                $(totalForms).val(nextIndex);
     75                                // Hide add button in case we've hit the max, except we want to add infinitely
     76                                if ((maxForms.val() != 0) && (maxForms.val() <= totalForms.val())) {
    8677                                        addButton.parent().hide();
    8778                                }
    8879                                // The delete button of each row triggers a bunch of other things
     
    9687                                        var forms = $("." + options.formCssClass);
    9788                                        $("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
    9889                                        // Show add button again once we drop below max
    99                                         if (maxForms >= forms.length) {
     90                                        if ((maxForms.val() == 0) || (maxForms.val() >= forms.length)) {
    10091                                                addButton.parent().show();
    10192                                        }
    10293                                        // Also, update names and ids for all remaining form controls
     
    10899                                        }
    109100                                        return false;
    110101                                });
    111                                 $(row).find("input,select,textarea,label").each(function() {
    112                                         updateElementIndex(this, options.prefix, totalForms);
    113                                 });
    114102                                // If a post-add callback was supplied, call it with the added form:
    115103                                if (options.added) options.added($(row));
    116104                                return false;
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 9dfdb8b..4e3cfdc 100644
    a b class InlineModelAdmin(BaseModelAdmin):  
    11931193
    11941194    def _media(self):
    11951195        from django.conf import settings
    1196         js = ['js/jquery.min.js', 'js/inlines.min.js']
     1196        js = ['js/jquery.min.js', 'js/inlines.js']
    11971197        if self.prepopulated_fields:
    11981198            js.append('js/urlify.js')
    11991199        if self.filter_vertical or self.filter_horizontal:
  • django/forms/formsets.py

    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    index a86c18f..58af0ac 100644
    a b __all__ = ('BaseFormSet', 'all_valid')  
    1212# special field names
    1313TOTAL_FORM_COUNT = 'TOTAL_FORMS'
    1414INITIAL_FORM_COUNT = 'INITIAL_FORMS'
     15MAX_NUM_FORM_COUNT = 'MAX_NUM_FORMS'
    1516ORDERING_FIELD_NAME = 'ORDER'
    1617DELETION_FIELD_NAME = 'DELETE'
    1718
    class ManagementForm(Form):  
    2425    def __init__(self, *args, **kwargs):
    2526        self.base_fields[TOTAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
    2627        self.base_fields[INITIAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
     28        self.base_fields[MAX_NUM_FORM_COUNT] = IntegerField(widget=HiddenInput)
    2729        super(ManagementForm, self).__init__(*args, **kwargs)
    2830
    2931class BaseFormSet(StrAndUnicode):
    class BaseFormSet(StrAndUnicode):  
    5658        else:
    5759            form = ManagementForm(auto_id=self.auto_id, prefix=self.prefix, initial={
    5860                TOTAL_FORM_COUNT: self.total_form_count(),
    59                 INITIAL_FORM_COUNT: self.initial_form_count()
     61                INITIAL_FORM_COUNT: self.initial_form_count(),
     62                MAX_NUM_FORM_COUNT: self.max_num
    6063            })
    6164        return form
    6265    management_form = property(_management_form)
Back to Top