# HG changeset patch # User cmlenz # Date 1188579660 0 # Node ID dba522b4c31d7c6e2c575062a7750f2aa2b2acfc # Parent 1d23dddd6c2df33a4412497b70eef754fe1adb8b GenshiTutorial: implemented AJAX commenting. diff --git a/examples/tutorial/geddit/controller.py b/examples/tutorial/geddit/controller.py --- a/examples/tutorial/geddit/controller.py +++ b/examples/tutorial/geddit/controller.py @@ -10,7 +10,7 @@ from genshi.filters import HTMLFormFiller from geddit.form import LinkForm, CommentForm -from geddit.lib import template +from geddit.lib import ajax, template from geddit.model import Link, Comment @@ -77,14 +77,20 @@ try: data = form.to_python(data) comment = link.add_comment(**data) - raise cherrypy.HTTPRedirect('/info/%s' % link.id) + if not ajax.is_xhr(): + raise cherrypy.HTTPRedirect('/info/%s' % link.id) + return template.render('_comment.html', comment=comment, + num=len(link.comments)) except Invalid, e: errors = e.unpack_errors() else: errors = {} - return template.render(link=link, comment=None, - errors=errors) | HTMLFormFiller(data=data) + if ajax.is_xhr(): + stream = template.render('_form.html', link=link, errors=errors) + else: + stream = template.render(link=link, comment=None, errors=errors) + return stream | HTMLFormFiller(data=data) def main(filename): diff --git a/examples/tutorial/geddit/lib/ajax.py b/examples/tutorial/geddit/lib/ajax.py new file mode 100644 --- /dev/null +++ b/examples/tutorial/geddit/lib/ajax.py @@ -0,0 +1,5 @@ +import cherrypy + +def is_xhr(): + requested_with = cherrypy.request.headers.get('X-Requested-With') + return requested_with and requested_with.lower() == 'xmlhttprequest' diff --git a/examples/tutorial/geddit/lib/template.py b/examples/tutorial/geddit/lib/template.py --- a/examples/tutorial/geddit/lib/template.py +++ b/examples/tutorial/geddit/lib/template.py @@ -5,6 +5,8 @@ from genshi.output import encode, get_serializer from genshi.template import Context, TemplateLoader +from geddit.lib import ajax + loader = TemplateLoader( os.path.join(os.path.dirname(__file__), '..', 'templates'), auto_reload=True @@ -18,9 +20,10 @@ def decorate(func): def wrapper(*args, **kwargs): cherrypy.thread_data.template = loader.load(filename) - if method == 'html': - options.setdefault('doctype', 'html') - serializer = get_serializer(method, **options) + opt = options.copy() + if not ajax.is_xhr() and method == 'html': + opt.setdefault('doctype', 'html') + serializer = get_serializer(method, **opt) stream = func(*args, **kwargs) if not isinstance(stream, Stream): return stream diff --git a/examples/tutorial/geddit/model.py b/examples/tutorial/geddit/model.py --- a/examples/tutorial/geddit/model.py +++ b/examples/tutorial/geddit/model.py @@ -15,7 +15,9 @@ return '<%s %r>' % (type(self).__name__, self.title) def add_comment(self, username, content): - self.comments.append(Comment(username, content)) + comment = Comment(username, content) + self.comments.append(comment) + return comment class Comment(object): @@ -26,4 +28,4 @@ self.time = datetime.utcnow() def __repr__(self): - return '<%s>' % (type(self).__name__) + return '<%s by %r>' % (type(self).__name__, self.username) diff --git a/examples/tutorial/geddit/templates/_comment.html b/examples/tutorial/geddit/templates/_comment.html new file mode 100644 --- /dev/null +++ b/examples/tutorial/geddit/templates/_comment.html @@ -0,0 +1,4 @@ +
  • + ${comment.username} at ${comment.time.strftime('%x %X')} +
    ${comment.content}
    +
  • diff --git a/examples/tutorial/geddit/templates/_form.html b/examples/tutorial/geddit/templates/_form.html new file mode 100644 --- /dev/null +++ b/examples/tutorial/geddit/templates/_form.html @@ -0,0 +1,23 @@ +
    + + + + + + + + + +
    + + ${errors.username} +
    + +
    ${errors.content}
    +
    + + +
    +
    diff --git a/examples/tutorial/geddit/templates/comment.html b/examples/tutorial/geddit/templates/comment.html --- a/examples/tutorial/geddit/templates/comment.html +++ b/examples/tutorial/geddit/templates/comment.html @@ -13,24 +13,6 @@ at ${comment.time.strftime('%x %X')}:
    ${comment.content}

    -
    - - - - - - -
    - - ${errors.username} -
    - -
    ${errors.content}
    -
    -
    - - -
    -
    + diff --git a/examples/tutorial/geddit/templates/index.html b/examples/tutorial/geddit/templates/index.html --- a/examples/tutorial/geddit/templates/index.html +++ b/examples/tutorial/geddit/templates/index.html @@ -10,7 +10,6 @@

    News

    -

    Submit new link

    + +

    Submit new link

    diff --git a/examples/tutorial/geddit/templates/info.html b/examples/tutorial/geddit/templates/info.html --- a/examples/tutorial/geddit/templates/info.html +++ b/examples/tutorial/geddit/templates/info.html @@ -5,22 +5,60 @@ ${link.title} - + +

    ${link.title}

    ${link.url}
    posted by ${link.username} at ${link.time.strftime('%x %X')}
    -

    comment

    - + +

    comment

    diff --git a/examples/tutorial/geddit/templates/submit.html b/examples/tutorial/geddit/templates/submit.html --- a/examples/tutorial/geddit/templates/submit.html +++ b/examples/tutorial/geddit/templates/submit.html @@ -21,18 +21,19 @@ ${errors.url} - - + ${errors.title} + + + + + + -
    - - -