Opened 7 years ago

Closed 4 years ago

#6723 closed Uncategorized (duplicate)

djangonewforms-admin edit related feature (JS)

Reported by: zbyszek Owned by: nobody
Component: Forms Version: newforms-admin
Severity: Normal Keywords: newforms-admin foreignkey edit nfa-someday
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

i created a litle modification (edit feature) for related objects. only thing that i made is an function for js, and added only 2 lines of code in widgets.py in contrib/admin/. this does not affect anything else. so it should be safe to use. maybe someone could make it better or add delete feautre?
this is for the djangonewforms-admin.

so the code for the widgets.py would change to:

class RelatedFieldWidgetWrapper(object):
    """
    This class is a wrapper whose __call__() method mimics the interface of a
    Widget's render() method.
    """
    def __init__(self, render_func, rel, admin_site):
        self.render_func, self.rel = render_func, rel
        # so we can check if the related object is registered with this AdminSite
        self.admin_site = admin_site

    def __call__(self, name, value, *args, **kwargs):
        from django.conf import settings
        rel_to = self.rel.to
        related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower())
        output = [self.render_func(name, value, *args, **kwargs)]
        if rel_to in self.admin_site._registry: # If the related object has an admin interface:
            # TODO: "id_" is hard-coded here. This should instead use the correct
            # API to determine the ID dynamically.
            output.append(u'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
                (related_url, name))
            output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>' % settings.ADMIN_MEDIA_PREFIX)
            output.append('<a href="%s" class="related-lookup" id="edit_id_%s" onclick="return showEditSelectedPopup(this);"> ' % \
            (related_url, name))# this added someone can change the class name ;/
            output.append(u'<img src="%simg/admin/icon_changelink.gif" width="10" height="10" alt="Edit Selected"/></a>' % settings.ADMIN_MEDIA_PREFIX)#this added
        return mark_safe(u''.join(output))

    def __deepcopy__(self, memo):
        # There's no reason to deepcopy admin_site, etc, so just return self.
        memo[id(self)] = self
        return self

and the function for the JS would be(a bit modified showAddAnotherPopup() function):

function showEditSelectedPopup(triggeringLink) {
    var name = triggeringLink.id.replace(/^edit_/, '');
    name = name.replace(/\./g, '___');
	var elem = document.getElementById(name);
	var id = elem.options[elem.selectedIndex].value;
	if (id == '')
		return false;
	
    href = triggeringLink.href +id+'/';
    if (href.indexOf('?') == -1) {
        href += '?_popup=1';
    } else {
        href  += '&_popup=1';
    }
    var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
    win.focus();
    return false;
}

sorry for not using diffs etc , but i'm kinda new in all this:

Attachments (4)

widgets.diff (891 bytes) - added by zbyszek 7 years ago.
diff file for widgets.py
RelatedObjectLookups.patch (6.0 KB) - added by zbyszek 7 years ago.
for the JS file
RelatedObjectLookups.2.patch (6.0 KB) - added by zbyszek 7 years ago.
for the JS file
newforms-admin.patch (7.1 KB) - added by zbyszek 7 years ago.
Whole patch from top dir

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Please read the contributing document (this, in particular) to see how to create a patch. Asking us to wade through all this to work out which lines have changed is a but unfair.

Changed 7 years ago by zbyszek

diff file for widgets.py

Changed 7 years ago by zbyszek

for the JS file

Changed 7 years ago by zbyszek

for the JS file

Changed 7 years ago by zbyszek

Whole patch from top dir

comment:2 Changed 7 years ago by zbyszek

one more thing. in the future there must be aslo a function : dismissEditSelectedPopup() so the window will close itself and change the value of input or select. for this there should be a input in change_form.html <input name="_edit"> the same way it is for _post variable, and in options.py there has to be if request.POST.has_key('_edit') etc to return <scirpt>... the same way as with _popup. maybe the best way would have not _edit but _action value="add" or "edit"?

comment:3 Changed 7 years ago by jacob

  • Triage Stage changed from Unreviewed to Design decision needed

comment:4 Changed 7 years ago by Karen Tracey <kmtracey@…>

  • Keywords nfa-someday added

comment:5 Changed 4 years ago by julien

  • Resolution set to duplicate
  • Severity set to Normal
  • Status changed from new to closed
  • Type set to Uncategorized

Closing this as a dupe of #13165, which is newer but has a much more up-to-date discussion and patch.

Note: See TracTickets for help on using tickets.
Back to Top