django-tos 是一个可重用的 Django 应用程序,用于设置服务条款。
项目描述
django-tos
此项目允许管理员重置与最终用户的服务条款。它跟踪服务条款何时更改以及用户何时同意新的服务条款。
摘要
跟踪服务条款何时更改
用户在登录时需要被告知并同意/重新同意(提供自定义登录)
仅有两个模型(TOS 和用户协议)
服务条款安装
pip install django-tos
将 tos 添加到您的 INSTALLED_APPS 设置中。
使用 python manage.py migrate 同步您的数据库
配置
选项
有两种方法可以配置 django-tos - 要么在用户登录时启用服务条款检查,要么使用中间件在每次 GET 请求上启用服务条款检查。
如果您无法覆盖您的登录视图(例如,如果您正在使用 django-allauth),则应使用第二种选项。
选项 1:登录时检查服务条款
在您的根 URL 配置文件 urls.py 中添加
from tos.views import login
# terms of service links
urlpatterns += patterns('',
url(r'^login/$', login, {}, 'auth_login',),
url(r'^terms-of-service/', include('tos.urls')),
)
选项 2:中间件检查
此选项使用配置的 Django 缓存的 incr 方法。如果您在复杂或并行环境中使用 django-tos,请确保使用支持原子递增操作的缓存后端。有关更多信息,请参阅 Django 文档此部分末尾的说明。
此外,为确保使用可以跳过协议检查的用户(例如:开发者、员工、管理员、超级用户、员工)预热缓存正常工作,您需要在您的 INSTALLED_APPS 设置中包含 tos(例如,示例中的 myapp)。
INSTALLED_APPS = (
...
'tos',
...
'myapp', # Example app name
...
)
优点
可选地使用单独的缓存来存储 TOS 协议(如果默认缓存不支持原子递增操作则必须使用)
允许某些用户跳过 TOS 检查(例如:开发者、员工、管理员、超级用户、员工)
使用信号来使缓存的协议无效
当用户匿名或未登录时跳过协议检查
当请求是 AJAX 时跳过协议检查
当请求不是 GET 请求时跳过协议检查(以避免干扰数据更改)
缺点
需要为每个已登录用户创建一个缓存键
需要为每个员工用户创建一个额外的缓存键
当活跃的 TermsOfService 发生变化时,可能留下缓存中的键
效率
员工用户最佳情况:2 次缓存命中
非员工用户最佳情况:1 次缓存未命中,2 次缓存命中
最坏情况:1 次缓存命中,2 次缓存未命中,1 次数据库查询,1 次缓存设置(这仅在用户登录时发生)
选项 2 配置
在您的根 URL 配置文件 urls.py 中仅添加 TOS URL
# terms of service links urlpatterns += patterns('', url(r'^terms-of-service/', include('tos.urls')), )
可选:由于 TOS 使用的缓存将主要进行读取操作,您可以使用专门用于 TOS 的单独缓存。为此,在项目的 settings.py 中创建一个新的缓存
CACHES = { ... # The cache specifically for django-tos 'tos': { # Can use any name here 'BACKEND': ..., 'LOCATION': ..., 'NAME': 'tos-cache', # Can use any name here }, }
并配置 django-tos 使用新缓存
TOS_CACHE_NAME = 'tos' # Must match the key name in in CACHES
此设置默认为 default 缓存。
然后在您的项目的 settings.py 中添加中间件到 MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = ( ... # Terms of service checks 'tos.middleware.UserAgreementMiddleware', )
可选:要允许用户跳过 TOS 检查,您需要在 TOS 缓存中为它们设置相应的缓存键。每个用户的缓存键需要以 django:tos:skip_tos_check: 为前缀,并附加用户 ID。
以下是一个示例应用程序配置,允许员工用户和超级用户跳过 TOS 协议检查
from django.apps import AppConfig, apps from django.conf import settings from django.contrib.auth import get_user_model from django.core.cache import caches from django.db.models import Q from django.db.models.signals import post_save, pre_save from django.dispatch import receiver class MyAppConfig(AppConfig): name = 'myapp' def ready(self): if 'tos' in settings.INSTALLED_APPS: cache = caches[getattr(settings, 'TOS_CACHE_NAME', 'default')] tos_app = apps.get_app_config('tos') TermsOfService = tos_app.get_model('TermsOfService') @receiver(post_save, sender=get_user_model(), dispatch_uid='set_staff_in_cache_for_tos') def set_staff_in_cache_for_tos(user, instance, **kwargs): if kwargs.get('raw', False): return # Get the cache prefix key_version = cache.get('django:tos:key_version') # If the user is staff allow them to skip the TOS agreement check if instance.is_staff or instance.is_superuser: cache.set('django:tos:skip_tos_check:{}'.format(instance.id), version=key_version) # But if they aren't make sure we invalidate them from the cache elif cache.get('django:tos:skip_tos_check:{}'.format(instance.id), False): cache.delete('django:tos:skip_tos_check:{}'.format(instance.id), version=key_version) @receiver(post_save, sender=TermsOfService, dispatch_uid='add_staff_users_to_tos_cache') def add_staff_users_to_tos_cache(*args, **kwargs): if kwargs.get('raw', False): return # Get the cache prefix key_version = cache.get('django:tos:key_version') # Efficiently cache all of the users who are allowed to skip the TOS # agreement check cache.set_many({ 'django:tos:skip_tos_check:{}'.format(staff_user.id): True for staff_user in get_user_model().objects.filter( Q(is_staff=True) | Q(is_superuser=True)) }, version=key_version) # Immediately add staff users to the cache add_staff_users_to_tos_cache()
django-tos-i18n
使用 django-modeltranslation 实现的 django-tos 国际化。
TOS 国际化安装
假设您已正确安装 django-tos 到您的应用程序中,您只需要将以下应用程序添加到 INSTALLED_APPS
INSTALLED_APPS += ('modeltranslation', 'tos_i18n')
并且您还应在 Django LANGUAGES 变量中定义您的语言,例如。
LANGUAGES = (
('pl', 'Polski'),
('en', 'English'),
)
请注意,将这些添加到 INSTALLED_APPS 将会改变 Django 模型。具体来说,对于每个需要翻译的 field,都会添加具有名称 field_<lang_code> 的附加字段,例如对于给定的模型
class MyModel(models.Model):
name = models.CharField(max_length=10)
将生成以下字段: name , name_en, name_pl。
您可能需要迁移数据库,如果您使用 Django < 1.7,则建议使用 South。这些迁移应保留在您的本地项目中。
如何使用 South 迁移 tos
以下是一些分步示例,说明如何将使用 syncdb 同步的传统 django-tos 安装转换为具有 South 迁移的已翻译 django-tos-i18n。
通过在 Django 设置文件中放置以下内容,通知 South 将迁移存储在自定义位置
SOUTH_MIGRATION_MODULES = { 'tos': 'YOUR_APP.migrations.tos', }
添加所需目录(包)
mkdir -p YOUR_APP/migrations/tos touch YOUR_APP/migrations/tos/__init__.py
创建初始迁移(指当前数据库状态)
python manage.py schemamigration --initial tos
模拟迁移(因为更改已经存在于数据库中)
python manage.py migrate tos --fake
将tos_i18n(和modeltranslation)安装到INSTALLED_APPS
INSTALLED_APPS += ('modeltranslation', 'tos_i18n',)
确保Django的LANGUAGES设置已正确配置。
迁移已更改的内容
$ python manage.py schemamigration --auto tos $ python migrate tos
就这些。你现在已经在多语言模式下运行tos,使用你已在LANGUAGES设置中声明的语言。这也会在Django管理中做出所有必要的调整。
有关翻译如何在细节上工作的更多信息,请参阅django-modeltranslation文档。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
django-tos-1.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 792a064a1337221f8683e07fb7d06e0839cb7c6ff3f4ca0804f7698ac947572e |
|
MD5 | f93141c7d91e71ecfd792e51ba8d6f6e |
|
BLAKE2b-256 | 167fb5c535f0a983b5e0308f5e3f158e0cdadf7a3717fbf6c29966450faabd8c |
django_tos-1.1.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8c3e91bef6b6bfe31c4481d22442d0afe6c268f82dc9983094ecb594c5f316e9 |
|
MD5 | db2af93580a92db9f02054ea6cf03f0a |
|
BLAKE2b-256 | 0f355e916c2dd1f4bc8998881f80ba832ca71c19c810856e83013046ae77841d |