Guest User

Untitled

a guest
Jul 3rd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. from django.template import Node, TemplateSyntaxError
  2. from django.template.library import parse_bits, Library
  3. from django.shortcuts import render_to_string
  4.  
  5. register = Library()
  6.  
  7.  
  8. class Button(Node):
  9. """
  10. Usage::
  11.  
  12. {% button type="button" class="btn-primary" name="submit" icon="cog" %}
  13. Press me
  14. {% endbutton %}
  15.  
  16. All arguments are optional.
  17.  
  18. ``type``:
  19. - What kind of button to render. Possible values are "a", "button", and
  20. "submit", for `<a>`, `<button>`, and `<input type="submit">`
  21. respectively. Defaults to "a"
  22.  
  23. ``class``:
  24. - Any CSS class names for the button. See the wagtailstyleguide for all the
  25. possible classes. Defaults to the standard button class.
  26.  
  27. ``name``:
  28. - The name attribute for the button. Only applicable for "button" and
  29. "submit" button types. Defaults to no name.
  30.  
  31. ``icon``:
  32. - Used to add an icon to the button. See the wagtailstyleguide for all
  33. the possible icons. Defaults to no icon
  34. """
  35.  
  36. TAG_NAME = 'button'
  37.  
  38. types = {
  39. 'a': 'wagtailadmin/components/buttons/a.html',
  40. 'button': 'wagtailadmin/components/buttons/button.html',
  41. 'submit': 'wagtailadmin/components/buttons/submit.html',
  42. }
  43.  
  44. def __init__(self, inner, type_var, class_var, icon_var, name_var):
  45. self.type_var = type_var
  46. self.class_var = class_var
  47. self.icon_var = icon_var
  48. self.name_var = name_var
  49. self.inner_nodelist = inner
  50.  
  51. def render(self, context):
  52.  
  53. classes = []
  54. if self.class_var:
  55. classes.append(self.class_var.resolve(context))
  56. if self.icon_var:
  57. classes.append('icon-' + self.icon_var.resolve(context))
  58.  
  59. if self.type_var:
  60. button_type = self.type_var.resolve(context)
  61. if button_type not in self.types:
  62. raise TemplateSyntaxError("'%s' unknown button type %s" % (
  63. self.TAG_NAME, button_type))
  64. else:
  65. button_type = 'a'
  66.  
  67. template = self.types[button_type]
  68. return render_to_string(template, {
  69. 'class': ' '.join(classes),
  70. 'name': self.name_var.resolve(context) if self.name_var else '',
  71. 'type': button_type,
  72. })
  73.  
  74. @classmethod
  75. def handle(cls, parser, token):
  76. name = cls.TAG_NAME
  77.  
  78. bits = token.split_contents()[1:]
  79. # This is typically taken from getargspec, but we are doing funky things...
  80.  
  81. # All the possible arguments
  82. params = ['type', 'class', 'icon', 'name']
  83. # Everything has an empty default
  84. defaults = [(arg, None) for arg in params]
  85. # No *args or **kwargs
  86. varargs, varkw = None
  87.  
  88. args, kwargs = parse_bits(
  89. parser, bits, params, varargs, varkw, defaults,
  90. takes_context=False, function_name=name)
  91.  
  92. # Get the contents till {% endbutton %}
  93. inner = parser.parse(['end' + name])
  94. parser.delete_first_token()
  95.  
  96. return cls(inner, *args, **kwargs)
  97. register.tag(Button.TAG_NAME, Button.handle)
Advertisement
Add Comment
Please, Sign In to add comment