--- a/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py Thu Feb 06 19:30:34 2014 +0100 +++ b/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py Fri Feb 07 18:21:09 2014 +0100 @@ -51,6 +51,7 @@ self.__findTemplateTag) mainMenu.addSeparator() mainMenu.addMenu(self.__initTagsMenu()) + mainMenu.addMenu(self.__initFiltersMenu()) def __initTagsMenu(self): """ @@ -159,6 +160,76 @@ self.__tagsMenu = menu return menu + def __initFiltersMenu(self): + """ + Private method to initialize the filters menu. + + @return generated menu (QMenu) + """ + menu = QMenu(self.tr("Filters")) + menu.addAction( + self.tr("add - Add variable or string"), + lambda: self.__applyTemplate("add")) + menu.addAction( + self.tr("addslashes - Add slashes before quotes"), + lambda: self.__applyTemplate("addslashes")) + menu.addSeparator() + menu.addAction( + self.tr("capfirst - Capitalize first character"), + lambda: self.__applyTemplate("capfirst")) + menu.addAction( + self.tr("center - Center value"), + lambda: self.__applyTemplate("center")) + menu.addAction( + self.tr("cut - Cut characters"), + lambda: self.__applyTemplate("cut")) + menu.addSeparator() + menu.addAction( + self.tr("date - Format date"), + lambda: self.__applyTemplate("date")) + menu.addAction( + self.tr("default - Use dafault if False"), + lambda: self.__applyTemplate("default")) + menu.addAction( + self.tr("default_if_none - Use default if None"), + lambda: self.__applyTemplate("default_if_none")) + menu.addAction( + self.tr("dictsort - Sort dictionaries"), + lambda: self.__applyTemplate("dictsort")) + menu.addAction( + self.tr("dictsortreversed - Sort dictionaries reversed"), + lambda: self.__applyTemplate("dictsortreversed")) + menu.addAction( + self.tr("divisibleby - Check divisibility"), + lambda: self.__applyTemplate("divisibleby")) + menu.addSeparator() + menu.addAction( + self.tr("escape - Escape as HTML"), + lambda: self.__applyTemplate("escape")) + menu.addAction( + self.tr("escapejs - Escape as JavaScript"), + lambda: self.__applyTemplate("escapejs")) + menu.addSeparator() + menu.addAction( + self.tr("filesizeformat - Format file sizes"), + lambda: self.__applyTemplate("filesizeformat")) + menu.addAction( + self.tr("first - First item of a list"), + lambda: self.__applyTemplate("first")) + menu.addAction( + self.tr("fix_ampersands - Replace ampersands"), + lambda: self.__applyTemplate("fix_ampersands")) + menu.addAction( + self.tr("floatformat - Format floating numbers"), + lambda: self.__applyTemplate("floatformat")) + menu.addAction( + self.tr("force_escape - Escape as HTML immediately"), + lambda: self.__applyTemplate("force_escape")) + menu.addSeparator() + + self.__filtersMenu = menu + return menu + def __findTemplateTag(self): """ Private slot to find a template tag and insert its text. @@ -435,6 +506,115 @@ data[0], selectedText) #################################################### + ## Template Filters ## + #################################################### + + elif tag == "add": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Add Variable or String"), + [self.tr("Variables or String to add:") + ], + ["variable"]) + if ok: + templateText = "|add:{0}".format(data[0]) + elif tag == "addslashes": + templateText = "|addslashes " + elif tag == "capfirst": + templateText = "|capfirst " + elif tag == "center": + width, ok = QInputDialog.getInt( + None, + self.tr("Center Value"), + self.tr("Enter width of the output:"), + 10, 1, 99, 1) + if ok: + templateText = '|center:"{0}"'.format(width) + elif tag == "cut": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Cut Characters"), + [self.tr("Characters to cut:") + ], + [" "]) + if ok: + templateText = '|cut:"{0}"'.format(data[0]) + elif tag == "date": + date, ok = QInputDialog.getItem( + None, + self.tr("Format Date"), + self.tr("Enter date format:"), + ["DATETIME_FORMAT", "SHORT_DATETIME_FORMAT", + "SHORT_DATE_FORMAT", "DATE_FORMAT"], + 0, True) + if ok: + templateText = '|date:"{0}" '.format(date) + elif tag == "default": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Default Value if False"), + [self.tr("Enter default value if result is False:") + ], + ["nothing"]) + if ok: + templateText = '|default:"{0}" '.format(data[0]) + elif tag == "default_if_none": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Default Value if None"), + [self.tr("Enter default value if result is None:") + ], + ["nothing"]) + if ok: + templateText = '|default:"{0}" '.format(data[0]) + elif tag == "dictsort": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Sort Dictionaries"), + [self.tr("Enter key to sort on:") + ], + ["key"]) + if ok: + templateText = '|dictsort:"{0}" '.format(data[0]) + elif tag == "dictsortreversed": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Sort Dictionaries reversed"), + [self.tr("Enter key to sort on:") + ], + ["key"]) + if ok: + templateText = '|dictsortreversed:"{0}" '.format(data[0]) + elif tag == "divisibleby": + divisor, ok = QInputDialog.getInt( + None, + self.tr("Check Divisibility"), + self.tr("Enter divisor value:"), + 2, 1, 99, 1) + if ok: + templateText = '|divisibleby:"{0}" '.format(divisor) + elif tag == "escape": + templateText = '|escape ' + elif tag == "escapejs": + templateText = '|escapejs ' + elif tag == "filesizeformat": + templateText = '|filesizeformat ' + elif tag == "first": + templateText = '|first ' + elif tag == "fix_ampersands": + templateText = '|fix_ampersands ' + elif tag == "floatformat": + decimals, ok = QInputDialog.getInt( + None, + self.tr("Format Floating Number"), + self.tr("Enter number of decimal places:"), + 2, -20, 20, 1) + if ok: + templateText = '|floatformat:"{0}" '.format(decimals) + elif tag == "force_escape": + templateText = '|force_escape ' + + #################################################### ## Fallback: return just the tag name ## ####################################################