CRUD な i/f

以前、bot を作った時に作成してました。それを参考にしつつ simplejson 使う方向で。
あ、あと twitter 経由で以下なフォローを頂戴しておりました。

JSONArray とか完全に忘れてました。あと Adapter 云々なあたりも若干微妙。とりあえず微妙さ加減が猛烈ではありますが、大晦日な格闘技を見ながら以下をでっちあげてみました。

#!/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 os
import cgi
import wsgiref.handlers

from django.utils import simplejson

from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util

class Notepad(db.Model):
    title = db.StringProperty()
    body = db.StringProperty(multiline=True)

class ListHandler(webapp.RequestHandler):
    def get(self):
        list = Notepad.all()
        titleList = dict()
        data = dict()
        for item in list:
            titleList['title'] = item.title
            titleList['id'] = item.key

        data = {'List' : data}
        self.response.content_type = "application/json"
        simplejson.dump(data, self.response.out, ensure_ascii=False)

class FetchHandler(webapp.RequestHandler):
    def get(self, key):
        notepad = Notepad.get(key)
        item = dict()
        data = dict()
        item['title'] = notepad.title
        item['id'] = notepad.key
        data = {'item' : data}
        self.response.content_type = "application/json"
        simplejson.dump(data, self.response.out, ensure_ascii=False)

class AddHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Hello world!')

    def post(self):
        try:
            notepad = Notepad()
            title = cgi.escape(self.request.get('title'))
            body = cgi.escape(self.request.get('body'))
            notepad.put()
            self.redirect('/list')
        except:
            self.redirect('/list')

class UpdateHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Hello world!')
    def post(self, key):
        try:
            notepad = Notepad.get(key)
            body = cgi.escape(self.request.get('body'))
            notepad.body = body
            notepad.put()
            self.redirect('/list')
        except:
            self.redirect('/list')

class DeleteHandler(webapp.RequestHandler):
    def get(self, key):
        try:
            notepad = Notepad.get(key)
            if notepad:
                notepad.delete()
            self.redirect('/list')
        except:
            self.redirect('/list')

class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Hello world!')


def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                          ('/list', ListHandler),
                                          ('/fetch/(\w+)', FetchHandler),
                                          ('/add/(\w+)', AddHandler),
                                          ('/update/(\w+)', UpdateHandler),
                                          ('/delete/(\w+)', DeleteHandler),
                                          ],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

端末側がなんとかならないと試験できません。上記な実装をサーバ側の仕様って事にしといて、端末な実装を検討着手な 23:07 (とほほ

なんとなく

戻す JSON が微妙な気がしてるんですが、そのあたりは別途って事でご容赦下さいませ。