Source

wdcms / main.py

# -*- coding: utf-8 -*-
from settings import  *
from model import  collection,content
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import yui
import logging,os
from google.appengine.ext import db
import math
logging.getLogger().setLevel(logging.DEBUG)




class NotFound(yui.HtmlRequestHandler):
    @yui.server_cache(60, False)
    def get(self):
        COLLECTIONS=collection.all().order("-time")
        template_values = {
        'COLLECTIONS':COLLECTIONS,
        }
        path = os.path.join(os.path.dirname(__file__), 'tpl','404.html')
        self.write(template.render(path, template_values))
#--------------------------------#
#--------------------------------#

class MainPage(yui.HtmlRequestHandler):
    @yui.server_cache(CACHETIME, False)
    def get(self):
        try:
            DEFAULT=db.GqlQuery("SELECT * FROM collection WHERE url = :1",'default')[0]
            COLLECTIONS=collection.all().order("-time")
        except IndexError:
            DEFAULT=None
            COLLECTIONS=None
        template_values = {
        'DEFAULT':DEFAULT,
        'COLLECTIONS':COLLECTIONS,
        }
        path = os.path.join(os.path.dirname(__file__), 'tpl','mainpage.html')
        self.write(template.render(path, template_values))

class CollectionPage(yui.HtmlRequestHandler):
    @yui.server_cache(CACHETIME, False)
    def get(self,collectionUrl):
        try: 
            COLLECTION=db.GqlQuery("SELECT * FROM collection WHERE url = :1",collectionUrl)[0]
            COLLECTIONS=collection.all().order("-time")
        except IndexError:
            COLLECTION=None
            COLLECTIONS=collection.all().order("-time")
        template_values = {
        'COLLECTION':COLLECTION,
        'COLLECTIONS':COLLECTIONS,
        }
        path = os.path.join(os.path.dirname(__file__), 'tpl','collectionpage.html')
        self.write(template.render(path, template_values))

class ContentPage(yui.HtmlRequestHandler):
    @yui.server_cache(CACHETIME, False)
    def get(self,collectionUrl,contenturl=None):
        try:
            COLLECTIONS=collection.all().order("-time")
            if contenturl:
                COLLECTION=db.GqlQuery("SELECT * FROM collection WHERE url = :1",collectionUrl)[0]
                CONTENT=db.GqlQuery("SELECT * FROM content WHERE url = :1",contenturl)[0]
            else:
                try:
                    COLLECTION=db.GqlQuery("SELECT * FROM collection WHERE url = :1","default")[0]
                except:
                    COLLECTION=collection()
                    COLLECTION.url="default"
                    COLLECTION.time=getCNnow()
                    COLLECTION.put()
                CONTENT=db.GqlQuery("SELECT * FROM content WHERE url = :1",collectionUrl)[0]
        except IndexError:
            self.redirect('/404')
            return

        
        template_values = {
        'COLLECTION':COLLECTION,
        'COLLECTIONS':COLLECTIONS,
        'CONTENT':CONTENT,
#        'exam':self.GET["exam"],

        }
        if self.GET["mod"]=="true":
            path = os.path.join(os.path.dirname(__file__), 'tpl','mod%s.html'%CONTENT.type)
            COLLECTIONS=collection.all()
            template_values["COLLECTIONS"]=COLLECTIONS
        else:
            if CONTENT.type=='html' or CONTENT.type==None:
                path = os.path.join(os.path.dirname(__file__), 'tpl','html.html')
            elif CONTENT.type=='css':
                self.header['Content-type'] = 'text/css; charset=UTF-8'
                path = os.path.join(os.path.dirname(__file__), 'tpl','css.css')
            elif CONTENT.type=='js':
                self.header['Content-type'] = 'text/javascript; charset=UTF-8'
                path = os.path.join(os.path.dirname(__file__), 'tpl','js.js')
            elif CONTENT.type=='rst':
                self.header['Content-type'] = 'text/html; charset=UTF-8'
                path = os.path.join(os.path.dirname(__file__), 'tpl','rst.html')
            elif CONTENT.type=='freemind':
                if self.GET["blob"]=="true":
                    self.header['Content-type'] = 'application/octet-stream'
                    self.write(CONTENT.content)
                    return
#                else:
#                    self.header['Content-type'] = 'text/html; charset=UTF-8'
                path = os.path.join(os.path.dirname(__file__), 'tpl','freemind.html')
        self.write(template.render(path, template_values))

application = yui.WsgiApplication([
    ('/', MainPage),
    ('/__(.*)', CollectionPage),
    ('/404', NotFound) ,
    ('/(.*)/(.*)', ContentPage),
    ('/(.*)', ContentPage),
])

def main():
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()