Handler.__init__.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.template import Context, Template
import django.template.loader
from google.appengine.api import users
import py.util as siteutil
import os
 
class BaseHTML(object):
    def __init__(self, htmlPath = ''):
        self.content = Context({'sitetitle':None,'style':None })
        self.htmlFile = htmlPath
 
    def update(self, unit):
        ''' 對參數進行更新'''
        self.content.update(unit)
        return self
    def render(self, resquestHandler = None):
        ''' 將html檔案配合參數輸出'''
        if resquestHandler:
            resquestHandler.response.out.write(django.template.loader.render_to_string(self.htmlFile,self.content))
        else:
            return django.template.loader.render_to_string(self.htmlFile, self.content)
 
class IndexHTML(BaseHTML):
    def __init__(self):
        super(IndexHTML,self).__init__('index.html')
        self.content.update(siteutil.loginout_url("/"))
        u = users.get_current_user()
        list = siteutil.newestlist(10)
        for page in list:
            page.pagetitle = siteutil.quote(page.pagetitle)
        self.content.update({'login':u,'isindex':True, 'newestlist': list,
                'ver': UpdateStateHTML().read_state()[1]})#只取得每個版本更新的資訊
        #檢查現在是否有使用者,影響到首頁畫面
        if u:
            self.content.update({'username':u.nickname(),'firstuser':not siteutil.findUsedUser(u)})
 
class NewPageHTML(BaseHTML):
    def __init__(self):
        super(NewPageHTML, self).__init__('fiftyarticle.html')
        self.content.update(siteutil.loginout_url("/newtext"))
        u = users.get_current_user()
        list = siteutil.newestlist(50)
        for page in list:
            page.pagetitle = siteutil.quote(page.pagetitle)
        self.content.update({'login':u, 'pagelist': list})
        #檢查現在是否有使用者,影響到首頁畫面
        if u:
            self.content.update({'username':u.nickname(),'firstuser':not siteutil.findUsedUser(u)})
 
import ConfigParser
 
class UpdateStateHTML(BaseHTML):
    def __init__(self):
        super(UpdateStateHTML, self).__init__('updateState.html')
        self.content.update(siteutil.loginout_url("/ver"))
        nowver, values = self.read_state()
        self.content['states'] = values
        self.content['nowver'] = nowver
        u = users.get_current_user()
        self.content['login'] = u
        if u:
            self.content.update({'username':u.nickname(),'firstuser':not siteutil.findUsedUser(u)})
 
    def read_state(self):
        '''
        讀取版本資訊資料
        '''
        ver_path = os.path.join(os.path.dirname(__file__), os.path.join(os.path.join('..','..'),'ver_state'))
        values = []
        nowver = '0'
        for Path, Directory, files in os.walk(ver_path):
            for filename in files:
                value = UpdateStateHTML.read_value(os.path.join(ver_path,filename))
                if value['versioninformation']['ver'] > nowver:
                    nowver = value['versioninformation']['ver']
                    values.insert(0, value)
                else:
                    values.append(value)
        return nowver,values
    @staticmethod
    def read_value(filename):
        '''
        讀取ini檔案資料
        '''
        config = ConfigParser.ConfigParser()
        values = {}
        config.read(filename)
        for section in config.sections():
            values[section] = {}
            for item in config.items(section):
                values[section][item[0]] = item[1]
        return values