Handler.edit
from google.appengine.ext import webapp
from .. import util as siteutil
from .. util import page as pageutil
from . import BaseHTML
from google.appengine.api import users
from ..customfilter.grammar_conver import context_conver
 
class edit(webapp.RequestHandler):
    '''編輯及建立的超類別'''
    def get(self, username = None, request_page = None, privew = None):
        self.getContent(username, request_page)
 
    def getContent(self, username, request_page):
        ''' 取得內容的方法,供複寫用'''
        pass
 
    def post(self, username = None, request_page = None, priview = None):
        if username == "/preview" or priview == "/preview":#檢查是否為預覽請求
            self.preview(username, request_page)
        else:
            if request_page:
                request_page = siteutil.unquote(request_page)
            self.publish(username, request_page)
 
    def preview(self, username, request_page):
        '''回傳預覽請求'''
        self.response.out.write(context_conver(self.request.get('content')))
 
    def list_to_str(self,list):
        str = ''
        for astr in list:
            str += astr + ' '
        return str[:-1]
 
class editer(edit):
    '''編輯頁面的對應類別'''
    def getContent(self, username, request_page):
        request_page = siteutil.unquote(request_page)
        try:#嘗試存取網頁
            page = siteutil.findPage(siteutil.findUsedUser(username), request_page)[0]
        except pageutil.NotFindPageError:#一旦有例外,回傳404找不到網頁
            self.response.set_status(404,"can't find content.maybe is wrong name.")
        else:
            EditHTML({
                'type':'編輯',
                'path':self.request.url,
                'backpath': '/' + username + '-' + page.pagetitle,
                'pagetitle': page.pagetitle,
                'title': page.title,
                'tag' : self.list_to_str(page.tag),
                'content': page.content}).render(self)
 
    def publish(self,username,request_page):
        '''
        發布內容
        發布後到編輯的頁面(/[username]-[pagetitle])
        '''
        try:#嘗試存取網頁
            page = siteutil.findPage(siteutil.findUsedUser(username), request_page)[0]
        except pageutil.NotFindPageError:#一旦有例外,回傳404找不到網頁
            self.response.set_status(404,"can't find content.maybe is wrong name.")
        else:
            revise = {}
            if page.pagetitle != self.request.get('pagetitle'):
                revise['pagetitle'] = self.request.get('pagetitle')
            if page.title != self.request.get('title'):
                revise['title'] = self.request.get('title')
            if page.content != self.request.get('content'):
                revise['content'] = self.request.get('content')
            if page.tag != self.request.get('tag'):
                revise['tag'] = self.request.get('tag').split(' ')
            if revise:
                pageutil.updatePage(revise, page)
            self.redirect('/' + username + '-' + siteutil.quote(self.request.get('pagetitle')))
 
class creater(edit):
    '''建立頁面的對應類別'''
    def getContent(self, username = None, request_page = None):
        html = EditHTML({
                'type':'建立',
                'path':'/create',
                'backpath':'back',
                'title' : self.request.get('title'),
                'pagetitle' : self.request.get('title')}).render(self)
 
    def publish(self, username, request_page):
        '''
        發布內容
        發布後到建立的頁面(/[username]-[pagetitle])
        '''
        u = users.get_current_user()
        if not siteutil.findUsedUser(u):#檢查是否為第一次使用
            pageutil.create_by_first_use(u)#建立初始頁面(列表,etc)
        pageData = {
                'pagetitle' : self.request.get('pagetitle'),
                'title' : self.request.get('title'),
                'content' : self.request.get('content'),
                'tag' : self.request.get('tag').split(' ')
                }
        pageutil.createPage(pageData, u)
        self.redirect('/' + u.nickname() + '-' + siteutil.quote(pageData['pagetitle']))
 
class EditHTML(BaseHTML):
    ''' 共通使用的 render'''
    def __init__(self, content):
        super(EditHTML, self).__init__('edit.html')
        self.content.update(siteutil.logout_url('/'))
        u = users.get_current_user()
        self.content['username'] = u.nickname()
        self.content['login'] = True
        self.content.update(content)