用户自注册
gocept.registration的Python项目详细描述
用户自注册
此包提供实现用户自注册的功能 应用。
一般工作流要求每个用户至少提供他的电子邮件 地址,但也可以提供其他数据。用户注册后, 发送电子邮件,要求他通过点击 嵌入在电子邮件中。一旦注册被确认,临时 存储的注册数据将被删除。
应用程序可以通过订阅事件自定义注册行为 用于注册和确认,并提供特定于应用程序的视图和 用于注册和电子邮件创建的适配器。
这个包/构建中包含一个演示应用程序,它展示了如何 使用可用的基本视图快速创建注册和 为您的应用程序提供确认视图和自定义电子邮件。两种观点 电子邮件生成提供了很好的灵活性,所以它们可以 直接重复使用,但也可以提供自己的。
用户自注册
注册用户
注册由注册实用程序管理:
>>> 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的注册包含在由hash标识的实用程序中:
>>> registrations[peter.hash] <gocept.registration.registrations.Registration object at 0x...>
彼得现在可以使用给他的散列来确认他的注册:
>>> 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 ...>
应用程序挂钩
应用程序可以使用两个钩子自定义注册过程:
- 注册对象的objectaddevent,和
- 当用户确认其注册时,注册确认就发生了
让我们为这两个事件注册一个订户,以演示每个事件的调用位置:
>>> 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()
确认电子邮件
发送注册电子邮件分为两部分:创建电子邮件 本身,并发送它。
创建确认邮件
为了提供一些中心配置,注册可以调整为 注册邮件配置:
>>> 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对象。
- 错误:修复了所包含的缺少导入语句。
0.2.0(2008-05-10)
- 特点:提供套餐覆盖率报告。
- 功能:实现一个生成 可预测的散列。
- 功能:使用IMailDelivery接口而不是IMailer。
0.1.0(2008-03-28)
- 初次发布。