Skip to content

Commit

Permalink
Getting rid of unused code in the treeeditor
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Oordt committed Dec 16, 2010
1 parent d093d06 commit 9d92b0d
Showing 1 changed file with 2 additions and 185 deletions.
187 changes: 2 additions & 185 deletions editor/tree_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ def get_ordering(self):
return '', ''
return super(ChangeList, self).get_ordering()


def django_boolean_icon(field_val, alt_text=None, title=None):
"""
Return HTML code for a nice representation of true/false.
"""

# Origin: contrib/admin/templatetags/admin_list.py
BOOLEAN_MAPPING = { True: 'yes', False: 'no', None: 'unknown' }
alt_text = alt_text or BOOLEAN_MAPPING[field_val]
if title is not None:
title = 'title="%s" ' % title
else:
title = ''
return mark_safe(u'<img src="%simg/admin/icon-%s.gif" alt="%s" %s/>' %
(django_settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], alt_text, title))


def _build_tree_structure(cls):
"""
Build an in-memory representation of the item tree, trying to keep
Expand All @@ -58,62 +41,8 @@ def add_as_descendant(n, p):

return all_nodes


# ------------------------------------------------------------------------
def ajax_editable_boolean_cell(item, attr, text='', override=None):
"""
Generate a html snippet for showing a boolean value on the admin page.
Item is an object, attr is the attribute name we should display. Text
is an optional explanatory text to be included in the output.
This function will emit code to produce a checkbox input with its state
corresponding to the item.attr attribute if no override value is passed.
This input is wired to run a JS ajax updater to toggle the value.
If override is passed in, ignores the attr attribute and returns a
static image for the override boolean with no user interaction possible
(useful for "disabled and you can't change it" situations).
"""
if text:
text = '&nbsp;(%s)' % unicode(text)

if override is not None:
a = [ django_boolean_icon(override, text), text ]
else:
value = getattr(item, attr)
a = [
'<input type="checkbox"',
value and ' checked="checked"' or '',
' onclick="return inplace_toggle_boolean(%d, \'%s\')";' % (item.id, attr),
' />',
text,
]

a.insert(0, '<div id="wrap_%s_%d">' % ( attr, item.id ))
a.append('</div>')
return unicode(''.join(a))

# ------------------------------------------------------------------------
def ajax_editable_boolean(attr, short_description):
"""
Convenience function: Assign the return value of this method to a variable
of your ModelAdmin class and put the variable name into list_display.
Example:
class MyTreeEditor(TreeEditor):
list_display = ('__unicode__', 'active_toggle')
active_toggle = ajax_editable_boolean('active', _('is active'))
"""
def _fn(self, item):
return ajax_editable_boolean_cell(item, attr)
_fn.allow_tags = True
_fn.short_description = short_description
_fn.editable_boolean_field = attr
return _fn


class TreeEditor(admin.ModelAdmin):
list_per_page = 10000 # We can't have pagination
class Media:
css = {'all':(settings.MEDIA_PATH + "jquery.treeTable.css",)}
js = []
Expand All @@ -134,83 +63,7 @@ def __init__(self, *args, **kwargs):
'admin/%s/editor/tree_editor.html' % opts.app_label,
'admin/editor/tree_editor.html',
]

def _collect_editable_booleans(self):
"""
Collect all fields marked as editable booleans. We do not
want the user to be able to edit arbitrary fields by crafting
an AJAX request by hand.
"""
if hasattr(self, '_ajax_editable_booleans'):
return

self._ajax_editable_booleans = {}

for field in self.list_display:
# The ajax_editable_boolean return value has to be assigned
# to the ModelAdmin class
item = getattr(self.__class__, field, None)
if not item:
continue

attr = getattr(item, 'editable_boolean_field', None)
if attr:
def _fn(self, page):
return [ ajax_editable_boolean_cell(page, _fn.attr) ]
_fn.attr = attr
result_func = getattr(item, 'editable_boolean_result', _fn)
self._ajax_editable_booleans[attr] = result_func

def _refresh_changelist_caches(self):
"""
Refresh information used to show the changelist tree structure such as
inherited active/inactive states etc.
XXX: This is somewhat hacky, but since it's an internal method, so be it.
"""

pass

def _toggle_boolean(self, request):
"""
Handle an AJAX toggle_boolean request
"""
self._collect_editable_booleans()

item_id = request.POST.get('item_id')
attr = request.POST.get('attr')

if not self._ajax_editable_booleans.has_key(attr):
return HttpResponseBadRequest("not a valid attribute %s" % attr)

try:
obj = self.model._default_manager.get(pk=unquote(item_id))

attr = str(attr)

before_data = self._ajax_editable_booleans[attr](self, obj)

setattr(obj, attr, not getattr(obj, attr))
obj.save()

self._refresh_changelist_caches() # ???: Perhaps better a post_save signal?

# Construct html snippets to send back to client for status update
data = self._ajax_editable_booleans[attr](self, obj)

except Exception, e:
return HttpResponse("FAILED " + unicode(e), mimetype="text/plain")

# Weed out unchanged cells to keep the updates small. This assumes
# that the order a possible get_descendents() returns does not change
# before and after toggling this attribute. Unlikely, but still...
d = []
for a, b in zip(before_data, data):
if a != b:
d.append(b)

return HttpResponse(simplejson.dumps(d), mimetype="application/json")
#

def get_changelist(self, request, **kwargs):
"""
Returns the ChangeList class for use on the changelist page.
Expand All @@ -222,22 +75,6 @@ def changelist_view(self, request, extra_context=None, *args, **kwargs):
Handle the changelist view, the django view for the model instances
change list/actions page.
"""

if 'actions_column' not in self.list_display:
self.list_display.append('actions_column')

# handle common AJAX requests
if request.is_ajax():
cmd = request.POST.get('__cmd')
if cmd == 'toggle_boolean':
return self._toggle_boolean(request)
elif cmd == 'move_node':
return self._move_node(request)
else:
return HttpResponse('Oops. AJAX request not understood.')

self._refresh_changelist_caches()

extra_context = extra_context or {}
extra_context['EDITOR_MEDIA_PATH'] = settings.MEDIA_PATH
extra_context['tree_structure'] = mark_safe(simplejson.dumps(
Expand Down Expand Up @@ -265,25 +102,5 @@ def queryset(self, request):
Returns a QuerySet of all model instances that can be edited by the
admin site. This is used by changelist_view.
"""

# Use default ordering, always
return self.model._default_manager.get_query_set()

def _actions_column(self, page):
actions = []
actions.append(u'<a href="add/?parent=%s" title="%s"><img src="%simg/admin/icon_addlink.gif" alt="%s"></a>' % (
page.pk, _('Add child'), django_settings.ADMIN_MEDIA_PREFIX ,_('Add child')))

actions.append(u'<a href="#" onclick="return cut_item(\'%s\', this)" title="%s">&#x2702;</a>' % (
page.pk, _('Cut')))

actions.append(u'<a class="paste_target" href="#" onclick="return paste_item(\'%s\', \'last-child\')" title="%s">&#x21b3;</a>' % (
page.pk, _('Insert as child')))
actions.append(u'<a class="paste_target" href="#" onclick="return paste_item(\'%s\', \'left\')" title="%s">&#x21b1;</a>' % (
page.pk, _('Insert before')))
return actions

def actions_column(self, page):
return u' '.join(self._actions_column(page))
actions_column.allow_tags = True
actions_column.short_description = _('actions')

0 comments on commit 9d92b0d

Please sign in to comment.