1 # -*- coding: utf-8 -*-
   2 """
   3 Tutorial 08 - 对话
   4 
   5 保存对话信息在 CP2 中非常的简单:
   6     cpg.request 提供了一个字典对象叫 sessionMap 记录了当前用户的会话数据.
   7 如果你使用的是内存(RAM)式的会话,你可以保存任何类型的对象在此字典中;
   8 否则,你将限制只能保持可以被线性化的对象.
   9 """
  10 from cherrypy import cpg
  11 #改造,增加 Aspect机制
  12 from cherrypy.lib.aspect import Aspect, STOP, CONTINUE
  13 
  14 class Page(Aspect):
  15     """要求从 Aspect 继承类
  16     """
  17     title = 'Untitled Page'
  18 
  19     def __init__(self):
  20         """增加全局性点击计数对象
  21         """
  22         self.count = 0
  23     def _count(self):
  24         """想简单的增加统一的计数函式,发现不行!
  25         """
  26         # 创建无聊的点击计数
  27         self.count = cpg.request.sessionMap.get('count', 0) + 1
  28         # 记录计数在会话对象中
  29         cpg.request.sessionMap['count'] = self.count
  30 
  31     def header(self):
  32         """强调页面编码
  33         """
  34         return '''
  35             <html>
  36             <head>
  37                 <title>%s</title>
  38                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  39             <head>
  40             <body>
  41             <h2>%s</h2>
  42         ''' % (self.title, self.title)
  43 
  44     def footer(self):
  45         return '''
  46             </body>
  47             </html>
  48         '''
  49 
  50     def _before(self, methodName, method):
  51         """投影函式 _before 会在任何函式运行时被调用,如果你有一些函式不想如此处理
  52         可以在调用前检测一下
  53         """
  54         #self.count = cpg.request.sessionMap.get('count', 0) + 1
  55         if methodName not in ['header', 'footer']:
  56             return CONTINUE, self.header()
  57         else:
  58             return CONTINUE, ''
  59 
  60     def _after(self, methodName, method):
  61         """同上投影函式 _after 是在任何函式运行后自动调用
  62         """
  63         #cpg.request.sessionMap['count'] = self.count
  64         if methodName not in ['header', 'footer']:
  65             return CONTINUE, self.footer()
  66         else:
  67             return CONTINUE, ''
  68 
  69 class HitCounter(Page):
  70     """点击计数页面对象
  71     """
  72     title = '点击计数 尝试页面!'
  73     def __init__(self):
  74         """直接增加下层对象
  75         """
  76         self.deep = DeepPage()
  77     def index(self):
  78         # 创建无聊的点击计数
  79         self.count = cpg.request.sessionMap.get('count', 0) + 1
  80         # 记录计数在会话对象中
  81         cpg.request.sessionMap['count'] = self.count        
  82         #self._count()
  83         
  84         # 在页面中显示计数信息
  85         return '''
  86             During your current session, you've viewed this
  87             page %s times! Your life is a patio of fun!
  88 
  89             <hr/>
  90             goto deep page count again!?
  91             <a href="deep">deep page</a>
  92         ''' % self.count
  93 
  94     index.exposed = True
  95 
  96 class DeepPage(Page):
  97     """下层对象的展示
  98     """
  99     title = 'DeepPage base AnotherPage'
 100     def index(self):
 101         #self._count()
 102         
 103         # 创建无聊的点击计数
 104         self.count = cpg.request.sessionMap.get('count', 0) + 1
 105         # 记录计数在会话对象中
 106         cpg.request.sessionMap['count'] = self.count
 107         
 108         return '''
 109             <p>
 110             在下层对象中继续计数!
 111             </p>:%s
 112             <hr/>
 113             <a href="../">go back </a>
 114         '''% self.count
 115 
 116     index.exposed = True
 117 
 118 
 119 cpg.root = HitCounter()
 120 
 121 cpg.server.start(configFile = 'tutorial.conf')

CherryPyTut/08_sessions.py (last edited 2009-12-25 07:14:31 by localhost)