def tag_newly_completed_contents(user_or_user_id, topic_id: int):
    """
    Tag user completed contents on email marketing for segmentation
    This will only consider module -> section -> chapter -> topic
    accordingly with topics_id
    :param topic_id: topic_id from topic user last updated
    :param user_or_user_id: Django User or his id
    :return: list of contents_full_tags
    """
    user = core_facade.find_user_by_id(user_or_user_id)
    topic = get_topic_with_contents_by_id(topic_id)
    possible_changing_tags = {
        topic.chapter.section.module.full_slug,
        topic.chapter.section.full_slug,
        topic.chapter.full_slug,
        topic.full_slug,
    }
    completed_module_content_slugs = (
        c.full_slug
        for c in completed_module_contents(user, topic.find_module()))
    tags = [
        s for s in completed_module_content_slugs
        if s in possible_changing_tags
    ]
    if tags:
        email_marketing_facade.tag_as(user.email, user.id, *tags)
    return tags
예제 #2
0
def find_user_by_id(user_id: int) -> _User:
    """
    Find user by her id
    :param user_id:
    :return:
    """
    return _core_facade.find_user_by_id(user_id)
예제 #3
0
def sync_user_on_discourse(user_or_id):
    """
    Synchronize user data on forum if API is configured
    :param user_or_id: Django user or his id
    :return: returns result of hitting Discourse api
    """
    can_make_api_call = bool(settings.DISCOURSE_API_KEY and settings.DISCOURSE_API_USER)
    can_work_without_sync = not (settings.DISCOURSE_BASE_URL or can_make_api_call)
    if can_work_without_sync:
        _logger.info('Discourse Integration not available')
        return
    elif not can_make_api_call:
        raise MissingDiscourseAPICredentials('Must define both DISCOURSE_API_KEY and DISCOURSE_API_USER configs')

    user = _core_facade.find_user_by_id(user_or_id)

    # https://meta.discourse.org/t/sync-sso-user-data-with-the-sync-sso-route/84398
    params = {
        'email': user.email,
        'external_id': user.id,
        'require_activation': 'false',
        'groups': ','.join(g.name for g in user.groups.all())
    }
    sso_payload, signature = generate_sso_payload_and_signature(params)
    # query_string = parse.urlencode()
    url = f'{settings.DISCOURSE_BASE_URL}/admin/users/sync_sso'
    headers = {
        'content-type': 'multipart/form-data',
        'Api-Key': settings.DISCOURSE_API_KEY,
        'Api-Username': settings.DISCOURSE_API_USER,
    }

    requests.post(url, data={'sso': sso_payload, 'sig': signature}, headers=headers)