Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. class DTCallback(DTPluginBase):
  2. """
  3. This class executes a callback for fields that require it.
  4.  
  5. There is some super hacky shit making this work right now, as you can see in CELL. I should
  6. probably work to remove this garbage.
  7. """
  8. def __init__(self, callbacks, header_callbacks=None):
  9. self.callbacks = callbacks
  10. self.header_callbacks = header_callbacks
  11.  
  12. ##### THE CHANGED BIT ######
  13. def header(self, callchain, column_index):
  14. callback = None
  15. if column_index in self.header_callbacks:
  16. callback = self.header_callbacks[column_index]
  17. if callback is not None:
  18. value = '<span>%s</span>' % callback(column_index, data)
  19. if isinstance(value, basestring):
  20. #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.
  21. #God forbid bad HTML should come along.
  22. try:
  23. return etree.fromstring(value)
  24. except etree.XMLSyntaxError:
  25. return value
  26. elif isinstance(value, (list, tuple)):
  27. return [etree.fromstring(i) for i in value]
  28. return value
  29. ##### END THE CHANGED BIT ######
  30.  
  31. def cell(self, callchain, data, column_index, column_name, row_number):
  32. callback = None
  33. if column_index in self.callbacks:
  34. callback = self.callbacks[column_index]
  35. if column_name in self.callbacks:
  36. callback = self.callbacks[column_name]
  37. if callback is not None:
  38. value = '<span>%s</span>' % callback(column_name, data)
  39. if isinstance(value, basestring):
  40. #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.
  41. #God forbid bad HTML should come along.
  42. try:
  43. return etree.fromstring(value)
  44. except etree.XMLSyntaxError:
  45. return value
  46. elif isinstance(value, (list, tuple)):
  47. return [etree.fromstring(i) for i in value]
  48. return value
  49.  
  50.  
  51.  
  52.  
  53. def table(context, queryset, fields=(), exclude=(), classes=(), record_url=None, instance=None, make_selectable=False, make_editable=False, listfield_callback=None, wrapper=True, **kwargs):
  54. listfield_callback = listfield_callback or {}
  55. header_callback = {}
  56.  
  57. #THe crazy mako shit is mostly at the top
  58. try:
  59. context.caller_stack._push_frame()
  60. caller = context.get('caller', runtime.UNDEFINED)
  61. capture = context.get('capture', runtime.UNDEFINED)
  62.  
  63. for key in fields:
  64. index = isinstance(key, (tuple, list)) and key[0] or key
  65. #FIRST COLUMN SPECIAL CASE ***DEPRECATED***
  66. if key == fields[0] and hasattr(caller, 'td__first'): #First column override :TODO: make this more generic
  67. #: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.
  68. listfield_callback[1] = partial(capture, lambda attr, obj, context: getattr(caller, 'td__first')(attr, obj))
  69. #:TODO: can we deprecate this too?
  70. if hasattr(caller, 'td_%s' % index):
  71. func = getattr(caller, 'td_%s' % index)
  72. listfield_callback[index] = partial(capture, partial(lambda func, attr, obj: func(obj), func))
  73.  
  74.  
  75. ##### THE CHANGED BIT ######
  76.  
  77. if hasattr(caller, 'th_%s' % index):
  78. func = getattr(caller, 'th_%s' % index)
  79. header_callback[index] = partial(capture, partial(lambda func, attr, obj: func(obj), func))
  80.  
  81. ##### END THE CHANGED BIT ######
  82.  
  83.  
  84. finally:
  85. context.caller_stack._pop_frame()
  86. #End completely crazy mako shit
  87.  
  88. ...
Add Comment
Please, Sign In to add comment