Guest User

Untitled

a guest
Jan 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import re
  2.  
  3. from django.core.management.base import BaseCommand
  4. from django.template import base, defaulttags
  5. from django.template.loader import get_template
  6. from django.template.base import TextNode
  7.  
  8. # Monkey patching
  9. render_tags = ('block ', 'endblock ', 'extends ', 'include ')
  10. BLOCK_TAG_START = []
  11. BLOCK_TAG_START2 = '%s (%s)' % (re.escape('{%'), '|'.join(render_tags))
  12.  
  13. for tag in render_tags:
  14. BLOCK_TAG_START.append('{%% %s' % tag)
  15.  
  16.  
  17. base.BLOCK_TAG_START = tuple(BLOCK_TAG_START)
  18. base.VARIABLE_TAG_START = '{{ block.super'
  19. base.tag_re = (re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' %
  20. (BLOCK_TAG_START2, re.escape(base.BLOCK_TAG_END),
  21. re.escape(base.VARIABLE_TAG_START), re.escape(base.VARIABLE_TAG_END),
  22. re.escape(base.COMMENT_TAG_START), re.escape(base.COMMENT_TAG_END))))
  23. # End monkey patching
  24.  
  25.  
  26. from django.template.base import VariableNode
  27.  
  28.  
  29. def do_load(parser, token):
  30. defaulttags.register.tags['load_original'](parser, token)
  31. return TextNode('{%% %s %%}' % token.contents)
  32.  
  33.  
  34. defaulttags.register.tags['load_original'] = defaulttags.register.tags['load']
  35. defaulttags.register.tags['load'] = do_load
  36.  
  37.  
  38. def render_patch(self, context):
  39. if self.filter_expression.token == 'block.super':
  40. return self.render_original(context)
  41. return "{{ %s }}" % self.filter_expression.token
  42.  
  43.  
  44. VariableNode.render_original = VariableNode.render
  45. VariableNode.render = render_patch
  46.  
  47.  
  48. class Command(BaseCommand):
  49. args = '<None>'
  50. help = 'Update employees'
  51.  
  52. def handle(self, *args, **options):
  53. unify_templates = ['admin/index.html']
  54.  
  55. for unify_template in unify_templates:
  56. template = get_template(unify_template)
  57. template_unify = template.render({})
  58. destination_name = template.origin.name.replace('.html', '.unify.html')
  59. print(destination_name)
  60. f = open(destination_name, 'w')
  61. f.write(template_unify)
  62. f.close()
Add Comment
Please, Sign In to add comment