| 1120 | | For convenience, let's now add a new small module to our `lib` package. Inside the `geddit/lib` directory, create a file named `ajax`, and add the following code to it: |
| | 1120 | We'll also need to make the display of an individual comment available as an HTML fragment, so let's factor it out into a separate template file, too. |
| | 1121 | |
| | 1122 | Add a template called `_comment.html` to the `geddit/templates` directory, and insert the following lines: |
| | 1123 | |
| | 1124 | {{{ |
| | 1125 | #!genshi |
| | 1126 | <li id="comment$num"> |
| | 1127 | <strong>${comment.username}</strong> at ${comment.time.strftime('%x %X')} |
| | 1128 | <blockquote>${comment.content}</blockquote> |
| | 1129 | </li> |
| | 1130 | }}} |
| | 1131 | |
| | 1132 | And in `geddit/templates/info.html` replace the `<ul>` element rendering the comments with the following: |
| | 1133 | |
| | 1134 | {{{ |
| | 1135 | #!genshi |
| | 1136 | <ul py:if="link.comments" class="comments"> |
| | 1137 | <xi:include href="_comment.html" |
| | 1138 | py:for="num, comment in enumerate(link.comments)" /> |
| | 1139 | </ul> |
| | 1140 | }}} |
| | 1141 | |
| | 1142 | Now we'll need to look into modifying the `Root.comment()` method so that it correctly deals with the AJAX requests we'll be adding. |
| | 1143 | |
| | 1144 | For convenience, let's add a new small module to our `lib` package. Inside the `geddit/lib` directory, create a file named `ajax`, and add the following code to it: |
| | 1156 | |
| | 1157 | Add an import of that module to the top of the `geddit/controller.py` file, replacing: |
| | 1158 | |
| | 1159 | {{{ |
| | 1160 | #!python |
| | 1161 | from geddit.lib import template |
| | 1162 | }}} |
| | 1163 | |
| | 1164 | with: |
| | 1165 | |
| | 1166 | {{{ |
| | 1167 | #!python |
| | 1168 | from geddit.lib import ajax, template |
| | 1169 | }}} |
| | 1170 | |
| | 1171 | Then, replace the `Root.comment()` method in `geddit/controller.py` with the following code: |
| | 1172 | |
| | 1173 | {{{ |
| | 1174 | #!python |
| | 1175 | @cherrypy.expose |
| | 1176 | @template.output('comment.html') |
| | 1177 | def comment(self, id, cancel=False, **data): |
| | 1178 | link = self.data.get(id) |
| | 1179 | if not link: |
| | 1180 | raise cherrypy.NotFound() |
| | 1181 | if cherrypy.request.method == 'POST': |
| | 1182 | if cancel: |
| | 1183 | raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
| | 1184 | form = CommentForm() |
| | 1185 | try: |
| | 1186 | data = form.to_python(data) |
| | 1187 | comment = link.add_comment(**data) |
| | 1188 | if not ajax.is_xhr(): |
| | 1189 | raise cherrypy.HTTPRedirect('/info/%s' % link.id) |
| | 1190 | return template.render('_comment.html', comment=comment, |
| | 1191 | num=len(link.comments)) |
| | 1192 | except Invalid, e: |
| | 1193 | errors = e.unpack_errors() |
| | 1194 | else: |
| | 1195 | errors = {} |
| | 1196 | |
| | 1197 | if ajax.is_xhr(): |
| | 1198 | stream = template.render('_form.html', link=link, errors=errors) |
| | 1199 | else: |
| | 1200 | stream = template.render(link=link, comment=None, errors=errors) |
| | 1201 | return stream | HTMLFormFiller(data=data) |
| | 1202 | }}} |