From 5ef70a6fc4552753df9bcf998dbd6ce7e88ea2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Tue, 12 Jul 2022 17:48:06 +0200 Subject: [PATCH] add tests and docs for ANALYTICAL_IDENTITY_FUNC settings --- docs/settings.rst | 14 ++++++++++++++ tests/unit/test_utils.py | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/docs/settings.rst b/docs/settings.rst index 4865049..ef0a77a 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -25,3 +25,17 @@ their default values. ``GOOGLE_ANALYTICS_INTERNAL_IPS`` to configure for Google Analytics. See :ref:`internal-ips`. + +.. data:: ANALYTICAL_IDENTITY_FUNC + + Default: Identity function dependent on provider + + A function that returns the identity of the given user. This overrides the + default settings of different providers. + + E.g. Google has in its conditions for enabling UserID the requirement, that prohibits + sending personal data (such as an e-mail address) to analytics. + If e-mail address is used as username, using GTag would break the requirements. + + In such case add uuid field to the user and set ```ANALYTICAL_IDENTITY_FUNC``` to + ```lambda user: user.uuid``` diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 8a9bd13..6e10b69 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -34,6 +34,7 @@ class SettingDeletedTestCase(TestCase): class MyUser(AbstractBaseUser): identity = models.CharField(max_length=50) + name = models.CharField(max_length=50) USERNAME_FIELD = 'identity' class Meta: @@ -46,6 +47,11 @@ class GetIdentityTestCase(TestCase): get_id = get_identity(Context({}), user=MyUser(identity='fake_id')) assert get_id == 'fake_id' + @override_settings(ANALYTICAL_IDENTITY_FUNC=lambda user: f"{user.name} ({user.identity})") + def test_custom_username_field_identity_func(self): + get_id = get_identity(Context({}), user=MyUser(identity='fake_id', name='fake_name')) + assert get_id == 'fake_name (fake_id)' + @override_settings(ANALYTICAL_DOMAIN="example.org") class GetDomainTestCase(TestCase):