Commits

Brent Tubbs committed 8021989

first working version

Comments (0)

Files changed (9)

 api_version: 1
 
 handlers:
+- url: /static
+  static_dir: static
+
 - url: .*
   script: main.py
+  login: required

default_data.yaml

+panelists:
+- name: Joe Schmoe
+  birthdate: 1979-04-17
+  points: 23980
+  status: active
+  comments: |
+    Joe sure has a lot of points.
+- name: Berry Phillips
+  birthdate: 1962-05-05
+  points: 3500
+  status: inactive
+  comments: |
+    This panelist hasn't taken any surveys in a while.
+- name: Francois Renault
+  birthdate: 2002-07-04
+  points: 98760
+  status: ineligible
+  comments: |
+    Francois is too young to participate in the panel.
 #!/usr/bin/env python
+from google.appengine.dist import use_library
+use_library('django', '1.2')
+
 import os
 import yaml
 
+from google.appengine.api import users
 from google.appengine.ext import webapp
 from google.appengine.ext.webapp import util
 from google.appengine.ext.webapp import template
+from google.appengine.ext import db
+
+from models import Account
 
 here = os.path.dirname(__file__)
 
-# define a couple template paths
-main_tmpl = os.path.join(here, 'templates', 'index.html')
-live_tmpl = os.path.join(here, 'templates', 'live.html')
+tmpl = os.path.join(here, 'templates', 'index.html')
 
-default_live = open(live_tmpl).read()
+def render(tmpl_text, context):
+    t = template.Template(tmpl_text)
+    c = template.Context(context)
+    result = t.render(c)
+    return result
 
-# load up some data to show.
-f = open(os.path.join(here, 'panelists.yaml'))
-panelists_yaml = f.read()
-panelists = yaml.safe_load(panelists_yaml)
+import cgi
 
-class MainHandler(webapp.RequestHandler):
+def clean_request(req):
+    cleaned = {}
+    for k, v in req.POST.items():
+        cleaned[k] = cgi.unescape(v)
+    return cleaned
+
+def get_account(user):
+    return db.Query(Account).filter('user = ', user).get() or Account(user=user)
+
+class NiceHandler(webapp.RequestHandler):
+    """
+    Provides a shorter way of saying self.response.out.write().
+    """
+    def out(self, text):
+        self.response.out.write(text)
+
+class MainHandler(NiceHandler):
     def get(self):
+        user = users.get_current_user()
+        account = get_account(user) 
         vars = {
-            'panelists_yaml': panelists_yaml,
-            'panelists': panelists,
-            'live_tmpl': default_live,
-            'live_rendered': template.render
+            'account': account,
         }
-        self.response.out.write(panelists_yaml)
+        result = template.render(tmpl, vars)
+        self.out(result)
 
+    def post(self):
+        user = users.get_current_user()
+        account = get_account(user) 
+        # save user's data and template
+        account.data = self.request.get('data')
+        account.template = self.request.get('template')
+        account.save()
+        # redirect to main page.
+        self.redirect('/')
+
+class TestPageHandler(NiceHandler):
+    def get(self):
+        # Render the live template with the panelist data, and return 
+        user = users.get_current_user()
+        account = get_account(user) 
+        self.out(render(account.template, yaml.safe_load(account.data)))
 
 def main():
-    application = webapp.WSGIApplication([('/', MainHandler)],
-                                         debug=True)
+    application = webapp.WSGIApplication([
+        ('/', MainHandler),
+        ('/rendered', TestPageHandler),
+    ], debug=True)
     util.run_wsgi_app(application)
 
 
+import os
+
 from google.appengine.ext import db
 
-class Panelist(db.Model):
-    name = db.StringProperty(required=True)
-    birthdate = db.DateProperty()
-    points = db.IntegerProperty(required=True, default=0)
-    status = db.StringProperty(required=True, choices=set(['active', 'inactive', 'ineligible']))
+here = os.path.dirname(__file__)
+panelists_yaml = open(os.path.join(here, 'default_data.yaml')).read()
+test_tmpl = open(os.path.join(here, 'templates', 'live.html')).read()
+
+class Account(db.Model):
+    user = db.UserProperty(required=True)
+    data = db.TextProperty(default=panelists_yaml)
+    template = db.TextProperty(default=test_tmpl)

panelists.yaml

-panelists:
-- name: Joe Schmoe
-  birthdate: 1979-04-17
-  points: 23980
-  status: active
-  comments: |
-    Joe sure has a lot of points.
-- name: Berry Phillips
-  birthdate: 1962-05-05
-  points: 3500
-  status: inactive
-  comments: |
-    This panelist hasn't taken any surveys in a while.
-- name: Francois Renault
-  birthdate: 2002-07-04
-  points: 98760
-  status: ineligible
-  comments: |
-    Francois is too young to participate in the panel.
+html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outline:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}

static/tmplatur.css

+.half_block {
+    width: 500px;
+    height: 500px;
+    float: left;
+    margin-left: 10px;
+}
+
+.full_block {
+    width: 1010;
+    float: left;
+    margin-left: 10px;
+    min-height: 500px;
+}

templates/index.html

 <html>
 <head>
-    <meta http-equiv="content-type" content="text/html; charset=utf-8">
-
+    <link rel="stylesheet" href="/static/reset.css" type="text/css" media="screen" charset="utf-8">
+    <link rel="stylesheet" href="/static/tmplatur.css" type="text/css" media="screen" charset="utf-8">
     <title>Tmplatur</title>
-    
+    <style>
+    </style>
 </head>
 <body>
-    <pre>
-    {{ panelists_yaml }}
-    </pre>
+    Hi {{ account.user.nickname }}
+    <form method="post" >
+    <textarea name="data" class="half_block">{{ account.data }}</textarea>
+    <textarea name="template" class="half_block">{{ account.template }}</textarea>
+    <input type="submit" value="Submit" />
+    </form>
+    <div style="clear: both;"></div>
+    <iframe src="/rendered" class="full_block"></iframe>
 </body>
 </html>

templates/live.html

+<html>
+<head>
+    <title>Test Template</title>
+</head>
+<body>
+    <ul>
+    {% for dude in panelists %} 
+    <li>{{ dude.name }}</li>    
+    {% endfor %}
+    </ul>
+</body>
+</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.