Django应用,用于与intercom.io集成
项目描述
django-intercom简化了在django应用程序中使用http://intercom.io的过程。您只需执行以下操作。
支持的Python版本:2.7和3.3+
版本
0.1.3 : 07-09-2019 - 修复匿名错误,它没有正确设置 [@humitos] - 将django提升到2.0.13 - 更改默认_user,使其不是全局的 [@humitos]
0.1.2 : 10-19-2018 - #35修复(错误):TypeError: 'bool'对象不是可调用的 [@AnArchkoleptik] - 将django开发要求提升到2.0.9
0.1.1 : 07-21-2018 - 修复用户未认证时的intercom_tag
0.1.0 : 06-30-2018 - 将包重命名为避免与python-intercom_tag冲突 - 重构custom_data和company_data部分 - 重构is_authenticated - 修复PEP8问题 - 添加使用匿名用户与intercom集成的功能
0.0.13 : 2-16-2016 - 修复了setup.py在Python 3上的问题
0.0.12 : 2-1-2016 - 添加了对Python 3的支持
文档
文档也可在网上查阅,见http://django-intercom.readthedocs.org
安装
使用easy_setup或pip安装django-intercom
pip install django-intercom
将intercom添加到您的django设置文件中的INSTALLED_APPS
INSTALLED_APPS = ( # all # other # apps 'django_intercom', )
在django设置文件中添加
设置,并使用您的intercom应用程序ID。 在settings.py中
INTERCOM_APPID = "your appID"
在主体标签之前将模板标签代码添加到您的基模板中。
在页面顶部放置以下内容
{% load intercom %}
在页面底部,在 </body> 标签之前放置以下内容
{% intercom_tag %}
启用安全模式(可选)
这是可选的,如果不设置,则不会使用安全模式。
如果您想启用安全模式,可以在 settings.py 中添加 INTERCOM_SECURE_KEY,使用您从 intercom->app->security 页面获取的私钥。
在settings.py中
INTERCOM_SECURE_KEY = "your security_code"
您需要查看代码示例以找到安全密钥。
您还需要确保在启用正确之前,在安全页面上勾选“启用安全模式”复选框。
禁用标签(可选)
这是可选的,如果不设置为 True,则标签将处于活动状态。
如果您想禁用标签,可以在 settings.py 中添加 INTERCOM_DISABLED。
在settings.py中
INTERCOM_DISABLED = True
这在您在一段时间内不希望在某些环境中将用户信息发送到 intercom.io 的每个请求时很有用,例如在开发环境中。
这提供了一个快速禁用标签的方法,而无需在模板或 settings.py 中的 INTERCOM_APPID 中删除/注释标签(后者将禁用信息发送,但会在日志中引起设置警告)。
Intercom 收件箱
Intercom 有能力将收件箱链接添加到您的应用程序中,以便人们可以联系您,并且您可以让他们知道当有消息等待时。如果您想使用这些功能,您需要执行以下操作。
将 intercom css id 添加到包含文本的任何内联元素中,例如
<a id="Intercom" href="#">Support</a>
将适当的 CSS 添加到您的样式表中。
无图标
#Intercom { display: inline-block; text-decoration: underline; padding: 0; }
白色信封(黑色背景上的白色文本)
#Intercom { display: inline-block; text-decoration: underline; padding: 0 0 0 24px; background: transparent url(https://www.intercom.io/images/white_env.png) no-repeat left center; }
黑色信封(白色/灰色背景上的黑色文本)
#Intercom { display: inline-block; text-decoration: underline; padding: 0 0 0 24px; background: transparent url(https://www.intercom.io/images/black_env.png) no-repeat left center; }
如果您想显示未读消息计数,则还需要添加以下内容
#Intercom em { display: inline-block; font-style: normal; text-decoration: underline; }
配置您的设置。如果想要更改默认值,请将以下内容添加到 django settings 中。
INTERCOM_ENABLE_INBOX
默认:True
在 settings.py 中
INTERCOM_ENABLE_INBOX = True
INTERCOM_ENABLE_INBOX_COUNTER
默认:True
在 settings.py 中
INTERCOM_ENABLE_INBOX_COUNTER = True
INTERCOM_INBOX_CSS_SELECTOR
默认:‘#Intercom’
在 settings.py 中
INTERCOM_INBOX_CSS_SELECTOR = '#Intercom'
用户数据
默认情况下,django-intercom 将以下用户信息发送到 intercom.io
user_id(来自 request.user.id)
email(来自 request.user.email)
name(来自 request.user.username 或,作为后备,request.user.get_username())
created_at(来自 request.user.date_joined)
user_hash(如果设置了 INTERCOM_SECURE_KEY,则使用 INTERCOM_SECURE_KEY 和 user_id 计算)
您可以通过创建一个带有 user_data 方法的类来覆盖 1-4 字段中的任何或全部字段,该方法接受 Django User 模型作为参数。该方法应返回一个包含任何或全部键 user_id、email、name 和 user_created 的字典,以及每个键的期望值。请注意,user_created 键必须包含一个 datetime。以下是一个示例
from django.utils.dateformat import DateFormat class IntercomUserData: """ User data class located anywhere in your project This one is located in thepostman/utils/user_data.py """ def user_data(self, user): """ Required method, same name and only accepts one attribute (django User model) """ return { 'name' : user.userprofile.name, }
您需要将您的类注册到 django-intercom 中,以便它知道在哪里找到它。您通过将类添加到 INTERCOM_USER_DATA_CLASS 设置来实现这一点。
INTERCOM_USER_DATA_CLASS
默认 = None
在settings.py中
INTERCOM_USER_DATA_CLASS = 'thepostman.utils.user_data.IntercomUserData'
自定义数据
Intercom.io 允许您发送自定义数据,django-intercom 使这变得简单。您所需做的就是创建一个带有 custom_data 方法的类,该方法接受 Django User 模型作为参数并返回一个字典。以下是一个示例
from thepostman.models import message class IntercomCustomData: """ Custom data class located anywhere in your project This one is located in thepostman/utils/custom_data.py """ def custom_data(self, user): """ Required method, same name and only accepts one attribute (django User model) """ num_messages = message.objects.filter(user=user).count() num_unread = messages.objects.filter(user=user, read=False).count() return { 'num_messages' : num_messages, 'num_unread' : num_unread, }
一旦您构建了这些类,您需要将它们注册到 django-intercom 中,以便它知道在哪里找到它们。您通过将类添加到 INTERCOM_CUSTOM_DATA_CLASSES 设置来实现这一点。请注意,如果多个自定义数据类返回相同的字典键,则最后运行的类(列表中较低的位置)将覆盖之前的类。
INTERCOM_CUSTOM_DATA_CLASSES
默认 = None
在settings.py中
INTERCOM_CUSTOM_DATA_CLASSES = [ 'thepostman.utils.custom_data.IntercomCustomData', ]
公司数据
Intercom.io 允许您通过公司分组用户,django-intercom 使得这变得容易。您只需要创建一个类,该类包含一个名为 company_data 的方法,该方法接受一个 Django 用户模型作为参数,并返回一个包含键 id、name 和 created_at 以及您想存储的关于公司的其他信息的字典。请注意,created_at 键必须包含 Unix 时间戳。以下是一个示例
from django.utils.dateformat import DateFormat class IntercomCompanyData: """ Company data class located anywhere in your project This one is located in thepostman/utils/company_data.py """ def company_data(self, user): """ Required method, same name and only accepts one attribute (django User model) """ organisation = user.organisation return { 'id' : organisation.id, 'name' : organisation.name, 'created_at' : DateFormat(organisation.created_at).U(), 'price_plan' : organisation.price_plan, }
您需要将您的类注册到 django-intercom,以便它知道在哪里找到它。您可以通过将类添加到 INTERCOM_COMPANY_DATA_CLASS 设置来完成此操作。
INTERCOM_COMPANY_DATA_CLASS
默认 = None
在settings.py中
INTERCOM_COMPANY_DATA_CLASS = 'thepostman.utils.company_data.IntercomCompanyData'
项目详情
django-intercom-0.1.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 33a7cd924cf6c371e18e4f835e208e12a54a2b94cbf0c2129359326a5f5f41df |
|
MD5 | b9d3a66fa7bb965ac5fec4f94aa7ab23 |
|
BLAKE2b-256 | 6a3a69541a8020ffe1fbc728c5084f6ab6176234a0769a5a787c255cf56fe8ed |