适用于TurboGears2的可插拔应用程序,它提供了一个基本的用户资料页面,包含表单,允许用户编辑自己的资料或更改电子邮件/密码
项目描述
关于userprofile
userprofile是适用于TurboGears2的可插拔应用程序,它提供了一个基本的用户资料页面,包含表单,允许用户编辑自己的资料或更改密码或电子邮件。
userprofile与sqlachemy和ming后端都兼容。
安装
userprofile可以从pypi或github安装
pip install tgapp-userprofile
对于大多数用户应该都可以正常工作
插入userprofile
在您的应用程序 config/app_cfg.py 中导入 plug
from tgext.pluggable import plug
然后在文件 末尾 调用 plug 并传入 userprofile
plug(base_config, 'userprofile')
您将能够在 http://localhost:8080/userprofile 访问您的资料。
选项
tgapp-userprofile 支持一些选项,可以通过 plug 方法传递以自定义应用程序的各个方面
- user_partial - 显示在用户资料页面中的部分路径。
在不更改模板的情况下向资料页面添加更多数据很有用
custom_css - 用于资料页面的CSS文件路径,而不是默认的CSS文件。
用户属性
tgapp-userprofile 在 User 类实例中查找各种属性以驱动其默认行为,最重要的属性是 profile_data 属性,它可以提供一个包含用户信息的字典,用于在个人资料页面上显示,但其他属性也可以调整行为。
profile_data
一个字典,用于在个人资料页面上显示条目,默认字典是由以下内容构建的:
{'display_name':('Display Name', user.display_name), 'email_address':('Email Address', user.email_address)}
字典的每个键都是字段的 ID,在大多数情况下,它将与存储该字段的用户属性的名称相同。字典的值是元组,其中第一个值是要显示的字段名称,第二个值是字段的实际值。
如果存在 avatar 键,则它将提供用户头像图片的 URL。如果不存在,userprofile 将寻找 tgapp-fbauth 的 Facebook 头像,或者将回退到默认头像。
display_name 键将用作个人资料页面的标题。
profile_form
一个 ToscaWidgets 或 tw2 表单,可以用来编辑用户个人资料。默认情况下,提供一个自动生成的表单,其中每个条目在 profile_data 中都有一个文本字段。
save_profile
一个可调用的函数,它将接收由编辑表单提交的用户数据,并期望相应地更新用户。
默认情况下,值将按原样存储在具有与 profile_data 中提供的相同 ID 的用户字段中。
Bootstrap 布局
如果您想使用 Bootstrap 来美化 UserForm 或 ChangePasswordForm 表单布局的样式,请在您的 app_cfg 中:
def replace_profile_form_layout(): from axf.bootstrap import BootstrapFormLayout from userprofile.lib import UserForm from userprofile.lib import ChangePasswordForm UserForm.child = BootstrapFormLayout(children=UserForm.child.children) UserForm.submit.css_class = 'btn-primary form-control' ChangePasswordForm.child = BootstrapFormLayout(children=ChangePasswordForm.child.children) ChangePasswordForm.submit.css_class = 'btn-primary form-control' milestones.config_ready.register(replace_profile_form_layout)
身份验证配置
由于使用 userprofile 用户可以更改其电子邮件地址或用户名,因此您必须正确配置 repoze.who。在 app_cfg.py 中,定位到 ApplicationAuthMetadata 的 authenticate 方法:它应返回用户的 ID。现在 repoze.who 将将用户 ID 保存到 cookie 中以识别用户,并且由于此 ID 不会更改,因此身份验证过程允许用户安全地更改电子邮件地址和用户名。
此示例演示了一个基于用户名或电子邮件地址的 Ming 登录。
class ApplicationAuthMetadata(TGAuthMetadata): def __init__(self, sa_auth): self.sa_auth = sa_auth def get_query(self, login): try: _id = ObjectId(login) except InvalidId: _id = login return { '$or': [{'email_address': login}, {'user_name': login}, {'_id': _id}], 'blocked': {'$ne': True}, } def authenticate(self, environ, identity): login = identity['login'] user = self.sa_auth.user_class.query.find(self.get_query(login)).first() if not user: # pragma: no cover login = None elif not user.validate_password(identity['password']): login = None if login is None: try: from urllib.parse import parse_qs, urlencode except ImportError: from urlparse import parse_qs from urllib import urlencode from tg.exceptions import HTTPFound params = parse_qs(environ['QUERY_STRING']) params.pop('password', None) # Remove password in case it was there if user is None: # pragma: no cover params['failure'] = 'user-not-found' else: params['login'] = identity['login'] params['failure'] = 'invalid-password' # When authentication fails send user to login page. environ['repoze.who.application'] = HTTPFound( location=environ['SCRIPT_NAME'] + '?'.join(('/login', urlencode(params, True))) ) return str(user._id) if user and login else login def get_user(self, identity, userid): return self.sa_auth.user_class.query.find(self.get_query(userid)).first() def get_groups(self, identity, userid): return [g.group_name for g in identity['user'].groups] def get_permissions(self, identity, userid): return [p.permission_name for p in identity['user'].permissions] base_config.sa_auth.authmetadata = ApplicationAuthMetadata(base_config.sa_auth)
项目详情
tgapp-userprofile-0.3.6.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b16bb4c02cd591304386c7196d01916cb5f19110046402f8d29ffe02c74e9c9c |
|
MD5 | d128cc6adce7fe3d3589c71740bbe570 |
|
BLAKE2b-256 | d646b2e0fd88567044e714d176f2c62acc22bc0530c9bd1fbae2ca440fd7f83f |