GAE Test code
@@ -0,0 +1,8 @@ | ||
1 | +application: wakimoto-test | |
2 | +version: 1 | |
3 | +runtime: python | |
4 | +api_version: 1 | |
5 | + | |
6 | +handlers: | |
7 | +- url: /.* | |
8 | + script: sample.py |
@@ -0,0 +1,23 @@ | ||
1 | +indexes: | |
2 | + | |
3 | +# AUTOGENERATED | |
4 | + | |
5 | +# This index.yaml is automatically updated whenever the dev_appserver | |
6 | +# detects that a new type of query is run. If you want to manage the | |
7 | +# index.yaml file manually, remove the above marker line (the line | |
8 | +# saying "# AUTOGENERATED"). If you want to manage some indexes | |
9 | +# manually, move them above the marker line. The index.yaml file is | |
10 | +# automatically uploaded to the admin console when you next deploy | |
11 | +# your application using appcfg.py. | |
12 | + | |
13 | +- kind: MyData | |
14 | + properties: | |
15 | + - name: user | |
16 | + - name: create_time | |
17 | + direction: desc | |
18 | + | |
19 | +- kind: MyNode | |
20 | + properties: | |
21 | + - name: user | |
22 | + - name: create_time | |
23 | + direction: desc |
@@ -0,0 +1,84 @@ | ||
1 | +# -*- coding: utf-8 -*- | |
2 | +import os | |
3 | +from google.appengine.ext.webapp import template | |
4 | +from google.appengine.ext import webapp | |
5 | +from google.appengine.ext.webapp.util import run_wsgi_app | |
6 | + | |
7 | +from google.appengine.ext import db | |
8 | + | |
9 | +from google.appengine.api import users | |
10 | + | |
11 | +#自己参照型のツリーノード | |
12 | +class MyNode(db.Model): | |
13 | + user = db.UserProperty(required=True) | |
14 | + parent_node = db.SelfReferenceProperty(required=False) | |
15 | + title = db.StringProperty(required=True,multiline=False) | |
16 | + content = db.StringProperty(multiline=True) | |
17 | + create_time = db.DateTimeProperty(auto_now_add=True) | |
18 | + update_time = db.DateTimeProperty(auto_now_add=True) | |
19 | + | |
20 | +class MainPage(webapp.RequestHandler): | |
21 | + | |
22 | +#----------------------------------------------------------------------------------- | |
23 | +#ライブラリ | |
24 | +#あとでどっか移動させる | |
25 | + | |
26 | + #parentがないノードは一つだけという仕様 | |
27 | + def get_root_node(self, user): | |
28 | + node = MyNode.all().filter('user', user).filter('parent',None).get() | |
29 | + if node == None: | |
30 | + node = MyNode(user=user,title='root',content='',parent=None) | |
31 | + node.save() | |
32 | + return node | |
33 | + | |
34 | +#------------------------------------------------------------------------------------ | |
35 | + | |
36 | + def get(self): | |
37 | + user = users.get_current_user() | |
38 | + if user == None: | |
39 | + self.redirect(users.create_login_url(self.request.uri)) | |
40 | + return | |
41 | + | |
42 | + nodes = MyNode.all().filter('user', user).order('-create_time').fetch(10, 0) | |
43 | + logouturl = users.create_logout_url(self.request.uri) | |
44 | + params = {'nodes':nodes,'message':'ああああ','logouturl':logouturl} | |
45 | + fpath = os.path.join(os.path.dirname(__file__),'views','sample.html') | |
46 | + html = template.render(fpath,params) | |
47 | + self.response.headers['Content-Type'] = 'text/html' | |
48 | + self.response.out.write(html) | |
49 | + | |
50 | + def post(self): | |
51 | + user = users.get_current_user() | |
52 | + title = self.request.get('title') | |
53 | + content = self.request.get('content') | |
54 | + parent_id = self.request.get('parent_id') | |
55 | + | |
56 | + #タイトル内が/で区切られていたらその階層の親を作る | |
57 | + title_tree = title.split('/') | |
58 | + #親の確認or生成 | |
59 | + if parent_id=="0": | |
60 | + parent_node = self.get_root_node(user) | |
61 | + else: | |
62 | + parent_node = MyNode.get_by_id(long(parent_id)) | |
63 | + #parent_idからたどってその子の中に同名ノードがある場合のみそれを目的の親ノードと見なす | |
64 | + for i in range (0,len(title_tree)-2): | |
65 | + p_node = MyNode.all().filter('user', user).filter('parent_node',parent_node).filter('title',title_tree[i]).get() | |
66 | + if p_node == None: | |
67 | + p_node = MyNode(user=user,title=title_tree[i],content='',parent_node=parent_node) | |
68 | + p_node.save() | |
69 | + parent_node = p_node | |
70 | + node = MyNode(user=user,title=title_tree[len(title_tree)-1],content=content,parent_node=parent_node) | |
71 | + node.save() | |
72 | + self.redirect('/') | |
73 | + | |
74 | + | |
75 | +application = webapp.WSGIApplication([ | |
76 | + ('/', MainPage), | |
77 | + ], debug=True) | |
78 | + | |
79 | + | |
80 | +def main(): | |
81 | + run_wsgi_app(application) | |
82 | + | |
83 | +if __name__ == "__main__": | |
84 | + main() |
@@ -0,0 +1,51 @@ | ||
1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
2 | + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
3 | +<html xmlns="http://www.w3.org/1999/xhtml" | |
4 | + xml:lang="ja" lang="ja"> | |
5 | + <head> | |
6 | + <meta http-equiv="Content-Type" | |
7 | + content="text/html; charset=UTF-8" /> | |
8 | + <title>Home</title> | |
9 | + </head> | |
10 | + <body> | |
11 | + <h1>Home Page</h1> | |
12 | + <p>{{message}}</p> | |
13 | + <form method="post" action="/"> | |
14 | + <table> | |
15 | + <tr> | |
16 | + <th>タイトル:</th> | |
17 | + <td><input type="text" name="title" /></td> | |
18 | + </tr> | |
19 | + <tr> | |
20 | + <th>本文:</th> | |
21 | + <td><textarea name="content"></textarea></td> | |
22 | + </tr> | |
23 | + <tr><th></th> | |
24 | + <td><input type="submit" value="送信" /></td></tr> | |
25 | + </table> | |
26 | + <input type="hidden" name="parent_id" value="0" /> | |
27 | + </form> | |
28 | + <hr /> | |
29 | + <table border="1" width="500px"> | |
30 | + <tr> | |
31 | + <th width="50px">id</th> | |
32 | + <th width="50px">parent_id</th> | |
33 | + <th width="150px">タイトル</th> | |
34 | + <th width="350px">本文</th> | |
35 | + <th width="150px">作成日</th> | |
36 | + <th width="150px">更新日</th> | |
37 | + </tr> | |
38 | + {% for node in nodes %} | |
39 | + <tr> | |
40 | + <td>{{node.key.id}}</td> | |
41 | + <td>{{node.parent_node.key.id}}</td> | |
42 | + <td>{{node.title}}</td> | |
43 | + <td>{{node.content}}</td> | |
44 | + <td>{{node.create_time}}</td> | |
45 | + <td>{{node.update_time}}</td> | |
46 | + </tr> | |
47 | + {% endfor %} | |
48 | + </table> | |
49 | + <a href="{{logouturl}}">logout</a> | |
50 | + </body> | |
51 | +</html> | |
\ No newline at end of file |