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):
      template_values = {
      }
      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:
            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]
        except IndexError:
            COLLECTION=None
        template_values = {
        'COLLECTION':COLLECTION,
        }
        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:
            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:
            template_values = {}
            path = os.path.join(os.path.dirname(__file__), 'tpl','404.html')
            self.write(template.render(path, template_values))
            return

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

        }
        if self.GET["mod"]=="true":
            if CONTENT.type=='html' or CONTENT.type==None:
                path = os.path.join(os.path.dirname(__file__), 'tpl','mod.html')
            elif CONTENT.type=='css':
                path = os.path.join(os.path.dirname(__file__), 'tpl','modcss.html')
            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'
                path = os.path.join(os.path.dirname(__file__), 'tpl','css.css')
        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()