diff -r ba04ed0b14a1 -r e2b08694e945 ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py --- a/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py Wed Feb 05 19:13:12 2014 +0100 +++ b/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py Thu Feb 06 19:29:47 2014 +0100 @@ -7,10 +7,13 @@ Module implementing the Django tags menu handler. """ +import os + from PyQt4.QtCore import QObject -from PyQt4.QtGui import QMenu +from PyQt4.QtGui import QMenu, QInputDialog from E5Gui.E5Application import e5App +from E5Gui import E5FileDialog from .DjangoTagInputDialog import DjangoTagInputDialog @@ -68,7 +71,7 @@ self.tr("comment - Multiline Comment"), lambda: self.__applyTemplate("comment")) menu.addAction( - self.tr( "csrf_token - Cross Site Request Forgery Token"), + self.tr("csrf_token - Cross Site Request Forgery Token"), lambda: self.__applyTemplate("csrf_token")) menu.addAction( self.tr("cycle - Cycle variables each time used"), @@ -105,6 +108,53 @@ menu.addAction( self.tr("include - Render template given by file name"), lambda: self.__applyTemplate("includefile")) + menu.addSeparator() + menu.addAction( + self.tr("load - Load a custom template tag set"), + lambda: self.__applyTemplate("load")) + menu.addSeparator() + menu.addAction( + self.tr("now - Display current date and time"), + lambda: self.__applyTemplate("now")) + menu.addSeparator() + menu.addAction( + self.tr("regroup - Regroup list of alike objects by a common" + " attribute"), + lambda: self.__applyTemplate("regroup")) + menu.addSeparator() + menu.addAction( + self.tr("spaceless - Remove whitespace between HTML tags"), + lambda: self.__applyTemplate("spaceless")) + menu.addAction( + self.tr("ssi - Output contents of a given file into the page"), + lambda: self.__applyTemplate("ssi")) + menu.addAction( + self.tr("ssi - Output contents of a given file into the page" + " (dialog selection)"), + lambda: self.__applyTemplate("ssifile")) + menu.addSeparator() + menu.addAction( + self.tr("templatetag - Output syntax characters used for" + " template"), + lambda: self.__applyTemplate("templatetag")) + menu.addSeparator() + menu.addAction( + self.tr("url - Return an absolute path reference"), + lambda: self.__applyTemplate("url")) + menu.addAction( + self.tr("url...as - Return an absolute path reference"), + lambda: self.__applyTemplate("urlas")) + menu.addSeparator() + menu.addAction( + self.tr("verbatim - Output block contents without rendering"), + lambda: self.__applyTemplate("verbatim")) + menu.addSeparator() + menu.addAction( + self.tr("widthratio - Calculate width ratio"), + lambda: self.__applyTemplate("verbatim")) + menu.addAction( + self.tr("with - Cache a complex variable under a simpler name"), + lambda: self.__applyTemplate("verbatim")) self.__tagsMenu = menu return menu @@ -162,7 +212,8 @@ if tag == "autoescape": templateText = ( - "{% autoescape on %} " + selectedText + " {% endautoescape %}") + "{{% autoescape on %}} {0} {{% endautoescape %}}".format( + selectedText)) replace = True elif tag == "block": data, ok = DjangoTagInputDialog.getText( @@ -171,16 +222,15 @@ [self.tr("Enter block name:")], ["block_name"]) if ok: - templateText = ( - "{% block " + data[0] + " %} " + selectedText + - " {% endblock %}") + templateText = "{{% block {0} %}} {1} {{% endblock %}}".format( + data[0], selectedText) replace = True elif tag == "comment": - templateText = ( - "{% comment %} " + selectedText + " {% endcomment %}") + templateText = "{{% comment %}} {0} {{% endcomment %}}".format( + selectedText) replace = True elif tag == "csrf_token": - templateText = ("{% csrf_token %}") + templateText = "{% csrf_token %}" elif tag == "cycle": data, ok = DjangoTagInputDialog.getText( None, @@ -188,9 +238,9 @@ [self.tr("Enter items to cycle, space separated")], ["item1 item2 item3"]) if ok: - templateText = ("{% cycle " + data[0] + " %} ") + templateText = "{{% cycle {0} %}}".format(data[0]) elif tag == "debug": - templateText = ("{% debug %}") + templateText = "{% debug %}" elif tag == "extendsvariable": data, ok = DjangoTagInputDialog.getText( None, @@ -198,7 +248,7 @@ [self.tr("Enter variable name:")], ["variable"]) if ok: - templateText = ('{% extends ' + data[0] + ' %} ') + templateText = "{{% extends {0} %}}".format(data[0]) elif tag == "extendsfile": data, ok = DjangoTagInputDialog.getText( None, @@ -206,7 +256,7 @@ [self.tr("Enter parent file name:")], ["base.html"]) if ok: - templateText = ('{% extends "' + data[0] + '" %} ') + templateText = '{{% extends "{0}" %}}'.format(data[0]) elif tag == "filter": data, ok = DjangoTagInputDialog.getText( None, @@ -215,8 +265,8 @@ ["lower|safe"]) if ok: templateText = ( - "{% filter " + data[0] + " %} " + selectedText + - " {% endfilter %}") + "{{% filter {0} %}} {1} {{% endfilter %}}".format( + data[0], selectedText)) replace = True elif tag == "firstof": data, ok = DjangoTagInputDialog.getText( @@ -227,9 +277,9 @@ ["var1 var2", "fallback_value"]) if ok: templateText = ( - "{% filter force_escape %}{% firstof " + data[0] + - ' "' + data[1] + '" %} ' + selectedText + - " {% endfilter %}") + '{{% filter force_escape %}}{{% firstof {0} "{1}" %}}' + ' {2} {{% endfilter %}}'.format( + data[0], data[1], selectedText)) replace = True elif tag == "for": data, ok = DjangoTagInputDialog.getText( @@ -240,8 +290,8 @@ ["item", "values"]) if ok: templateText = ( - "{% for " + data[0] + " in " + data[1] + - " %} " + selectedText + " {% endfor %}") + "{{% for {0} in {1} %}} {2} {{% endfor %}}".format( + data[0], data[1], selectedText)) replace = True elif tag == "for...empty": data, ok = DjangoTagInputDialog.getText( @@ -253,8 +303,9 @@ ["item", "values", '"Nothing."']) if ok: templateText = ( - "{% for " + data[0] + " in " + data[1] + " %} " + - selectedText + " {% empty %} " + data[2] + " {% endfor %}") + "{{% for {0} in {1} %}} {2} {{% empty %}} {3}" + " {{% endfor %}}".format( + data[0], data[1], selectedText, data[2])) replace = True elif tag == "includevariable": data, ok = DjangoTagInputDialog.getText( @@ -263,7 +314,7 @@ [self.tr("Enter variable name:")], ["variable"]) if ok: - templateText = ('{% include ' + data[0] + ' %} ') + templateText = '{{% include {0} %}}'.format(data[0]) elif tag == "includefile": data, ok = DjangoTagInputDialog.getText( None, @@ -271,7 +322,117 @@ [self.tr("Enter file name:")], ["other.html"]) if ok: - templateText = ('{% include "' + data[0] + '" %} ') + templateText = '{{% include "{0}" %}}'.format(data[0]) + elif tag == "load": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Load"), + [self.tr("Enter template tag set to load:")], + ["foo bar"]) + if ok: + templateText = '{{% load "{0}" %}}'.format(data[0]) + elif tag == "now": + dateformat, ok = QInputDialog.getItem( + None, + self.tr("Now"), + self.tr("Current date time format:"), + ["DATETIME_FORMAT", "SHORT_DATETIME_FORMAT", + "SHORT_DATE_FORMAT", "DATE_FORMAT"], + 0, False) + if ok: + templateText = '{{% now "{0}" %}}'.format(dateformat) + elif tag == "regroup": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Regroup List"), + [self.tr("List Variable:"), self.tr("Common Attribute:"), + self.tr("Name of resulting list:")], + ["cities", "country", "country_list"]) + if ok: + templateText = '{{% regroup {0} by {1} as {2} %}}'.format( + data[0], data[1], data[2]) + elif tag == "spaceless": + templateText = "{{% spaceless %}} {0} {{% endspaceless %}}".format( + selectedText) + replace = True + elif tag == "ssi": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("SSI"), + [self.tr("Full path to template:")], + ["/tmp/ssi_template.html"]) + if ok: + templateText = '{{% ssi "{0}" parsed %}}'.format(data[0]) + elif tag == "ssifile": + ssi = E5FileDialog.getOpenFileName( + None, + self.tr("SSI"), + os.path.expanduser("~"), + self.tr("All Files (*)")) + if ssi: + templateText = '{{% ssi "{0}" parsed %}}'.format(ssi) + elif tag == "templatetag": + templatetag, ok = QInputDialog.getItem( + None, + self.tr("Template Tag"), + self.tr("Argument:"), + ["block", "variable", "brace", "comment"], + 0, False) + if ok: + templateText = ( + '{{% templatetag open{0} %}} {1} {{% templatetag' + ' close{0} %}}'.format(templatetag, selectedText)) + replace = True + elif tag == "url": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("URL"), + [self.tr("View method name:"), + self.tr("Optional arguments (space separated):")], + ["path.to.some_view", "var1 var2"]) + if ok: + if data[1]: + data[1] = ' ' + data[1] + templateText = '{{% url "{0}"{1} %}}'.format( + data[0], data[1]) + elif tag == "urlas": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("URL...as"), + [self.tr("View method name:"), + self.tr("Optional arguments (space separated):"), + self.tr("URL name:")], + ["path.to.some_view", "var1 var2", "url_name"]) + if ok: + if data[1]: + data[1] = ' ' + data[1] + templateText = '{{% url "{0}"{1} as {2} %}}'.format( + data[0], data[1], data[2]) + elif tag == "verbatim": + templateText = "{{% verbatim %}} {0} {{% endverbatim %}}".format( + selectedText) + replace = True + elif tag == "widthratio": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Width Ratio"), + [self.tr("Variable:"), + self.tr("Maximum Value:"), + self.tr("Maximum Width:")], + ["variable", "max_value", "max_width"]) + if ok: + templateText = "{{% widthratio {0} {1} {2} %}}".format( + data[0], data[1], data[2]) + elif tag == "with": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Cache Variables"), + [self.tr("Variables to cache as key=value (space separated):") + ], + ["variable1=foo.bar variable2=bar.baz"]) + if ok: + templateText = '{{% with {0} %}} {1} {{% endwith %}}'.format( + data[0], selectedText) #################################################### ## Fallback: return just the tag name ##