Permalink
Browse files

- Initial commit

  • Loading branch information...
1 parent 0e0af99 commit 9979561dbd3472162aae5d693c62b5861c511b7a @daniloz committed May 1, 2012
Showing with 277 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +20 −0 app.yaml
  3. BIN favicon.ico
  4. +11 −0 index.yaml
  5. +130 −0 main.py
  6. +21 −0 templates/index.html
  7. +16 −0 templates/u2-rot13-form.html
  8. +67 −0 templates/u2-signup-form.html
  9. +10 −0 templates/u2-signup-welcome.html
View
@@ -0,0 +1,2 @@
+*.bak
+*.pyc
View
@@ -0,0 +1,20 @@
+application: dzf-test
+version: 2-1
+runtime: python27
+api_version: 1
+threadsafe: yes
+
+handlers:
+- url: /favicon\.ico
+ static_files: favicon.ico
+ upload: favicon\.ico
+
+- url: .*
+ script: main.app
+
+libraries:
+- name: webapp2
+ version: "2.5.1"
+
+- name: jinja2
+ version: "2.6"
View
Binary file not shown.
View
@@ -0,0 +1,11 @@
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run. If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED"). If you want to manage some indexes
+# manually, move them above the marker line. The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
View
130 main.py
@@ -0,0 +1,130 @@
+#!/usr/bin/env python
+#
+# Copyright 2007 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import webapp2
+import logging
+import jinja2
+import cgi
+import os
+import re
+
+template_dir = os.path.join(os.path.dirname(__file__), 'templates')
+jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
+ autoescape = True)
+
+def render_str(template, **params):
+ t = jinja_env.get_template(template)
+ return t.render(params)
+
+
+class BaseHandler(webapp2.RequestHandler):
+ links = dict(homeworks = [])
+
+ #def __init__(self, request, response, **kwargs):
+ def __init__(self, request, response):
+ #logging.info(kwargs)
+ #super(BaseHandler, self).__init__(request,response)
+ #if kwargs and kwargs['href'] and kwargs['caption']:
+ # self.links['homeworks'].append(dict(href=path, caption=caption))
+ self.initialize(request, response)
+ self.links['homeworks'].append(dict(href="/unit2/rot13",caption="Unit2: ROT13"))
+ self.links['homeworks'].append(dict(href="/unit2/signup",caption="Unit2: SignUp"))
+
+ def render(self, template, **kw):
+ self.response.out.write(render_str(template, **kw))
+
+ def write(self, *a, **kw):
+ self.response.out.write(*a, **kw)
+
+
+class Rot13Page(BaseHandler):
+ def get(self):
+ self.render('u2-rot13-form.html')
+
+ def post(self):
+ user_text = self.request.get('text')
+ text_rot13ed = user_text.encode('rot13')
+ params = dict(usertext = text_rot13ed);
+ self.render('u2-rot13-form.html', **params)
+
+
+USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
+def validate_username(username):
+ return username and USER_RE.match(username)
+
+PASS_RE = re.compile(r"^.{3,20}$")
+def validate_password(password):
+ return password and PASS_RE.match(password)
+
+EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
+def validate_email(email):
+ return not email or EMAIL_RE.match(email)
+
+
+class SignUpPage(BaseHandler):
+ def get(self):
+ self.render('u2-signup-form.html')
+
+ def post(self):
+ username = self.request.get('username')
+ password = self.request.get('password')
+ verify = self.request.get('verify')
+ email = self.request.get('email')
+
+ params = dict(username = username,
+ email = email)
+
+ has_error = False
+
+ if not validate_username(username):
+ params['username_error'] = "That's not a valid username."
+ has_error = True
+
+ if not validate_password(password):
+ params['password_error'] = "That wasn't a valid password."
+ has_error = True
+ elif password != verify:
+ params['verify_error'] = "Your passwords didn't match."
+ has_error = True
+
+ if not validate_email(email):
+ params['email_error'] = "That's not a valid email."
+ has_error = True
+
+ if has_error:
+ self.render('u2-signup-form.html', **params)
+ else:
+ self.redirect('/unit2/signup/welcome?username=' + username)
+
+class SignUpWelcomePage(BaseHandler):
+ def get(self):
+ username = self.request.get('username')
+ if validate_username(username):
+ self.render('u2-signup-welcome.html', username = username)
+ else:
+ self.redirect('/unit2/signup')
+
+class RootPage(BaseHandler):
+ def get(self):
+ self.render('index.html', **self.links)
+
+app = webapp2.WSGIApplication([('/',RootPage),
+ ('/unit2/rot13',Rot13Page),
+ ('/unit2/signup',SignUpPage),
+ #('/unit2/rot13',Rot13Page(None,None, dict(href="/unit2/rot13",caption="Unit2: ROT13"))),
+ #('/unit2/signup',SignUpPage(None,None, dict(href="/unit2/signup",caption="Unit2: SignUp"))),
+ ('/unit2/signup/welcome',SignUpWelcomePage)],
+ debug=True)
View
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>My Test Playground</title>
+ <style type="text/css">
+ h2 { padding-left:20px; }
+ ul { padding-left:60px; }
+ li { margin-bottom:5px; }
+ </style>
+ </head>
+
+ <body>
+ <h1>Welcome to my test playground</h1>
+ <h2>Udacity CS253 Homeworks</h2>
+ <ul>
+ {% for item in homeworks %}
+ <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
+ {% endfor %}
+ </ul>
+ </body>
+</html>
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+
+<html>
+ <head>
+ <title>Unit 2 Rot 13</title>
+ </head>
+
+ <body>
+ <h2>Enter some text to ROT13:</h2>
+ <form method="post">
+ <textarea name="text" style="height: 100px; width: 400px;">{{usertext}}</textarea>
+ <br>
+ <input type="submit">
+ </form>
+ </body>
+</html>
@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Sign Up</title>
+ <style type="text/css">
+ .label {text-align: right}
+ .error {color: red}
+ </style>
+ </head>
+
+ <body>
+ <h2>Signup</h2>
+ <form method="post">
+ <table>
+ <tr>
+ <td class="label">
+ Username
+ </td>
+ <td>
+ <input type="text" name="username" value="{{username}}">
+ </td>
+ <td class="error">
+ {{username_error}}
+ </td>
+ </tr>
+
+ <tr>
+ <td class="label">
+ Password
+ </td>
+ <td>
+ <input type="password" name="password" value="">
+ </td>
+ <td class="error">
+ {{password_error}}
+ </td>
+ </tr>
+
+ <tr>
+ <td class="label">
+ Verify Password
+ </td>
+ <td>
+ <input type="password" name="verify" value="">
+ </td>
+ <td class="error">
+ {{verify_error}}
+ </td>
+ </tr>
+
+ <tr>
+ <td class="label">
+ Email (optional)
+ </td>
+ <td>
+ <input type="text" name="email" value="{{email}}">
+ </td>
+ <td class="error">
+ {{email_error}}
+ </td>
+ </tr>
+ </table>
+
+ <input type="submit">
+ </form>
+ </body>
+</html>
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Welcome</title>
+ </head>
+
+ <body>
+ <h1>Welcome, {{username}}!</h1>
+ </body>
+</html>

0 comments on commit 9979561

Please sign in to comment.