Ticket #2972: auto_populated_field_script.patch

File auto_populated_field_script.patch, 2.5 KB (added by panta@…, 17 years ago)

auto_populated_field_script patch for multiple slugs in a model

  • django/contrib/admin/templatetags/admin_modify.py

    diff -Naur django-trunk/django/contrib/admin/templatetags/admin_modify.py django-trunk-modified/django/contrib/admin/templatetags/admin_modify.py
    old new  
    180180output_all = register.simple_tag(output_all)
    181181
    182182def auto_populated_field_script(auto_pop_fields, change = False):
     183    dependencies = []
     184    dependencies_bodies = {}
     185    dependencies_scripts = {}
     186    for field in auto_pop_fields:
     187        for g in field.prepopulate_from:
     188            if g not in dependencies:
     189                dependencies.append(g)
     190                dependencies_bodies[g] = []
     191                dependencies_scripts[g] = ""
     192
    183193    t = []
    184194    for field in auto_pop_fields:
     195        for g in field.prepopulate_from:
     196            if g not in dependencies:
     197                dependencies.append(g)
    185198        if change:
    186199            t.append(u'document.getElementById("id_%s")._changed = true;' % field.name)
    187200        else:
     
    189202
    190203        add_values = u' + " " + '.join([u'document.getElementById("id_%s").value' % g for g in field.prepopulate_from])
    191204        for f in field.prepopulate_from:
    192             t.append(u'document.getElementById("id_%s").onkeyup = function() {' \
    193                      ' var e = document.getElementById("id_%s");' \
    194                      ' if(!e._changed) { e.value = URLify(%s, %s);} }; ' % (
    195                      f, field.name, add_values, field.max_length))
     205            var_name = 'e_%s' % field.name
     206            f_script_body = u' var %s = document.getElementById("id_%s");' \
     207                            ' if(!%s._changed) { %s.value = URLify(%s, %s);}\n ' % (
     208                var_name, field.name, var_name, var_name, add_values, field.max_length)
     209            dependencies_bodies[f].append(f_script_body)
     210
     211    t = []
     212    for field in auto_pop_fields:
     213        for f in field.prepopulate_from:
     214            if dependencies_scripts[f] == "":
     215                f_script_pre  = u'document.getElementById("id_%s").onkeyup = function() {' % f
     216                f_script_post = '\n }; '
     217                f_script = f_script_pre + u''.join(dependencies_bodies[f]) + f_script_post
     218                t.append(f_script)
     219                dependencies_scripts[f] = f_script
     220
    196221    return u''.join(t)
    197222auto_populated_field_script = register.simple_tag(auto_populated_field_script)
    198223
Back to Top