/ Sweet home / Blog / Archives / Ajax sous Django : Python et Taconite /

Ajax sous Django : Python et Taconite

Parce que je vais en avoir besoin pour faire de l'Ajax sous Django (en association avec http_response), j'ai récris en Python ma classe PHP servant à construire les "command documents", soit les fichiers XML contenant les modification du DOM à apporter par le plugin Taconite de JQuery, en me basant sur minidom que l'on pourrait présenter comme étant l'équivalent de simpleXML en PHP. Un peu d'indulgence, pour l'instant je suis encore un touriste dans le monde de Python (conseils bienvenus toutefois):

# usage:
#
# t = Taconite()
#
# t.append("#toto","")
# t.remove("#tutu")
# t.js('alert("hello world");')
# t.toggleClass('blue','body')
# t.css("body","background-color","white")
# [...]
# print t.toprettyxml()

import xml.dom.minidom as dom

class Taconite(dom.Document):
  def __init__(self):
    dom.Document.__init__(self)
    taconite = self.createElement("taconite")
    self.appendChild(taconite)

  def __str__(self):
    return self.toxml(encoding="utf-8")

  def camelizeCssProperty(self,property):
    words = property.split("-")
    camelized = words[0].lower()
    for word in words[1:] :
      camelized = camelized + word[0].upper() + word[1:]
    return camelized

  def js(self,script):
    command = self.createElement("eval")
    js = self.createTextNode(script)
    command.appendChild(js)
    self.childNodes[0].appendChild(command)

  def changeContentCommand(self,method,selector,content):
    html_dom = dom.parseString(content)
    command = self.createElement(method)
    command.setAttribute("select",selector)
    command.appendChild(html_dom.childNodes[0])
    self.childNodes[0].appendChild(command)

  def changeStateCommand(self,action,selector):
    command = self.createElement(action)
    command.setAttribute("select",selector)
    self.childNodes[0].appendChild(command)

  def CssCommand(self,action,css_class,selector):
    command1 = self.createElement(action)
    command1.setAttribute("select",selector)
    command1.setAttribute("arg1",css_class)
    command2 = self.createElement(action)
    command2.setAttribute("select",selector)
    command2.setAttribute("value",css_class)
    self.childNodes[0].appendChild(command1)
    self.childNodes[0].appendChild(command2)

  def addClass(self,css_class,selector):
    self.CssCommand("addClass",css_class,selector)

  def removeClass(self,css_class,selector):
    self.CssCommand("remove",css_class,selector)

  def toggleClass(self,css_class,selector):
    self.CssCommand("toggleClass",css_class,selector)

  def append(self,selector,content):
    self.changeContentCommand("append",selector,content)

  def prepend(self,selector,content):
    self.changeContentCommand("prepend",selector,content)

  def before(self,selector,content):
    self.changeContentCommand("before",selector,content)

  def after(self,selector,content):
    self.changeContentCommand("after",selector,content)

  def wrap(self,selector,content):
    self.changeContentCommand("wrap",selector,content)

  def replace(self,selector,content):
    self.changeContentCommand("replace",selector,content)

  def replaceContent(self,selector,content):
    self.changeContentCommand("replaceContent",selector,content)

  def remove(self,selector):
    self.changeStateCommand("remove",selector)

  def show(self,selector):
    self.changeStateCommand("show",selector)

  def hide(self,selector):
    self.changeStateCommand("hide",selector)

  def removeContent(self,selector):
    self.changeStateCommand("empty",selector)

  def css(self,selector,property,value):
    command = self.createElement("css")
    command.setAttribute("select",selector)
    command.setAttribute("name",self.camelizeCssProperty(property))
    command.setAttribute("value",value)
    self.childNodes[0].appendChild(command)

Quand j'aurai un peu de temps je m'occuperai des commentaires au format pydoc. En attendant, JQuery + Django... ça devrait faire des étincelles.


Last update: 2010-09-05 07:16:06


<< Changement de dédiboite
Python toujours... >>
 

Comment this





Benchmark ! :-)

  1. dispatch
    time  : 0.0480
    memory: 544.53 kb
  2. cms\controllers\Index::norouteAction: find published page
    time  : 0.0049
    memory: 213.82 kb
  3. cms\controllers\Index::norouteAction: find template
    time  : 0.0001
    memory: 1.22 kb
  4. cms\controllers\Index::norouteAction: find layout
    time  : 0.0001
    memory: 1.13 kb
  5. cms\models\Page::getPublishedChildren
    time  : 0.0271
    memory: 373.38 kb
  6. cms\models\Page::getAncestors
    time  : 0.0088
    memory: 305.23 kb
  7. cms\models\Page::getParent
    time  : 0.0088
    memory: 303.64 kb
  8. cms\models\Page::getPublishedLeftSibling
    time  : 0.0040
    memory: 101.8 kb
  9. cms\models\Page::getPublishedRightSibling
    time  : 0.0039
    memory: 104.66 kb
  10. cms\models\Page::getPublishedComments
    time  : 0.0010
    memory: 28.01 kb