template.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from jinja2 import contextfunction
  2. from flask_admin._compat import string_types, reduce
  3. from flask_admin.babel import gettext
  4. class BaseListRowAction(object):
  5. def __init__(self, title=None):
  6. self.title = title
  7. def render(self, context, row_id, row):
  8. raise NotImplementedError()
  9. @contextfunction
  10. def render_ctx(self, context, row_id, row):
  11. return self.render(context, row_id, row)
  12. def _resolve_symbol(self, context, symbol):
  13. if '.' in symbol:
  14. parts = symbol.split('.')
  15. m = context.resolve(parts[0])
  16. return reduce(getattr, parts[1:], m)
  17. else:
  18. return context.resolve(symbol)
  19. class LinkRowAction(BaseListRowAction):
  20. def __init__(self, icon_class, url, title=None):
  21. super(LinkRowAction, self).__init__(title=title)
  22. self.url = url
  23. self.icon_class = icon_class
  24. def render(self, context, row_id, row):
  25. m = self._resolve_symbol(context, 'row_actions.link')
  26. if isinstance(self.url, string_types):
  27. url = self.url.format(row_id=row_id)
  28. else:
  29. url = self.url(self, row_id, row)
  30. return m(self, url)
  31. class EndpointLinkRowAction(BaseListRowAction):
  32. def __init__(self, icon_class, endpoint, title=None, id_arg='id', url_args=None):
  33. super(EndpointLinkRowAction, self).__init__(title=title)
  34. self.icon_class = icon_class
  35. self.endpoint = endpoint
  36. self.id_arg = id_arg
  37. self.url_args = url_args
  38. def render(self, context, row_id, row):
  39. m = self._resolve_symbol(context, 'row_actions.link')
  40. get_url = self._resolve_symbol(context, 'get_url')
  41. kwargs = dict(self.url_args) if self.url_args else {}
  42. kwargs[self.id_arg] = row_id
  43. url = get_url(self.endpoint, **kwargs)
  44. return m(self, url)
  45. class TemplateLinkRowAction(BaseListRowAction):
  46. def __init__(self, template_name, title=None):
  47. super(TemplateLinkRowAction, self).__init__(title=title)
  48. self.template_name = template_name
  49. def render(self, context, row_id, row):
  50. m = self._resolve_symbol(context, self.template_name)
  51. return m(self, row_id, row)
  52. class ViewRowAction(TemplateLinkRowAction):
  53. def __init__(self):
  54. super(ViewRowAction, self).__init__(
  55. 'row_actions.view_row',
  56. gettext('View Record'))
  57. class ViewPopupRowAction(TemplateLinkRowAction):
  58. def __init__(self):
  59. super(ViewPopupRowAction, self).__init__(
  60. 'row_actions.view_row_popup',
  61. gettext('View Record'))
  62. class EditRowAction(TemplateLinkRowAction):
  63. def __init__(self):
  64. super(EditRowAction, self).__init__(
  65. 'row_actions.edit_row',
  66. gettext('Edit Record'))
  67. class EditPopupRowAction(TemplateLinkRowAction):
  68. def __init__(self):
  69. super(EditPopupRowAction, self).__init__(
  70. 'row_actions.edit_row_popup',
  71. gettext('Edit Record'))
  72. class DeleteRowAction(TemplateLinkRowAction):
  73. def __init__(self):
  74. super(DeleteRowAction, self).__init__(
  75. 'row_actions.delete_row',
  76. gettext('Edit Record'))
  77. # Macro helper
  78. def macro(name):
  79. '''
  80. Jinja2 macro list column formatter.
  81. :param name:
  82. Macro name in the current template
  83. '''
  84. def inner(view, context, model, column):
  85. m = context.resolve(name)
  86. if not m:
  87. return m
  88. return m(model=model, column=column)
  89. return inner