GAE
とりあえず OSX 10.5 なナニではどーにもならん模様なので、自宅でびあんで云々すべく作業着手。とりあえず django なパケジを入れました。
# apt-get install python-django
で、dpkg -L で grep してみました。
# dpkg -L python-django|grep 'django/utils/simple' /usr/share/python-support/python-django/django/utils/simplejson /usr/share/python-support/python-django/django/utils/simplejson/decoder.py /usr/share/python-support/python-django/django/utils/simplejson/encoder.py /usr/share/python-support/python-django/django/utils/simplejson/scanner.py /usr/share/python-support/python-django/django/utils/simplejson/tool.py /usr/share/python-support/python-django/django/utils/simplejson/__init__.py #
この位さっくり確認できないと駄目ですわな。あとは appengine なナニが導入されているのかどうか。
とりあえず
OSX な環境からアプリをコピーしたいんですが、でびあん側では Dropbox 使える状態ではないな。カタめてメイルでナニする事に。
で、試しに以下な get を書いてみたら空なナニが出力された事を確認してます。
class ListHandler(webapp.RequestHandler): def get(self): list = Notepad.all() array = [] for item in list: titleList = dict() titleList['title'] = item.title titleList['id'] = item.key array.append(titleList) data = {'List' : array} self.response.content_type = "application/json" self.response.out.write(simplejson.dumps(data, ensure_ascii=False))
ブラウザ上では
{"List": []}
な出力確認。とりあえず一連の処理を作りこんでおいてサーバに上げる。ちなみに上記のソレですが、OSX 上なローカル環境では正常動作してない事を確認してます。環境作る面倒なのでスルーな方向。
とりあえず版
以下をサーバに upload してます。
#!/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() array = [] for item in list: titleList = dict() titleList['title'] = item.title titleList['id'] = item.key array.append(titleList) data = {"List" : array} self.response.content_type = "application/json" self.response.out.write(simplejson.dumps(data, ensure_ascii=False)) def post(self): list = Notepad.all() array = [] for item in list: titleList = dict() titleList['title'] = item.title titleList['id'] = item.key array.append(titleList) data = {"List" : array} self.response.content_type = "application/json" self.response.out.write(simplejson.dumps(data, ensure_ascii=False)) class FetchHandler(webapp.RequestHandler): # def get(self, key): def post(self, key): notepad = Notepad.get(key) item = dict() array = [] item['title'] = notepad.title item['id'] = notepad.key array.append(item) data = {'item' : array} self.response.content_type = "application/json" self.response.out.write(simplejson.dumps(data, 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) title = cgi.escape(self.request.get('title')) 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): def post(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()
どうなるやら。