Ajaxhandler
import datetime
from google.appengine.api import users
from google.appengine.ext import webapp
import db
from . import BaseHTML
import py.util
from template import default_templates
 
class AjaxHandler(webapp.RequestHandler):
    def get(self,request):
        if request == "getmylist":#指令:取得頁面清單
            pages = db.Pagedb.gql('where author = :1', py.util.findUsedUser(self.request.get('username'))).fetch(100)
            if pages:
                for page in pages:
                    page.pagetitle = py.util.quote(page.pagetitle)
                mylistTemplate = BaseHTML('mylistTemplete.html')
                mylistTemplate.content.update({'pagelist':pages,'checkbox':self.request.get('checkbox')})
                mylistTemplate.render(self)
            else:
                self.response.out.write('找不到資料')
        elif request == "deletePages":#指令:刪除頁面
            raise ValueError, self.request.get('checked')
        elif request == "css":#指令:取得自訂CSS資訊
            userdb = db.Userdb.get_by_key_name(self.request.get('username'))
            template = BaseHTML('css.html')
            if userdb and userdb.using_way == 'link':
                template.content['using_css'] = 'url'
            elif userdb and userdb.using_way == 'file':
                template.content['using_css'] = 'file'
            else:
                template.content['using_css'] = 'default'
            template.content['using_css_default'] = userdb.using_way_default
            template.content['default_templates'] = default_templates
            template.render(self)
        else:
            self.response.set_status(404,"can't find content.maybe is wrong name.")
 
    def post(self, request):
        #目前用於設定自訂CSS
        userdb = db.Userdb.get_by_key_name(users.get_current_user().nickname())
        if self.request.POST['select_way'] == 'file':#使用指定檔案
            userdb.using_way = 'file'
        elif self.request.POST['select_way'] == 'url':#使用指定url
            userdb.using_way = 'link'
            userdb.cssurl = self.request.POST['css_url']
        elif self.request.POST['select_way'] == 'default':#使用預設CSS
            userdb.using_way = 'default'
            userdb.using_way_default = self.request.POST['select_default']#設定使用的CSS
        else:
            self.response.set_status(404,"can't find content.maybe is wrong name.")#錯誤訊息用
        userdb.put()
        #發送回傳訊息
        if  self.request.POST['select_way'] == 'default':
            self.response.out.write(u'設定:' + self.request.POST['select_way'] + u':' + self.request.POST['select_default'] + u'. 成功')
        else:
            self.response.out.write(u'設定:' + self.request.POST['select_way'] + u'. 成功')