跳转到主要内容

用户自助注册

项目描述

用户自助注册

此软件包提供实现应用程序用户自助注册的功能。

一般工作流程要求每个用户至少提供他的电子邮件地址,但也可能提供其他数据。用户注册后,会发送一封电子邮件,要求用户通过点击电子邮件中嵌入的链接来确认其身份。一旦注册得到确认,临时存储的注册数据将被删除。

应用程序可以通过订阅注册和确认事件以及提供针对注册和电子邮件创建的应用程序特定视图和适配器来自定义注册行为。

此软件包/构建包中包含一个演示应用程序,展示了如何使用基本视图快速创建注册和确认视图以及为您的应用程序定制的电子邮件。视图和电子邮件生成都提供了很好的灵活性,可以直接重用,但您也可以提供自己的。

用户自助注册

注册用户

注册由注册实用程序管理

>>> from gocept.registration.registrations import Registrations
>>> registrations = Registrations()

现在我们可以通过传递用户的电子邮件地址和一些附加数据来注册用户

>>> peter = registrations.register('peter@example.com', {'name': u'Peter'})
>>> peter
<gocept.registration.registrations.Registration object at 0x...>

所有注册都有一个哈希值,匿名标识它们,以及附加的电子邮件和数据

>>> peter.hash
'<SHA-HASH>'
>>> peter.email
'peter@example.com'
>>> peter.data
{'name': u'Peter'}

Peter的注册包含在由哈希值标识的实用程序中

>>> registrations[peter.hash]
<gocept.registration.registrations.Registration object at 0x...>

Peter现在可以使用给他提供的哈希值确认他的注册

>>> registrations.confirm(peter.hash)

确认注册后,它不再存储在实用程序中

>>> registrations[peter.hash]
Traceback (most recent call last):
KeyError: '<SHA-HASH>'

自定义注册对象

在调用register方法时,我们还可以可选地传递一个工厂来构造注册。这在子类化Registration类时很有用。

>>> from gocept.registration.registrations import Registration
>>> class MyRegistration(Registration):
...     def __init__(self, hash, email, data):
...         assert data.has_key('agentNumber'), 'missing agent number!'
...         super(MyRegistration, self).__init__(hash, email, data)
>>> registrations.register('james@bond.com',
...                        {'name': u'James Bond'},
...                        factory=MyRegistration)
Traceback (most recent call last):
...
AssertionError: missing agent number!
>>> registrations.register('james@bond.com',
...                        {'name': u'James Bond',
...                         'agentNumber': u'007'},
...                        factory=MyRegistration)
<MyRegistration object at ...>

应用程序钩子

应用程序可以钩入两个钩子来自定义注册过程

  • 注册对象的ObjectAddedEvent,以及

  • 当用户确认他的注册时的RegistrationConfirmedEvent

让我们注册这两个事件的订阅者以演示每个事件在哪里被调用

>>> def registered(event):
...     print event, event.object
>>> import zope.component
>>> from zope.app.container.interfaces import IObjectAddedEvent
>>> zope.component.provideHandler(registered, (IObjectAddedEvent,))
>>> chuck = registrations.register('chuck@example.com', {'name': u'LeChuck'})
<zope.app.container.contained.ObjectAddedEvent object at 0x...>
<gocept.registration.registrations.Registration object at 0x...>
>>> def confirmed(event):
...     print event, event.registration
>>> from gocept.registration.interfaces import IRegistrationConfirmed
>>> zope.component.provideHandler(confirmed, (IRegistrationConfirmed,))
>>> registrations.confirm(chuck.hash)
<gocept.registration.interfaces.RegistrationConfirmedEvent object at 0x...>
<gocept.registration.registrations.Registration object at 0x...>

让我们再次清理那些注册

>>> from zope.app.testing import placelesssetup
>>> placelesssetup.tearDown()

确认电子邮件

发送注册电子邮件分为两部分:创建电子邮件本身和发送它。

创建确认邮件

为了提供一些中央配置,注册可以适应到IRegistrationEmailConfiguration

>>> from gocept.registration.interfaces import IEmailConfiguration
>>> from gocept.registration.interfaces import IRegistration
>>> class TestConfig(object):
...     zope.interface.implements(IEmailConfiguration)
...     addr_from = "Ad Ministrator <admin@example.com>"
...     confirmation_url = "http://example.com/confirm?hash=%s"
...     confirmation_template = """From: %(from)s
... To: %(to)s
... Subject: Please confirm your registration
...
... We received your registration. To activate it, please follow this confirmation
...
... link:
...
...  %(link)s"""
...     def __init__(self, registration):
...         pass
>>> zope.component.provideAdapter(TestConfig, adapts=(IRegistration,))

确认电子邮件是通过将注册对象适应到IRegistrationEmail接口来创建的。我们提供了一个简单的实现作为起点。

>>> from gocept.registration.email import ConfirmationEmail
>>> mail = ConfirmationEmail(chuck)
>>> print mail.message
From: Ad Ministrator <admin@example.com>
To: chuck@example.com
Subject: Please confirm your registration
<BLANKLINE>
We received your registration. To activate it, please follow this confirmation
link:
<BLANKLINE>
http://example.com/confirm?hash=<SHA-HASH>

发送确认邮件

我们提供了一个标准的事件处理程序,将发送注册的电子邮件

>>> from gocept.registration.email import send_registration_mail
>>> zope.component.provideAdapter(ConfirmationEmail, (IRegistration,))
>>> zope.component.provideHandler(send_registration_mail, (IRegistration, IObjectAddedEvent,))
>>> from zope.component.event import objectEventNotify
>>> zope.component.provideHandler(objectEventNotify, (IObjectAddedEvent,))
>>> from gocept.registration.tests import DummyMailer
>>> zope.component.provideUtility(DummyMailer())
>>> janine = registrations.register('janine@example.com')
(Ad Ministrator <admin@example.com> -> ['janine@example.com'])
From: Ad Ministrator <admin@example.com>
To: janine@example.com
Subject: Please confirm your registration
<BLANKLINE>
We received your registration. To activate it, please follow this confirmation
<BLANKLINE>
link:
<BLANKLINE>
http://example.com/confirm?hash=<SHA-HASH>

更改

0.3.0 (2010-06-22)

  • 特性:允许将工厂传递给Registrations对象的register

  • 错误:修复了Contained缺少导入语句的问题。

0.2.0 (2008-05-10)

  • 特性:使包的覆盖率报告可用。

  • 特性:实现一个生成可预测哈希值的测试注册组件。

  • 特性:使用IMailDelivery接口而不是IMailer

0.1.0 (2008-03-28)

  • 首次发布。

项目详细信息


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

gocept.registration-0.3.0.tar.gz (12.6 kB 查看哈希值)

上传时间

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面