Commits

Miki Tebeka  committed 60b2e43

reply utility func and error

  • Participants
  • Parent commits 29160b3
  • Branches template-error

Comments (0)

Files changed (2)

                 {{ login }}
             </span>
         </div>
+
+        {% if short_url %}
+        <div class="short">
+            Url shortened to <a href="{{ short_url }}">{{ short_url }}</a>
+        </div>
+        {% endif %}
+
+        {% if error %}
+        <div class="error">
+            ERROR: {{ error | escape }}
+        </div>
+        {% endif %}
+
         <form method="post">
             <label for="url">Url:</label><input name="url" /> 
             <input type="submit" value="Shorten">
         </form>
+
         <div class="footer">
             G'day {{ user }}
         </div>
 
 class Home(webapp.RequestHandler):
     def get(self):
+        self.reply()
+
+    def post(self):
+        url = self.request.get('url') or ''
+        url = url.strip()
+        if not url:
+            self.reply(error='No URL')
+            return
+        self.reply(short_url=url)
+
+    def reply(self, **kw):
+        '''Fills index template with kw and other variables, send to client.'''
         template = get_template('index.html')
-        env = {
+        env = kw.copy()
+        env.update({
             'login' : self.login_html(),
             'user' : users.get_current_user() or 'stranger'
-        }
+        })
         self.response.out.write(template.render(**env))
 
-    def post(self):
-        url = self.request.get('url')
-        self.response.out.write('You asked to shorten: {0}'.format(url))
-
     def login_html(self):
         '''Login/Logout HTML sinppet.'''
         user = users.get_current_user()