Permalink
Browse files

pages to list and create sites

  • Loading branch information...
1 parent 5938f5a commit a8e24fad2861f6ae713db0780c224dc20474484c @evdb committed Apr 2, 2011
@@ -45,7 +45,7 @@ def setup(self):
qs['size'] = size
self.qs = qs
- logging.debug( self.qs )
+ # logging.debug( self.qs )
return 1
@@ -1,36 +1,90 @@
import os
import logging
+import re
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users
+from models import Site
+
class SiteHandler(webapp.RequestHandler):
- template_vars = {}
- template_path = ''
- user = None
+ def __init__(self):
+ super(SiteHandler, self).__init__()
+ self.vars = {}
+ self.template_path = 'you_need_to_set_template_path'
- def get(self, site_key):
-
- # add user to vars
- self.user = users.get_current_user()
- self.template_vars["user"] = self.user
- self.template_vars["logout_url"] = users.create_logout_url('/')
+ user = users.get_current_user()
+ if user:
+ self.user = user
+ self.vars["user"] = self.user
+ self.vars["logout_url"] = users.create_logout_url('/')
+
+ def post(self, site_key):
+ self.get(site_key)
+ def get(self, site_key):
+
# dispatch
- if site_key == '': self.list_sites()
- else : return self.error(404)
-
- # render template
- self.response.out.write(
- template.render(
- self.template_path,
- self.template_vars
+ if site_key == '' : self.list_sites()
+ elif site_key == 'add' : self.add_site()
+ else : self.show_site(site_key)
+
+ if self.response.status == 200:
+
+ logging.debug( self.vars)
+
+ # render template
+ self.response.out.write(
+ template.render(
+ self.template_path,
+ self.vars
+ )
)
- )
def list_sites(self):
self.template_path = 'templates/site_list.html'
- self.template_vars["sites"] = ["FIXME", "FIXME"]
+
+ # find all matching sites
+ sites = Site.all().filter('owner =', self.user).fetch(100)
+ self.vars["sites"] = sites
+
+ def add_site(self):
+ self.template_path = 'templates/site_add.html'
+
+ # get the site key
+ site_key = self.request.get('site_key').strip()
+ # site_key = re.sub('\s+', '', site_key)
+ self.vars['site_key'] = site_key
+
+ # return if there is no site_key, or we are not a post
+ if (not site_key) or self.request.method != 'POST' :
+ return
+
+ # check that the site_key is valid
+ if not Site.is_key_valid( site_key ):
+ self.vars['site_key_error'] = "site key is not valid"
+ return
+
+ # check that the site_key is valid
+ if Site.is_key_taken( site_key ):
+ self.vars['site_key_error'] = "site key already exists"
+ return
+
+ # ok to create
+ site = Site( site_key=site_key )
+ site.put()
+ self.redirect('/site/' + site_key )
+
+ def show_site(self, site_key):
+ self.template_path = 'templates/site_show.html'
+
+ site = Site.all().filter( 'site_key =', site_key).get()
+
+ if not site:
+ return self.error(404)
+
+ logging.debug( site )
+ self.vars['site'] = site
View
@@ -29,9 +29,9 @@ def main():
application = webapp.WSGIApplication(
[
- ( '/avatar/.*', AvatarHandler ),
+ ( '/avatar/.*', AvatarHandler ),
( '/site/([^/]*).*', SiteHandler ),
- ( '/.*', IndexHandler ),
+ ( '/.*', IndexHandler ),
],
debug=is_dev # only debug on dev server
)
@@ -0,0 +1,2 @@
+"""The application models"""
+from site import Site
@@ -0,0 +1,37 @@
+import logging
+import re
+
+from google.appengine.ext import db
+
+class SiteKeyNotValid(Exception):
+ pass
+
+class SiteKeyTaken(Exception):
+ pass
+
+
+def site_key_validator(value):
+ if not Site.is_key_valid(value):
+ raise SiteKeyNotValid()
+ return True
+
+
+class Site(db.Model):
+ site_key = db.StringProperty(required=True, validator=site_key_validator)
+ owner = db.UserProperty(auto_current_user_add=True)
+ created = db.DateTimeProperty(auto_now_add=True)
+
+ @classmethod
+ def is_key_valid( cls, key ):
+ if re.match( '[a-z][a-z0-9\_]{5,19}$', key ):
+ logging.debug( "key '%s' is valid " % key )
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_key_taken( cls, key ):
+ if cls.all().filter( 'site_key =', key ).count(1):
+ return True
+ else:
+ return False
@@ -0,0 +1,22 @@
+{% extends "base.html" %}
+
+
+{% block title %}Add a site{% endblock %}
+
+
+{% block content %}
+
+<h1>Add a site</h1>
+
+<form action="/site/add" method="post">
+
+ {% if site_key_error %}
+ <div class="error">{{ site_key_error }}</div>
+ {% endif %}
+
+ <label for="site_key">site key:</label>
+ <input type="text" name="site_key" id="site_key" value="{{ site_key }}" />
+ <input type="submit" />
+</form>
+
+{% endblock %}
@@ -9,9 +9,9 @@
<h1>Your sites</h1>
{% for site in sites %}
-site
+ <a href="/site/{{ site.site_key }}">{{ site.site_key }}</a>
{% empty %}
-You have no sites - you should <a href="/site/new">create one</a>
+ You have no sites - you should <a href="/site/add">create one</a>
{% endfor %}
@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+
+
+{% block title %}{{ site.site_key }}{% endblock %}
+
+
+{% block content %}
+ <h1>{{ site.site_key }}</h1>
+{% endblock %}

0 comments on commit a8e24fa

Please sign in to comment.