1. Jimmy Yuen Ho Wong
  2. pythonhk

Source

pythonhk / src / pythonhk / controllers.py

from cherrypy._cperror import HTTPRedirect
try:
    import simplejson as json
except ImportError:
    import json

import cherrypy

import pythonhk
from pythonhk import api


class UserController(object):

    def get_register(self):
        template = pythonhk.template_loader .get_template("register.html")
        return template.render()

    def post_register(self, display_name=None, email=None, password=None,
                      password_again=None):
        if not display_name:
            raise cherrypy.HTTPError(400, "Field: Display name is required.")

        if not email:
            raise cherrypy.HTTPError(400, "Field: Email is required.")

        if not password:
            raise cherrypy.HTTPError(400, "Field: Password is required.")

        if not password_again:
            raise cherrypy.HTTPError(400, "Field: Password again is required.")


user_routes = cherrypy.dispatch.RoutesDispatcher()
user_routes.mapper.explicit = False

user_routes.connect("register", "/register", UserController,
                    action="get_register", conditions={"method":["GET"]})
user_routes.connect("register", "/register", UserController,
                    action="post_register", conditions={"method":["POST"]})


class Root(object):

    @cherrypy.expose
    def index(self):
        template = pythonhk.template_loader.get_template("index.html")
        return template.render()

    user = UserController()