Skip to content

Commit a359e09

Browse files
dbarellaJon Wayne Parrott
authored andcommitted
Refactor HTML response code (GoogleCloudPlatform#974)
* Refactor HTML response code Make the response writing more readable by pulling the HTML into a format string. * Inline the response template string Reduce some of the indirection. * Fix lint error
1 parent e6e8b76 commit a359e09

File tree

1 file changed

+30
-15
lines changed
  • appengine/standard/ndb/overview

1 file changed

+30
-15
lines changed

appengine/standard/ndb/overview/main.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
# [START all]
2424
import cgi
25+
import textwrap
2526
import urllib
2627

2728
from google.appengine.ext import ndb
@@ -48,23 +49,37 @@ def get(self):
4849
guestbook_name = self.request.get('guestbook_name')
4950
ancestor_key = ndb.Key("Book", guestbook_name or "*notitle*")
5051
greetings = Greeting.query_book(ancestor_key).fetch(20)
51-
52-
for greeting in greetings:
53-
self.response.out.write('<blockquote>%s</blockquote>' %
54-
cgi.escape(greeting.content))
5552
# [END query]
5653

57-
self.response.out.write("""
58-
<form action="/sign?%s" method="post">
59-
<div><textarea name="content" rows="3" cols="60"></textarea></div>
60-
<div><input type="submit" value="Sign Guestbook"></div>
61-
</form>
62-
<hr>
63-
<form>Guestbook name: <input value="%s" name="guestbook_name">
64-
<input type="submit" value="switch"></form>
65-
</body>
66-
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
67-
cgi.escape(guestbook_name)))
54+
greeting_blockquotes = []
55+
for greeting in greetings:
56+
greeting_blockquotes.append(
57+
'<blockquote>%s</blockquote>' % cgi.escape(greeting.content))
58+
59+
self.response.out.write(textwrap.dedent("""\
60+
<html>
61+
<body>
62+
{blockquotes}
63+
<form action="/sign?{sign}" method="post">
64+
<div>
65+
<textarea name="content" rows="3" cols="60">
66+
</textarea>
67+
</div>
68+
<div>
69+
<input type="submit" value="Sign Guestbook">
70+
</div>
71+
</form>
72+
<hr>
73+
<form>
74+
Guestbook name:
75+
<input value="{guestbook_name}" name="guestbook_name">
76+
<input type="submit" value="switch">
77+
</form>
78+
</body>
79+
</html>""").format(
80+
blockquotes='\n'.join(greeting_blockquotes),
81+
sign=urllib.urlencode({'guestbook_name': guestbook_name}),
82+
guestbook_name=cgi.escape(guestbook_name)))
6883

6984

7085
# [START submit]

0 commit comments

Comments
 (0)