def watch_changes()

in src/olympia/users/models.py [0:0]


def watch_changes(old_attr=None, new_attr=None, instance=None, sender=None, **kw):
    if old_attr is None:
        old_attr = {}
    if new_attr is None:
        new_attr = {}
    changes = {
        x for x in new_attr if not x.startswith('_') and new_attr[x] != old_attr.get(x)
    }

    # Log email changes.
    if (
        'email' in changes
        and new_attr['email'] is not None
        and old_attr.get('email') is not None
    ):
        log.info('Creating user history for user: %s', instance.pk)
        UserHistory.objects.create(email=old_attr.get('email'), user_id=instance.pk)
    # If username or display_name changes, reindex the user add-ons, if there
    # are any.
    if 'username' in changes or 'display_name' in changes:
        from olympia.addons.tasks import index_addons

        ids = [addon.pk for addon in instance.get_addons_listed()]
        if ids:
            index_addons.delay(ids)