ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py

changeset 5
e2b08694e945
parent 4
ba04ed0b14a1
child 6
5f1c0ebbdf5f
equal deleted inserted replaced
4:ba04ed0b14a1 5:e2b08694e945
5 5
6 """ 6 """
7 Module implementing the Django tags menu handler. 7 Module implementing the Django tags menu handler.
8 """ 8 """
9 9
10 import os
11
10 from PyQt4.QtCore import QObject 12 from PyQt4.QtCore import QObject
11 from PyQt4.QtGui import QMenu 13 from PyQt4.QtGui import QMenu, QInputDialog
12 14
13 from E5Gui.E5Application import e5App 15 from E5Gui.E5Application import e5App
16 from E5Gui import E5FileDialog
14 17
15 from .DjangoTagInputDialog import DjangoTagInputDialog 18 from .DjangoTagInputDialog import DjangoTagInputDialog
16 19
17 20
18 class DjangoTagsMenuHandler(QObject): 21 class DjangoTagsMenuHandler(QObject):
66 menu.addSeparator() 69 menu.addSeparator()
67 menu.addAction( 70 menu.addAction(
68 self.tr("comment - Multiline Comment"), 71 self.tr("comment - Multiline Comment"),
69 lambda: self.__applyTemplate("comment")) 72 lambda: self.__applyTemplate("comment"))
70 menu.addAction( 73 menu.addAction(
71 self.tr( "csrf_token - Cross Site Request Forgery Token"), 74 self.tr("csrf_token - Cross Site Request Forgery Token"),
72 lambda: self.__applyTemplate("csrf_token")) 75 lambda: self.__applyTemplate("csrf_token"))
73 menu.addAction( 76 menu.addAction(
74 self.tr("cycle - Cycle variables each time used"), 77 self.tr("cycle - Cycle variables each time used"),
75 lambda: self.__applyTemplate("cycle")) 78 lambda: self.__applyTemplate("cycle"))
76 menu.addSeparator() 79 menu.addSeparator()
103 self.tr("include - Render template given by variable"), 106 self.tr("include - Render template given by variable"),
104 lambda: self.__applyTemplate("includevariable")) 107 lambda: self.__applyTemplate("includevariable"))
105 menu.addAction( 108 menu.addAction(
106 self.tr("include - Render template given by file name"), 109 self.tr("include - Render template given by file name"),
107 lambda: self.__applyTemplate("includefile")) 110 lambda: self.__applyTemplate("includefile"))
111 menu.addSeparator()
112 menu.addAction(
113 self.tr("load - Load a custom template tag set"),
114 lambda: self.__applyTemplate("load"))
115 menu.addSeparator()
116 menu.addAction(
117 self.tr("now - Display current date and time"),
118 lambda: self.__applyTemplate("now"))
119 menu.addSeparator()
120 menu.addAction(
121 self.tr("regroup - Regroup list of alike objects by a common"
122 " attribute"),
123 lambda: self.__applyTemplate("regroup"))
124 menu.addSeparator()
125 menu.addAction(
126 self.tr("spaceless - Remove whitespace between HTML tags"),
127 lambda: self.__applyTemplate("spaceless"))
128 menu.addAction(
129 self.tr("ssi - Output contents of a given file into the page"),
130 lambda: self.__applyTemplate("ssi"))
131 menu.addAction(
132 self.tr("ssi - Output contents of a given file into the page"
133 " (dialog selection)"),
134 lambda: self.__applyTemplate("ssifile"))
135 menu.addSeparator()
136 menu.addAction(
137 self.tr("templatetag - Output syntax characters used for"
138 " template"),
139 lambda: self.__applyTemplate("templatetag"))
140 menu.addSeparator()
141 menu.addAction(
142 self.tr("url - Return an absolute path reference"),
143 lambda: self.__applyTemplate("url"))
144 menu.addAction(
145 self.tr("url...as - Return an absolute path reference"),
146 lambda: self.__applyTemplate("urlas"))
147 menu.addSeparator()
148 menu.addAction(
149 self.tr("verbatim - Output block contents without rendering"),
150 lambda: self.__applyTemplate("verbatim"))
151 menu.addSeparator()
152 menu.addAction(
153 self.tr("widthratio - Calculate width ratio"),
154 lambda: self.__applyTemplate("verbatim"))
155 menu.addAction(
156 self.tr("with - Cache a complex variable under a simpler name"),
157 lambda: self.__applyTemplate("verbatim"))
108 158
109 self.__tagsMenu = menu 159 self.__tagsMenu = menu
110 return menu 160 return menu
111 161
112 def __findTemplateTag(self): 162 def __findTemplateTag(self):
160 ## Template Tags ## 210 ## Template Tags ##
161 #################################################### 211 ####################################################
162 212
163 if tag == "autoescape": 213 if tag == "autoescape":
164 templateText = ( 214 templateText = (
165 "{% autoescape on %} " + selectedText + " {% endautoescape %}") 215 "{{% autoescape on %}} {0} {{% endautoescape %}}".format(
216 selectedText))
166 replace = True 217 replace = True
167 elif tag == "block": 218 elif tag == "block":
168 data, ok = DjangoTagInputDialog.getText( 219 data, ok = DjangoTagInputDialog.getText(
169 None, 220 None,
170 self.tr("Named Block"), 221 self.tr("Named Block"),
171 [self.tr("Enter block name:")], 222 [self.tr("Enter block name:")],
172 ["block_name"]) 223 ["block_name"])
173 if ok: 224 if ok:
174 templateText = ( 225 templateText = "{{% block {0} %}} {1} {{% endblock %}}".format(
175 "{% block " + data[0] + " %} " + selectedText + 226 data[0], selectedText)
176 " {% endblock %}")
177 replace = True 227 replace = True
178 elif tag == "comment": 228 elif tag == "comment":
179 templateText = ( 229 templateText = "{{% comment %}} {0} {{% endcomment %}}".format(
180 "{% comment %} " + selectedText + " {% endcomment %}") 230 selectedText)
181 replace = True 231 replace = True
182 elif tag == "csrf_token": 232 elif tag == "csrf_token":
183 templateText = ("{% csrf_token %}") 233 templateText = "{% csrf_token %}"
184 elif tag == "cycle": 234 elif tag == "cycle":
185 data, ok = DjangoTagInputDialog.getText( 235 data, ok = DjangoTagInputDialog.getText(
186 None, 236 None,
187 self.tr("Cycle Variables"), 237 self.tr("Cycle Variables"),
188 [self.tr("Enter items to cycle, space separated")], 238 [self.tr("Enter items to cycle, space separated")],
189 ["item1 item2 item3"]) 239 ["item1 item2 item3"])
190 if ok: 240 if ok:
191 templateText = ("{% cycle " + data[0] + " %} ") 241 templateText = "{{% cycle {0} %}}".format(data[0])
192 elif tag == "debug": 242 elif tag == "debug":
193 templateText = ("{% debug %}") 243 templateText = "{% debug %}"
194 elif tag == "extendsvariable": 244 elif tag == "extendsvariable":
195 data, ok = DjangoTagInputDialog.getText( 245 data, ok = DjangoTagInputDialog.getText(
196 None, 246 None,
197 self.tr("Extends"), 247 self.tr("Extends"),
198 [self.tr("Enter variable name:")], 248 [self.tr("Enter variable name:")],
199 ["variable"]) 249 ["variable"])
200 if ok: 250 if ok:
201 templateText = ('{% extends ' + data[0] + ' %} ') 251 templateText = "{{% extends {0} %}}".format(data[0])
202 elif tag == "extendsfile": 252 elif tag == "extendsfile":
203 data, ok = DjangoTagInputDialog.getText( 253 data, ok = DjangoTagInputDialog.getText(
204 None, 254 None,
205 self.tr("Extends"), 255 self.tr("Extends"),
206 [self.tr("Enter parent file name:")], 256 [self.tr("Enter parent file name:")],
207 ["base.html"]) 257 ["base.html"])
208 if ok: 258 if ok:
209 templateText = ('{% extends "' + data[0] + '" %} ') 259 templateText = '{{% extends "{0}" %}}'.format(data[0])
210 elif tag == "filter": 260 elif tag == "filter":
211 data, ok = DjangoTagInputDialog.getText( 261 data, ok = DjangoTagInputDialog.getText(
212 None, 262 None,
213 self.tr("Tag Filters"), 263 self.tr("Tag Filters"),
214 [self.tr("Multiple filters with arguments, pipes separated:")], 264 [self.tr("Multiple filters with arguments, pipes separated:")],
215 ["lower|safe"]) 265 ["lower|safe"])
216 if ok: 266 if ok:
217 templateText = ( 267 templateText = (
218 "{% filter " + data[0] + " %} " + selectedText + 268 "{{% filter {0} %}} {1} {{% endfilter %}}".format(
219 " {% endfilter %}") 269 data[0], selectedText))
220 replace = True 270 replace = True
221 elif tag == "firstof": 271 elif tag == "firstof":
222 data, ok = DjangoTagInputDialog.getText( 272 data, ok = DjangoTagInputDialog.getText(
223 None, 273 None,
224 self.tr("First Of"), 274 self.tr("First Of"),
225 [self.tr("Enter multiple variables, space separated:"), 275 [self.tr("Enter multiple variables, space separated:"),
226 self.tr("Enter fallback value:")], 276 self.tr("Enter fallback value:")],
227 ["var1 var2", "fallback_value"]) 277 ["var1 var2", "fallback_value"])
228 if ok: 278 if ok:
229 templateText = ( 279 templateText = (
230 "{% filter force_escape %}{% firstof " + data[0] + 280 '{{% filter force_escape %}}{{% firstof {0} "{1}" %}}'
231 ' "' + data[1] + '" %} ' + selectedText + 281 ' {2} {{% endfilter %}}'.format(
232 " {% endfilter %}") 282 data[0], data[1], selectedText))
233 replace = True 283 replace = True
234 elif tag == "for": 284 elif tag == "for":
235 data, ok = DjangoTagInputDialog.getText( 285 data, ok = DjangoTagInputDialog.getText(
236 None, 286 None,
237 self.tr("For Loop"), 287 self.tr("For Loop"),
238 [self.tr("Enter variable to use for iteration:"), 288 [self.tr("Enter variable to use for iteration:"),
239 self.tr("Enter sequence to iterate over:")], 289 self.tr("Enter sequence to iterate over:")],
240 ["item", "values"]) 290 ["item", "values"])
241 if ok: 291 if ok:
242 templateText = ( 292 templateText = (
243 "{% for " + data[0] + " in " + data[1] + 293 "{{% for {0} in {1} %}} {2} {{% endfor %}}".format(
244 " %} " + selectedText + " {% endfor %}") 294 data[0], data[1], selectedText))
245 replace = True 295 replace = True
246 elif tag == "for...empty": 296 elif tag == "for...empty":
247 data, ok = DjangoTagInputDialog.getText( 297 data, ok = DjangoTagInputDialog.getText(
248 None, 298 None,
249 self.tr("For Loop"), 299 self.tr("For Loop"),
251 self.tr("Enter sequence to iterate over:"), 301 self.tr("Enter sequence to iterate over:"),
252 self.tr("Enter output to use if loop is empty:")], 302 self.tr("Enter output to use if loop is empty:")],
253 ["item", "values", '"Nothing."']) 303 ["item", "values", '"Nothing."'])
254 if ok: 304 if ok:
255 templateText = ( 305 templateText = (
256 "{% for " + data[0] + " in " + data[1] + " %} " + 306 "{{% for {0} in {1} %}} {2} {{% empty %}} {3}"
257 selectedText + " {% empty %} " + data[2] + " {% endfor %}") 307 " {{% endfor %}}".format(
308 data[0], data[1], selectedText, data[2]))
258 replace = True 309 replace = True
259 elif tag == "includevariable": 310 elif tag == "includevariable":
260 data, ok = DjangoTagInputDialog.getText( 311 data, ok = DjangoTagInputDialog.getText(
261 None, 312 None,
262 self.tr("Include"), 313 self.tr("Include"),
263 [self.tr("Enter variable name:")], 314 [self.tr("Enter variable name:")],
264 ["variable"]) 315 ["variable"])
265 if ok: 316 if ok:
266 templateText = ('{% include ' + data[0] + ' %} ') 317 templateText = '{{% include {0} %}}'.format(data[0])
267 elif tag == "includefile": 318 elif tag == "includefile":
268 data, ok = DjangoTagInputDialog.getText( 319 data, ok = DjangoTagInputDialog.getText(
269 None, 320 None,
270 self.tr("Include"), 321 self.tr("Include"),
271 [self.tr("Enter file name:")], 322 [self.tr("Enter file name:")],
272 ["other.html"]) 323 ["other.html"])
273 if ok: 324 if ok:
274 templateText = ('{% include "' + data[0] + '" %} ') 325 templateText = '{{% include "{0}" %}}'.format(data[0])
326 elif tag == "load":
327 data, ok = DjangoTagInputDialog.getText(
328 None,
329 self.tr("Load"),
330 [self.tr("Enter template tag set to load:")],
331 ["foo bar"])
332 if ok:
333 templateText = '{{% load "{0}" %}}'.format(data[0])
334 elif tag == "now":
335 dateformat, ok = QInputDialog.getItem(
336 None,
337 self.tr("Now"),
338 self.tr("Current date time format:"),
339 ["DATETIME_FORMAT", "SHORT_DATETIME_FORMAT",
340 "SHORT_DATE_FORMAT", "DATE_FORMAT"],
341 0, False)
342 if ok:
343 templateText = '{{% now "{0}" %}}'.format(dateformat)
344 elif tag == "regroup":
345 data, ok = DjangoTagInputDialog.getText(
346 None,
347 self.tr("Regroup List"),
348 [self.tr("List Variable:"), self.tr("Common Attribute:"),
349 self.tr("Name of resulting list:")],
350 ["cities", "country", "country_list"])
351 if ok:
352 templateText = '{{% regroup {0} by {1} as {2} %}}'.format(
353 data[0], data[1], data[2])
354 elif tag == "spaceless":
355 templateText = "{{% spaceless %}} {0} {{% endspaceless %}}".format(
356 selectedText)
357 replace = True
358 elif tag == "ssi":
359 data, ok = DjangoTagInputDialog.getText(
360 None,
361 self.tr("SSI"),
362 [self.tr("Full path to template:")],
363 ["/tmp/ssi_template.html"])
364 if ok:
365 templateText = '{{% ssi "{0}" parsed %}}'.format(data[0])
366 elif tag == "ssifile":
367 ssi = E5FileDialog.getOpenFileName(
368 None,
369 self.tr("SSI"),
370 os.path.expanduser("~"),
371 self.tr("All Files (*)"))
372 if ssi:
373 templateText = '{{% ssi "{0}" parsed %}}'.format(ssi)
374 elif tag == "templatetag":
375 templatetag, ok = QInputDialog.getItem(
376 None,
377 self.tr("Template Tag"),
378 self.tr("Argument:"),
379 ["block", "variable", "brace", "comment"],
380 0, False)
381 if ok:
382 templateText = (
383 '{{% templatetag open{0} %}} {1} {{% templatetag'
384 ' close{0} %}}'.format(templatetag, selectedText))
385 replace = True
386 elif tag == "url":
387 data, ok = DjangoTagInputDialog.getText(
388 None,
389 self.tr("URL"),
390 [self.tr("View method name:"),
391 self.tr("Optional arguments (space separated):")],
392 ["path.to.some_view", "var1 var2"])
393 if ok:
394 if data[1]:
395 data[1] = ' ' + data[1]
396 templateText = '{{% url "{0}"{1} %}}'.format(
397 data[0], data[1])
398 elif tag == "urlas":
399 data, ok = DjangoTagInputDialog.getText(
400 None,
401 self.tr("URL...as"),
402 [self.tr("View method name:"),
403 self.tr("Optional arguments (space separated):"),
404 self.tr("URL name:")],
405 ["path.to.some_view", "var1 var2", "url_name"])
406 if ok:
407 if data[1]:
408 data[1] = ' ' + data[1]
409 templateText = '{{% url "{0}"{1} as {2} %}}'.format(
410 data[0], data[1], data[2])
411 elif tag == "verbatim":
412 templateText = "{{% verbatim %}} {0} {{% endverbatim %}}".format(
413 selectedText)
414 replace = True
415 elif tag == "widthratio":
416 data, ok = DjangoTagInputDialog.getText(
417 None,
418 self.tr("Width Ratio"),
419 [self.tr("Variable:"),
420 self.tr("Maximum Value:"),
421 self.tr("Maximum Width:")],
422 ["variable", "max_value", "max_width"])
423 if ok:
424 templateText = "{{% widthratio {0} {1} {2} %}}".format(
425 data[0], data[1], data[2])
426 elif tag == "with":
427 data, ok = DjangoTagInputDialog.getText(
428 None,
429 self.tr("Cache Variables"),
430 [self.tr("Variables to cache as key=value (space separated):")
431 ],
432 ["variable1=foo.bar variable2=bar.baz"])
433 if ok:
434 templateText = '{{% with {0} %}} {1} {{% endwith %}}'.format(
435 data[0], selectedText)
275 436
276 #################################################### 437 ####################################################
277 ## Fallback: return just the tag name ## 438 ## Fallback: return just the tag name ##
278 #################################################### 439 ####################################################
279 440

eric ide

mercurial