old_contents: #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  
  __author__ = 'Jiayi Li'
  
  '''
  url handlers
  '''
  
  import re, time, json, logging, hashlib, base64, asyncio
  
  from coroweb import get, post
  
  from models import User, Comment, Blog, next_id
  
  @get('/test')
  async def test():
      users = await User.findAll()
      return {
          '__template__': 'test.html',
          'users': users
      }
      
  @get('/')
  async def index():
      summary = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
      blogs = [
          Blog(id='1', name='Test Blog', summary=summary, created_at=time.time()-120),
          Blog(id='2', name='Something New', summary=summary, created_at=time.time()-3600),
          Blog(id='3', name='Learn Swift', summary=summary, created_at=time.time()-7200)
      ]
      return {
          '__template__': 'blogs.html',
          'blogs': blogs
      }
  
new_contents: #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  
  __author__ = 'Jiayi Li'
  
  '''
  url handlers
  '''
  
  import re, time, json, logging, hashlib, base64, asyncio
  
  from coroweb import get, post
  
  from errors import APIValueError, APIResourceNotFoundError
  
  from models import User, Comment, Blog, next_id
  
  @get('/test')
  async def test():
      users = await User.findAll()
      return {
          '__template__': 'test.html',
          'users': users
      }
      
  @get('/')
  async def index():
      summary = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
      blogs = [
          Blog(id='1', name='Test Blog', summary=summary, created_at=time.time()-120),
          Blog(id='2', name='Something New', summary=summary, created_at=time.time()-3600),
          Blog(id='3', name='Learn Swift', summary=summary, created_at=time.time()-7200)
      ]
      return {
          '__template__': 'blogs.html',
          'blogs': blogs
      }
  
  @get('/register')
  async def register():
      return {
          '__template__': 'register.html'
      }
  
  @get('/api/users')
  async def api_get_users(*, name):
      users = await User.findAll(orderBy='created_at desc')
      for u in users:
          u.passwd = '******'
      return dict(users=users)
  
  _RE_EMAIL = re.compile(r'^[a-z0-9\.\-\_]+\@[a-z0-9\-\_]+(\.[a-z0-9\-\_]+){1,4}$')
  _RE_SHA1 = re.compile(r'^[0-9a-f]{40}$')
  
  @post('/api/users')
  async def api_register(*, email, name, passwd):
      if not name or not name.strip():
          raise APIValueError('name')
      if not email or not _RE_EMAIL.match(email):
          raise APIValueError('email')
      if not passwd or not _RE_SHA1.match(passwd):
          raise APIValueError('passwd')
      users = await User.findAll('email=?', [email])
      if len(users) > 0:
          raise APIError('register:failed', 'email', 'Email is already in use.')
      uid = next_id()
      sha1_passwd = '%s:%s' % (uid, passwd)
      user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://i1.pixiv.net/img-original/img/2016/06/26/22/41/07/57606024_p0.png')
      await user.save()
      r = web.Response()
      r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
      user.passwd = '******'
      r.content_type = 'application/json'
      r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
      return r
subject: add sign up api