| /trunk/api_rpc.py r42 | /trunk/api_rpc.py r56 | ||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
|---|---|---|---|
| 2 | import wsgiref.handlers | 2 | import wsgiref.handlers |
| 3 | import xmlrpclib | 3 | import xmlrpclib |
| 4 | import sys | 4 | import sys |
| 5 | import cgi | 5 | import cgi |
| 6 | import base64 | 6 | import base64 |
| 7 | from datetime import datetime | 7 | from datetime import datetime |
| 8 | from SimpleXMLRPCServer import SimpleXMLRPCDispatcher | 8 | from SimpleXMLRPCServer import SimpleXMLRPCDispatcher |
| 9 | from functools import wraps | 9 | from functools import wraps |
| 10 | 10 | ||
| 11 | sys.path.append('modules') | 11 | sys.path.append('modules') |
| 12 | from base import * | 12 | from base import * |
| 13 | from model import * | 13 | from model import * |
| 14 | 14 | ||
| 15 | def checkauth(pos=1): | 15 | def checkauth(pos=1): |
| 16 | def _decorate(method): | 16 | def _decorate(method): |
| 17 | def _wrapper(*args, **kwargs): | 17 | def _wrapper(*args, **kwargs): |
| 18 | |||
| 18 | username = args[pos+0] | 19 | username = args[pos+0] |
| 19 | password = args[pos+1] | 20 | password = args[pos+1] |
| 20 | args = args[0:pos]+args[pos+2:] | 21 | args = args[0:pos]+args[pos+2:] |
| 21 | if not (username and password and g_blog.rpcuser==username) and (g_blog.rpcpassword==password): | 22 | if not (username and password and g_blog.rpcuser and g_blog.rpcpassword |
| 22 | raise ValueError("Authentication Failure") | 23 | and (g_blog.rpcuser==username) |
| 24 | and (g_blog.rpcpassword==password)): | ||
| 25 | raise ValueError("Authentication Failure") | ||
| 23 | return method(*args, **kwargs) | 26 | return method(*args, **kwargs) |
| 24 | 27 | ||
| 25 | return _wrapper | 28 | return _wrapper |
| 26 | return _decorate | 29 | return _decorate |
| 27 | 30 | ||
| 28 | def format_date(d): | 31 | def format_date(d): |
| 29 | if not d: return None | 32 | if not d: return None |
| 30 | return xmlrpclib.DateTime(d.isoformat()) | 33 | return xmlrpclib.DateTime(d.isoformat()) |
| 31 | 34 | ||
| 32 | def entry_struct(entry): | 35 | def entry_struct(entry): |
| 33 | categories=[] | 36 | categories=[] |
| 34 | if entry.categorie_keys: | 37 | if entry.categorie_keys: |
| 35 | categories =[cate.name for cate in entry.categories] | 38 | categories =[cate.name for cate in entry.categories] |
| 36 | 39 | ||
| 37 | 40 | ||
| 38 | struct = { | 41 | struct = { |
| 39 | 'postid': entry.key().id(), | 42 | 'postid': entry.key().id(), |
| 40 | 'title': entry.title, | 43 | 'title': entry.title, |
| 41 | 'link': entry.fullurl(), | 44 | 'link': entry.fullurl(), |
| 42 | 'permaLink': entry.fullurl(), | 45 | 'permaLink': entry.fullurl(), |
| 43 | 'description': unicode(entry.content), | 46 | 'description': unicode(entry.content), |
| 44 | 'categories': categories, | 47 | 'categories': categories, |
| 45 | 'userid': 1, | 48 | 'userid': 1, |
| 46 | 'mt_keywords':','.join(entry.tags), | 49 | 'mt_keywords':','.join(entry.tags), |
| 47 | 'wp_slug':entry.slug, | 50 | 'wp_slug':entry.slug, |
| 48 | 'wp_page_order':entry.menu_order, | 51 | 'wp_page_order':entry.menu_order, |
| 49 | # 'mt_excerpt': '', | 52 | # 'mt_excerpt': '', |
| 50 | # 'mt_text_more': '', | 53 | # 'mt_text_more': '', |
| 51 | # 'mt_allow_comments': 1, | 54 | # 'mt_allow_comments': 1, |
| 52 | # 'mt_allow_pings': 1} | 55 | # 'mt_allow_pings': 1} |
| 53 | } | 56 | } |
| 54 | if entry.date: | 57 | if entry.date: |
| 55 | struct['dateCreated'] = format_date(entry.date) | 58 | struct['dateCreated'] = format_date(entry.date) |
| 56 | return struct | 59 | return struct |
| 57 | 60 | ||
| 58 | class Logger(db.Model): | 61 | class Logger(db.Model): |
| 59 | request = db.TextProperty() | 62 | request = db.TextProperty() |
| 60 | response = db.TextProperty() | 63 | response = db.TextProperty() |
| 61 | date = db.DateTimeProperty(auto_now_add=True) | 64 | date = db.DateTimeProperty(auto_now_add=True) |
| 62 | 65 | ||
| 63 | 66 | ||
| 64 | #------------------------------------------------------------------------------- | 67 | #------------------------------------------------------------------------------- |
| 65 | # blogger | 68 | # blogger |
| 66 | #------------------------------------------------------------------------------- | 69 | #------------------------------------------------------------------------------- |
| 67 | 70 | ||
| 68 | @checkauth() | 71 | @checkauth() |
| 69 | def blogger_getUsersBlogs(discard): | 72 | def blogger_getUsersBlogs(discard): |
| 70 | return [{'url' : g_blog.baseurl, 'blogid' : '001', 'blogName' : g_blog.title}] | 73 | return [{'url' : g_blog.baseurl, 'blogid' : '001', 'blogName' : g_blog.title}] |
| 71 | 74 | ||
| 72 | #------------------------------------------------------------------------------- | 75 | #------------------------------------------------------------------------------- |
| 73 | # metaWeblog | 76 | # metaWeblog |
| 74 | #------------------------------------------------------------------------------- | 77 | #------------------------------------------------------------------------------- |
| 75 | 78 | ||
| 76 | @checkauth() | 79 | @checkauth() |
| 77 | def metaWeblog_newPost(blogid, struct, publish): | 80 | def metaWeblog_newPost(blogid, struct, publish): |
| 78 | if struct.has_key('categories'): | 81 | if struct.has_key('categories'): |
| 79 | cates = struct['categories'] | 82 | cates = struct['categories'] |
| 80 | else: | 83 | else: |
| 81 | cates = [] | 84 | cates = [] |
| 82 | 85 | ||
| 83 | newcates=[] | 86 | newcates=[] |
| 84 | for cate in cates: | 87 | for cate in cates: |
| 85 | c=Category.all().filter('name =',cate) | 88 | c=Category.all().filter('name =',cate) |
| 86 | if c: | 89 | if c: |
| 87 | newcates.append(c[0].key()) | 90 | newcates.append(c[0].key()) |
| 88 | entry=Entry(title = struct['title'], | 91 | entry=Entry(title = struct['title'], |
| 89 | content = struct['description'], | 92 | content = struct['description'], |
| 90 | categorie_keys=newcates | 93 | categorie_keys=newcates |
| 91 | ) | 94 | ) |
| 92 | 95 | ||
| 93 | if struct.has_key('mt_text_more'): | 96 | if struct.has_key('mt_text_more'): |
| 94 | content=struct['mt_text_more'] | 97 | content=struct['mt_text_more'] |
| 95 | if content: | 98 | if content: |
| 96 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] | 99 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] |
| 97 | if struct.has_key('mt_keywords'): | 100 | if struct.has_key('mt_keywords'): |
| 98 | entry.settags(struct['mt_keywords']) | 101 | entry.settags(struct['mt_keywords']) |
| 99 | 102 | ||
| 100 | if struct.has_key('wp_slug'): | 103 | if struct.has_key('wp_slug'): |
| 101 | entry.slug=struct['wp_slug'] | 104 | entry.slug=struct['wp_slug'] |
| 102 | 105 | ||
| 103 | if struct.has_key('mt_excerpt'): | 106 | if struct.has_key('mt_excerpt'): |
| 104 | entry.excerpt=struct['mt_excerpt'] | 107 | entry.excerpt=struct['mt_excerpt'] |
| 105 | 108 | ||
| 106 | if publish: | 109 | if publish: |
| 107 | entry.publish(True) | 110 | entry.publish(True) |
| 108 | else: | 111 | else: |
| 109 | entry.save() | 112 | entry.save() |
| 110 | postid =entry.key().id() | 113 | postid =entry.key().id() |
| 111 | return str(postid) | 114 | return str(postid) |
| 112 | @checkauth() | 115 | @checkauth() |
| 113 | def metaWeblog_newMediaObject(postid,struct): | 116 | def metaWeblog_newMediaObject(postid,struct): |
| 114 | name=struct['name'] | 117 | name=struct['name'] |
| 115 | mtype=struct['type'] | 118 | mtype=struct['type'] |
| 116 | #logging.info( struct['bits']) | 119 | #logging.info( struct['bits']) |
| 117 | bits=db.Blob(str(struct['bits'])) | 120 | bits=db.Blob(str(struct['bits'])) |
| 118 | media=Media(name=name,mtype=mtype,bits=bits) | 121 | media=Media(name=name,mtype=mtype,bits=bits) |
| 119 | media.put() | 122 | media.put() |
| 120 | 123 | ||
| 121 | return {'url':g_blog.baseurl+'/media/'+str(media.key())} | 124 | return {'url':g_blog.baseurl+'/media/'+str(media.key())} |
| 122 | 125 | ||
| 123 | @checkauth() | 126 | @checkauth() |
| 124 | def metaWeblog_editPost(postid, struct, publish): | 127 | def metaWeblog_editPost(postid, struct, publish): |
| 125 | if struct.has_key('categories'): | 128 | if struct.has_key('categories'): |
| 126 | cates = struct['categories'] | 129 | cates = struct['categories'] |
| 127 | else: | 130 | else: |
| 128 | cates = [] | 131 | cates = [] |
| 129 | newcates=[] | 132 | newcates=[] |
| 130 | for cate in cates: | 133 | for cate in cates: |
| 131 | c=Category.all().filter('name =',cate).fetch(1) | 134 | c=Category.all().filter('name =',cate).fetch(1) |
| 132 | if c: | 135 | if c: |
| 133 | newcates.append(c[0].key()) | 136 | newcates.append(c[0].key()) |
| 134 | entry=Entry.get_by_id(int(postid)) | 137 | entry=Entry.get_by_id(int(postid)) |
| 135 | 138 | ||
| 136 | 139 | ||
| 137 | if struct.has_key('mt_keywords'): | 140 | if struct.has_key('mt_keywords'): |
| 138 | entry.settags(struct['mt_keywords']) | 141 | entry.settags(struct['mt_keywords']) |
| 139 | 142 | ||
| 140 | if struct.has_key('wp_slug'): | 143 | if struct.has_key('wp_slug'): |
| 141 | entry.slug=struct['wp_slug'] | 144 | entry.slug=struct['wp_slug'] |
| 142 | if struct.has_key('mt_excerpt'): | 145 | if struct.has_key('mt_excerpt'): |
| 143 | entry.excerpt=struct['mt_excerpt'] | 146 | entry.excerpt=struct['mt_excerpt'] |
| 144 | 147 | ||
| 145 | 148 | ||
| 146 | entry.title = struct['title'] | 149 | entry.title = struct['title'] |
| 147 | entry.content = struct['description'] | 150 | entry.content = struct['description'] |
| 148 | if struct.has_key('mt_text_more'): | 151 | if struct.has_key('mt_text_more'): |
| 149 | content=struct['mt_text_more'] | 152 | content=struct['mt_text_more'] |
| 150 | if content: | 153 | if content: |
| 151 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] | 154 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] |
| 152 | entry.categorie_keys=newcates | 155 | entry.categorie_keys=newcates |
| 153 | if publish: | 156 | if publish: |
| 154 | entry.publish(True) | 157 | entry.publish(True) |
| 155 | else: | 158 | else: |
| 156 | entry.save() | 159 | entry.save() |
| 157 | 160 | ||
| 158 | return True | 161 | return True |
| 159 | 162 | ||
| 160 | 163 | ||
| 161 | @checkauth() | 164 | @checkauth() |
| 162 | def metaWeblog_getCategories(blogid): | 165 | def metaWeblog_getCategories(blogid): |
| 163 | categories =Category.all() | 166 | categories =Category.all() |
| 164 | cates=[] | 167 | cates=[] |
| 165 | for cate in categories: | 168 | for cate in categories: |
| 166 | cates.append({ 'categoryId' : cate.key().id_or_name(), | 169 | cates.append({ 'categoryId' : cate.key().id_or_name(), |
| 167 | 'parentId':0, | 170 | 'parentId':0, |
| 168 | 'description':cate.name, | 171 | 'description':cate.name, |
| 169 | 'categoryName':cate.name, | 172 | 'categoryName':cate.name, |
| 170 | 'htmlUrl':'', | 173 | 'htmlUrl':'', |
| 171 | 'rssUrl':'' | 174 | 'rssUrl':'' |
| 172 | }) | 175 | }) |
| 173 | return cates | 176 | return cates |
| 174 | 177 | ||
| 175 | @checkauth() | 178 | @checkauth() |
| 176 | def metaWeblog_getPost(postid): | 179 | def metaWeblog_getPost(postid): |
| 177 | entry = Entry.get_by_id(int(postid)) | 180 | entry = Entry.get_by_id(int(postid)) |
| 178 | return entry_struct(entry) | 181 | return entry_struct(entry) |
| 179 | 182 | ||
| 180 | @checkauth() | 183 | @checkauth() |
| 181 | def metaWeblog_getRecentPosts(blogid, num): | 184 | def metaWeblog_getRecentPosts(blogid, num): |
| 182 | entries = Entry.all().filter('entrytype =','post').order('-date').fetch(min(num, 20)) | 185 | entries = Entry.all().filter('entrytype =','post').order('-date').fetch(min(num, 20)) |
| 183 | return [entry_struct(entry) for entry in entries] | 186 | return [entry_struct(entry) for entry in entries] |
| 184 | 187 | ||
| 185 | @checkauth(pos=2) | 188 | @checkauth(pos=2) |
| 186 | def blogger_deletePost(appkey, postid, publish): | 189 | def blogger_deletePost(appkey, postid, publish): |
| 187 | post=Entry.get_by_id(int(postid)) | 190 | post=Entry.get_by_id(int(postid)) |
| 188 | post.delete() | 191 | post.delete() |
| 189 | return True | 192 | return True |
| 190 | 193 | ||
| 191 | #------------------------------------------------------------------------------- | 194 | #------------------------------------------------------------------------------- |
| 192 | # WordPress API | 195 | # WordPress API |
| 193 | #------------------------------------------------------------------------------- | 196 | #------------------------------------------------------------------------------- |
| 194 | @checkauth() | 197 | @checkauth() |
| 195 | def wp_newCategory(blogid,struct): | 198 | def wp_newCategory(blogid,struct): |
| 196 | name=struct['name'] | 199 | name=struct['name'] |
| 197 | 200 | ||
| 198 | category=Category.all().filter('name =',name).fetch(1) | 201 | category=Category.all().filter('name =',name).fetch(1) |
| 199 | if category and len(category): | 202 | if category and len(category): |
| 200 | return category[0].slug | 203 | return category[0].slug |
| 201 | else: | 204 | else: |
| 202 | category=Category(key_name=urlencode(name), name=name,slug=urlencode(name)) | 205 | category=Category(key_name=urlencode(name), name=name,slug=urlencode(name)) |
| 203 | category.put() | 206 | category.put() |
| 204 | return category.slug | 207 | return category.slug |
| 205 | 208 | ||
| 206 | 209 | ||
| 207 | @checkauth() | 210 | @checkauth() |
| 208 | def wp_newPage(blogid,struct,publish): | 211 | def wp_newPage(blogid,struct,publish): |
| 209 | 212 | ||
| 210 | entry=Entry(title = struct['title'], | 213 | entry=Entry(title = struct['title'], |
| 211 | content = struct['description'], | 214 | content = struct['description'], |
| 212 | ) | 215 | ) |
| 213 | if struct.has_key('mt_text_more'): | 216 | if struct.has_key('mt_text_more'): |
| 214 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] | 217 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] |
| 215 | 218 | ||
| 216 | if struct.has_key('wp_slug'): | 219 | if struct.has_key('wp_slug'): |
| 217 | entry.slug=struct['wp_slug'] | 220 | entry.slug=struct['wp_slug'] |
| 218 | if struct.has_key('wp_page_order'): | 221 | if struct.has_key('wp_page_order'): |
| 219 | entry.menu_order=int(struct['wp_page_order']) | 222 | entry.menu_order=int(struct['wp_page_order']) |
| 220 | entry.entrytype='page' | 223 | entry.entrytype='page' |
| 221 | if publish: | 224 | if publish: |
| 222 | entry.publish(True) | 225 | entry.publish(True) |
| 223 | else: | 226 | else: |
| 224 | entry.save() | 227 | entry.save() |
| 225 | 228 | ||
| 226 | postid =entry.key().id() | 229 | postid =entry.key().id() |
| 227 | return str(postid) | 230 | return str(postid) |
| 228 | 231 | ||
| 229 | 232 | ||
| 230 | @checkauth(2) | 233 | @checkauth(2) |
| 231 | def wp_getPage(blogid,pageid): | 234 | def wp_getPage(blogid,pageid): |
| 232 | entry = Entry.get_by_id(int(pageid)) | 235 | entry = Entry.get_by_id(int(pageid)) |
| 233 | return entry_struct(entry) | 236 | return entry_struct(entry) |
| 234 | 237 | ||
| 235 | @checkauth() | 238 | @checkauth() |
| 236 | def wp_getPages(blogid,num): | 239 | def wp_getPages(blogid,num): |
| 237 | entries = Entry.all().filter('entrytype =','page').order('-date').fetch(min(num, 20)) | 240 | entries = Entry.all().filter('entrytype =','page').order('-date').fetch(min(num, 20)) |
| 238 | return [entry_struct(entry) for entry in entries] | 241 | return [entry_struct(entry) for entry in entries] |
| 239 | 242 | ||
| 240 | @checkauth(2) | 243 | @checkauth(2) |
| 241 | def wp_editPage(blogid,pageid,struct,publish): | 244 | def wp_editPage(blogid,pageid,struct,publish): |
| 242 | 245 | ||
| 243 | entry=Entry.get_by_id(int(pageid)) | 246 | entry=Entry.get_by_id(int(pageid)) |
| 244 | 247 | ||
| 245 | ## if struct.has_key('mt_keywords'): | 248 | ## if struct.has_key('mt_keywords'): |
| 246 | ## entry.tags=struct['mt_keywords'].split(',') | 249 | ## entry.tags=struct['mt_keywords'].split(',') |
| 247 | 250 | ||
| 248 | if struct.has_key('wp_slug'): | 251 | if struct.has_key('wp_slug'): |
| 249 | entry.slug=struct['wp_slug'] | 252 | entry.slug=struct['wp_slug'] |
| 250 | 253 | ||
| 251 | if struct.has_key('wp_page_order'): | 254 | if struct.has_key('wp_page_order'): |
| 252 | entry.menu_order=int(struct['wp_page_order']) | 255 | entry.menu_order=int(struct['wp_page_order']) |
| 253 | 256 | ||
| 254 | 257 | ||
| 255 | entry.title = struct['title'] | 258 | entry.title = struct['title'] |
| 256 | entry.content = struct['description'] | 259 | entry.content = struct['description'] |
| 257 | if struct.has_key('mt_text_more'): | 260 | if struct.has_key('mt_text_more'): |
| 258 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] | 261 | entry.content=entry.content+"<!--more-->"+struct['mt_text_more'] |
| 259 | if publish: | 262 | if publish: |
| 260 | entry.publish(True) | 263 | entry.publish(True) |
| 261 | else: | 264 | else: |
| 262 | entry.save() | 265 | entry.save() |
| 263 | 266 | ||
| 264 | return True | 267 | return True |
| 265 | 268 | ||
| 266 | 269 | ||
| 267 | @checkauth() | 270 | @checkauth() |
| 268 | def wp_deletePage(blogid,pageid): | 271 | def wp_deletePage(blogid,pageid): |
| 269 | post=Entry.get_by_id(int(pageid)) | 272 | post=Entry.get_by_id(int(pageid)) |
| 270 | post.delete() | 273 | post.delete() |
| 271 | return True | 274 | return True |
| 272 | 275 | ||
| 273 | @checkauth() | 276 | @checkauth() |
| 274 | def wp_getAuthors(blogid): | 277 | def wp_getAuthors(blogid): |
| 275 | return [{'user_id':1,'user_login':'','display_name':'admin'}] | 278 | return [{'user_id':1,'user_login':'','display_name':'admin'}] |
| 276 | 279 | ||
| 277 | @checkauth() | 280 | @checkauth() |
| 278 | def wp_getPageList(blogid): | 281 | def wp_getPageList(blogid): |
| 279 | return [] | 282 | return [] |
| 280 | 283 | ||
| 281 | def mt_setPostCategories(*arg): | 284 | def mt_setPostCategories(*arg): |
| 282 | return True | 285 | return True |
| 283 | #------------------------------------------------------------------------------- | 286 | #------------------------------------------------------------------------------- |
| 284 | 287 | ||
| 285 | class PlogXMLRPCDispatcher(SimpleXMLRPCDispatcher): | 288 | class PlogXMLRPCDispatcher(SimpleXMLRPCDispatcher): |
| 286 | def __init__(self, funcs): | 289 | def __init__(self, funcs): |
| 287 | SimpleXMLRPCDispatcher.__init__(self, True, 'utf-8') | 290 | SimpleXMLRPCDispatcher.__init__(self, True, 'utf-8') |
| 288 | self.funcs = funcs | 291 | self.funcs = funcs |
| 289 | 292 | ||
| 290 | dispatcher = PlogXMLRPCDispatcher({ | 293 | dispatcher = PlogXMLRPCDispatcher({ |
| 291 | 'blogger.getUsersBlogs' : blogger_getUsersBlogs, | 294 | 'blogger.getUsersBlogs' : blogger_getUsersBlogs, |
| 292 | 'blogger.deletePost' : blogger_deletePost, | 295 | 'blogger.deletePost' : blogger_deletePost, |
| 293 | 296 | ||
| 294 | 'metaWeblog.newPost' : metaWeblog_newPost, | 297 | 'metaWeblog.newPost' : metaWeblog_newPost, |
| 295 | 'metaWeblog.editPost' : metaWeblog_editPost, | 298 | 'metaWeblog.editPost' : metaWeblog_editPost, |
| 296 | 'metaWeblog.getCategories' : metaWeblog_getCategories, | 299 | 'metaWeblog.getCategories' : metaWeblog_getCategories, |
| 297 | 'metaWeblog.getPost' : metaWeblog_getPost, | 300 | 'metaWeblog.getPost' : metaWeblog_getPost, |
| 298 | 'metaWeblog.getRecentPosts' : metaWeblog_getRecentPosts, | 301 | 'metaWeblog.getRecentPosts' : metaWeblog_getRecentPosts, |
| 299 | 'metaWeblog.newMediaObject':metaWeblog_newMediaObject, | 302 | 'metaWeblog.newMediaObject':metaWeblog_newMediaObject, |
| 300 | 303 | ||
| 301 | 'wp.getCategories':metaWeblog_getCategories, | 304 | 'wp.getCategories':metaWeblog_getCategories, |
| 302 | 'wp.newCategory':wp_newCategory, | 305 | 'wp.newCategory':wp_newCategory, |
| 303 | 'wp.newPage':wp_newPage, | 306 | 'wp.newPage':wp_newPage, |
| 304 | 'wp.getPage':wp_getPage, | 307 | 'wp.getPage':wp_getPage, |
| 305 | 'wp.getPages':wp_getPages, | 308 | 'wp.getPages':wp_getPages, |
| 306 | 'wp.editPage':wp_editPage, | 309 | 'wp.editPage':wp_editPage, |
| 307 | 'wp.getPageList':wp_getPageList, | 310 | 'wp.getPageList':wp_getPageList, |
| 308 | 'wp.deletePage':wp_deletePage, | 311 | 'wp.deletePage':wp_deletePage, |
| 309 | 'wp.getAuthors':wp_getAuthors, | 312 | 'wp.getAuthors':wp_getAuthors, |
| 310 | 313 | ||
| 311 | 'mt.setPostCategories':mt_setPostCategories | 314 | 'mt.setPostCategories':mt_setPostCategories |
| 312 | 315 | ||
| 313 | 316 | ||
| 314 | 317 | ||
| 315 | }) | 318 | }) |
| 316 | 319 | ||
| 317 | 320 | ||
| 318 | # {{{ Handlers | 321 | # {{{ Handlers |
| 319 | class CallApi(BaseRequestHandler): | 322 | class CallApi(BaseRequestHandler): |
| 320 | def get(self): | 323 | def get(self): |
| 321 | Logger(request = self.request.uri, response = '----------------------------------').put() | 324 | Logger(request = self.request.uri, response = '----------------------------------').put() |
| 322 | self.write('<h1>please use POST</h1>') | 325 | self.write('<h1>please use POST</h1>') |
| 323 | 326 | ||
| 324 | def post(self): | 327 | def post(self): |
| 325 | #self.response.headers['Content-Type'] = 'application/xml; charset=utf-8' | 328 | #self.response.headers['Content-Type'] = 'application/xml; charset=utf-8' |
| 326 | request = self.request.body | 329 | request = self.request.body |
| 327 | response = dispatcher._marshaled_dispatch(request) | 330 | response = dispatcher._marshaled_dispatch(request) |
| 328 | Logger(request = unicode(request, 'utf-8'), response = unicode(response, 'utf-8')).put() | 331 | Logger(request = unicode(request, 'utf-8'), response = unicode(response, 'utf-8')).put() |
| 329 | self.write(response) | 332 | self.write(response) |
| 330 | 333 | ||
| 331 | class View(BaseRequestHandler): | 334 | class View(BaseRequestHandler): |
| 332 | @requires_admin | 335 | @requires_admin |
| 333 | def get(self): | 336 | def get(self): |
| 334 | self.write('<html><body><h1>Logger</h1>') | 337 | self.write('<html><body><h1>Logger</h1>') |
| 335 | for log in Logger.all().order('-date').fetch(5,0): | 338 | for log in Logger.all().order('-date').fetch(5,0): |
| 336 | self.write("<p>date: %s</p>" % log.date) | 339 | self.write("<p>date: %s</p>" % log.date) |
| 337 | self.write("<h1>Request</h1>") | 340 | self.write("<h1>Request</h1>") |
| 338 | self.write('<pre>%s</pre>' % cgi.escape(log.request)) | 341 | self.write('<pre>%s</pre>' % cgi.escape(log.request)) |
| 339 | self.write("<h1>Reponse</h1>") | 342 | self.write("<h1>Reponse</h1>") |
| 340 | self.write('<pre>%s</pre>' % cgi.escape(log.response)) | 343 | self.write('<pre>%s</pre>' % cgi.escape(log.response)) |
| 341 | self.write("<hr />") | 344 | self.write("<hr />") |
| 342 | self.write('</body></html>') | 345 | self.write('</body></html>') |
| 343 | 346 | ||
| 344 | class DeleteLog(BaseRequestHandler): | 347 | class DeleteLog(BaseRequestHandler): |
| 345 | def get(self): | 348 | def get(self): |
| 346 | if self.chk_admin(): | 349 | if self.chk_admin(): |
| 347 | for log in Logger.all(): | 350 | for log in Logger.all(): |
| 348 | log.delete() | 351 | log.delete() |
| 349 | 352 | ||
| 350 | 353 | ||
| 351 | self.redirect('/rpc/view') | 354 | self.redirect('/rpc/view') |
| 352 | #}}} | 355 | #}}} |
| 353 | 356 | ||
| 354 | 357 | ||
| 355 | def main(): | 358 | def main(): |
| 356 | #webapp.template.register_template_library("filter") | 359 | #webapp.template.register_template_library("filter") |
| 357 | application = webapp.WSGIApplication( | 360 | application = webapp.WSGIApplication( |
| 358 | [ | 361 | [ |
| 359 | ('/rpc', CallApi), | 362 | ('/rpc', CallApi), |
| 360 | ('/rpc/view', View), | 363 | ('/rpc/view', View), |
| 361 | ('/rpc/dellog', DeleteLog), | 364 | ('/rpc/dellog', DeleteLog), |
| 362 | 365 | ||
| 363 | ], | 366 | ], |
| 364 | debug=True) | 367 | debug=True) |
| 365 | wsgiref.handlers.CGIHandler().run(application) | 368 | wsgiref.handlers.CGIHandler().run(application) |
| 366 | 369 | ||
| 367 | if __name__ == '__main__': | 370 | if __name__ == '__main__': |
| 368 | main() | 371 | main() |
| 369 | 372 | ||