Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DTCallback(DTPluginBase):
- """
- This class executes a callback for fields that require it.
- There is some super hacky shit making this work right now, as you can see in CELL. I should
- probably work to remove this garbage.
- """
- def __init__(self, callbacks, header_callbacks=None):
- self.callbacks = callbacks
- self.header_callbacks = header_callbacks
- ##### THE CHANGED BIT ######
- def header(self, callchain, column_index):
- callback = None
- if column_index in self.header_callbacks:
- callback = self.header_callbacks[column_index]
- if callback is not None:
- value = '<span>%s</span>' % callback(column_index, data)
- if isinstance(value, basestring):
- #HACK HACK HACK HACK, lxml is supergay when it comes to HTML or not-html. Just can't insert TEXT and have it play nice.
- #God forbid bad HTML should come along.
- try:
- return etree.fromstring(value)
- except etree.XMLSyntaxError:
- return value
- elif isinstance(value, (list, tuple)):
- return [etree.fromstring(i) for i in value]
- return value
- ##### END THE CHANGED BIT ######
- def cell(self, callchain, data, column_index, column_name, row_number):
- callback = None
- if column_index in self.callbacks:
- callback = self.callbacks[column_index]
- if column_name in self.callbacks:
- callback = self.callbacks[column_name]
- if callback is not None:
- value = '<span>%s</span>' % callback(column_name, data)
- if isinstance(value, basestring):
- #HACK HACK HACK HACK, lxml is supergay when it comes to HTML or not-html. Just can't insert TEXT and have it play nice.
- #God forbid bad HTML should come along.
- try:
- return etree.fromstring(value)
- except etree.XMLSyntaxError:
- return value
- elif isinstance(value, (list, tuple)):
- return [etree.fromstring(i) for i in value]
- return value
- def table(context, queryset, fields=(), exclude=(), classes=(), record_url=None, instance=None, make_selectable=False, make_editable=False, listfield_callback=None, wrapper=True, **kwargs):
- listfield_callback = listfield_callback or {}
- header_callback = {}
- #THe crazy mako shit is mostly at the top
- try:
- context.caller_stack._push_frame()
- caller = context.get('caller', runtime.UNDEFINED)
- capture = context.get('capture', runtime.UNDEFINED)
- for key in fields:
- index = isinstance(key, (tuple, list)) and key[0] or key
- #FIRST COLUMN SPECIAL CASE ***DEPRECATED***
- if key == fields[0] and hasattr(caller, 'td__first'): #First column override :TODO: make this more generic
- #:TODO: I wish I didn't need to wrap this in a lambda, backward compat issue; also I don't know how passing context to a mako function works.
- listfield_callback[1] = partial(capture, lambda attr, obj, context: getattr(caller, 'td__first')(attr, obj))
- #:TODO: can we deprecate this too?
- if hasattr(caller, 'td_%s' % index):
- func = getattr(caller, 'td_%s' % index)
- listfield_callback[index] = partial(capture, partial(lambda func, attr, obj: func(obj), func))
- ##### THE CHANGED BIT ######
- if hasattr(caller, 'th_%s' % index):
- func = getattr(caller, 'th_%s' % index)
- header_callback[index] = partial(capture, partial(lambda func, attr, obj: func(obj), func))
- ##### END THE CHANGED BIT ######
- finally:
- context.caller_stack._pop_frame()
- #End completely crazy mako shit
- ...
Add Comment
Please, Sign In to add comment