Google App Engine的简单用户管理示例?

2024-04-29 11:48:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是谷歌应用引擎的新手。在阅读教程时,我发现在php中所做的一些事情mysql在GAE中是不可用的。例如,数据存储中的自动增量功能不可用。另外,我对GAE中的会话管理感到困惑。总的来说,我很困惑,无法想象整件事。

请告诉我一个简单的用户管理系统,用户注册,用户登录,用户注销,会话(创建,管理,销毁)与数据存储。也请告诉我在哪里我可以得到简单但有效的例子。

提前谢谢。


Tags: 数据用户引擎功能mysql管理系统教程事情
3条回答

Django是您的最佳选择——根据我给您指出的版本,auth和sessions都应该按照Django文档“正常工作”。this article给出了简单的说明和如何从那里开始的示例。

对于Django会话,请参见here;对于Django auth,请参见here

你不会写用户管理和注册之类的东西,因为你使用谷歌自己的认证服务。这都包含在App Engine文档中。

我倾向于使用自己的用户和会话管理

对于我的web处理程序,我将附加一个名为session的decorator和一个名为authorize的decorator。装饰器将为每个请求附加一个会话,装饰器将确保用户获得授权。

(注意,authorize decorator是特定于我如何开发应用程序的——用户名是大多数请求中的第一个参数)。

例如,web处理程序可能如下所示:

class UserProfile(webapp.RequestHandler):
  @session
  @authorize
  def get(self, user):
     # Do some funky stuff
     # The session is attached to the self object.
     someObjectAttachedToSession = self.SessionObj.SomeStuff
     self.response.out.write("hello %s" % user)

在上面的代码中,session装饰器根据请求中存在的cookie附加一些我需要的会话内容。authorize头将确保只有在会话正确时用户才能访问该页。

decorators代码如下:

import functools
from model import Session
import logging

def authorize(redirectTo = "/"):
    def factory(method):
        'Ensures that when an auth cookie is presented to the request that is is valid'
        @functools.wraps(method)
        def wrapper(self, *args, **kwargs):

            #Get the session parameters
            auth_id = self.request.cookies.get('auth_id', '')
            session_id = self.request.cookies.get('session_id', '')

            #Check the db for the session
            session = Session.GetSession(session_id, auth_id)           

            if session is None:
                self.redirect(redirectTo)
                return
            else:
                if session.settings is None:
                    self.redirect(redirectTo)
                    return

                username = session.settings.key().name()

                if len(args) > 0:               
                    if username != args[0]:
                        # The user is allowed to view this page.
                        self.redirect(redirectTo)
                        return

            result = method(self, *args, **kwargs)

            return result
        return wrapper
    return factory

def session(method):
    'Ensures that the sessions object (if it exists) is attached to the request.'
    @functools.wraps(method)
    def wrapper(self, *args, **kwargs):

        #Get the session parameters
        auth_id = self.request.cookies.get('auth_id', '')
        session_id = self.request.cookies.get('session_id', '')

        #Check the db for the session
        session = Session.GetSession(session_id, auth_id)           

        if session is None:
            session = Session()
            session.session_id = Session.MakeId()
            session.auth_token = Session.MakeId()
            session.put()

        # Attach the session to the method
        self.SessionObj = session           

        #Call the handler.          
        result = method(self, *args, **kwargs)

        self.response.headers.add_header('Set-Cookie', 'auth_id=%s; path=/; HttpOnly' % str(session.auth_token))
        self.response.headers.add_header('Set-Cookie', 'session_id=%s; path=/; HttpOnly' % str(session.session_id))

        return result
    return wrapper

def redirect(method, redirect = "/user/"):
    'When a known user is logged in redirect them to their home page'
    @functools.wraps(method)
    def wrapper(self, *args, **kwargs):
        try:    
            if self.SessionObj is not None:
                if self.SessionObj.settings is not None:
                    # Check that the session is correct
                    username = self.SessionObj.settings.key().name()

                    self.redirect(redirect + username)
                    return
        except:
            pass
        return method(self, *args, **kwargs)
    return wrapper

相关问题 更多 >