【问题标题】:Google App Engine, define a Preprocessing classGoogle App Engine,定义一个预处理类
【发布时间】:2011-05-28 03:28:04
【问题描述】:


我正在尝试定义一个base request handling 类,以便webapp 页面可以继承一些基本方法和变量,否则需要为应用程序的每个页面重复定义这些方法和变量。一种类似的功能,如django preprocessors。这是我的其他页面继承的基类:

class BasePage(webapp.RequestHandler):
    def __init__(self):    
        self.user = users.get_current_user()    
        self.template_values = {
                'user': self.user,       
                'environ': self,   #I don't like the idea of passing the whole environ object to a template

                ##The below three functions cannot be executed during _init_ because of absence of self.request 
                #'openid_providers': self.openid_providers(),  
                #'logout_url': self.get_logout_url(),
                #'request': self.get_request(),
            }           

    ##A sort of similar functionality like render_to_response in django
    def render_template(self, template_name, values = None, *args, **kwargs):
        #PATH is the directory containing the templates
        if values: 
            for value in values:  self.template_values[value] = values[value]
        self.response.out.write(template.render(PATH+template_name, self.template_values, *args, **kwargs))                 

    ##Returns request as the name suggests
    def logout_url(self):
        return users.create_logout_url(self.request.url)

    ##Returns request as the name suggests
    def request(self):
        return request

    ##Returns openid login urls        
    def openid_providers(self):
        #OPENID_POVIDERS  is a list of dictionary 
        for p in OPENID_PROVIDERS:
             p['login_url'] = users.create_login_url(self.request.get('next', '/') , p['name'], p['url'])
        return OPENID_PROVIDERS  

除了在初始化过程中我无法传递一些变量,因为 self.request 不可用之外,一切正常。因此,对于一种解决方法,我所做的是将整个 self 变量作为模板变量传递。

是否有其他方法可以向模板提供模板变量(request、logout_url 等)?

【问题讨论】:

    标签: python google-app-engine web-applications django-templates preprocessor


    【解决方案1】:

    我已经在我的 AppEngine 代码中使用 Template Method Pattern 解决了这个问题

    基本上,基类看起来像:

    class MyBasePage(webapp.RequestHandler):
        def __init__(self):
            # common setup/init stuff here, 
            # omitted for this discussion
    
        def Setup(self):
            # request handling setup code needed in both GET/POST methods, like
            # checking for user login, getting session cookies, etc.
            # omitted for this discussion
    
        def get(self, *args):
            self.Setup()
            # call the derived class' 'DoGet' method that actually has 
            # the logic inside it
            self.DoGet(*args)
    
        def post(self, *args):
            self.Setup()
            # call the derived class' 'DoPost' method 
            self.DoPost(*args)
    
        def DoGet(self, *args):
            ''' derived classes override this method and 
                put all of their GET logic inside. Base class does nothing.'''
            pass
    
        def DoPost(self, *args):
            ''' derived classes override this method and 
                put all of their POST logic inside. Base class does nothing.'''
            pass
    

    ...您的派生类大多只需要担心那些 DoGet()DoPost() 方法的胆量。

    【讨论】:

    • 谢谢!我正在考虑定义自定义获取、基类中的帖子和使用 super(),但是这个 DoPost,DoGet 看起来很棒!我认为这对我不需要很多 django 功能的小项目很有好处。
    【解决方案2】:

    一个比 bgporter 简单得多的解决方案是在 webapp.RequestHandlerinitialize 方法中进行通用设置。这是一个工作示例,我们想在请求对象中添加一个类似 Django 的 is_ajax 方法:

    class BaseHandler(webapp.RequestHandler):
        def initialize(self, request, response):
            super(BaseHandler, self).initialize(request, response)
            # Add a Django-like is_ajax() method to the request object
            request.is_ajax = lambda: \
                request.environ.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
    

    在调用适当的getpost(或其他)方法之前,调用此方法是为了使用当前请求和响应对象初始化每个请求处理程序。

    【讨论】:

    • 是的,你如何做这样的事情并不是很明显。我不确定为什么处理程序不只是使用__init__ 来做同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多