Skip to content

Add more common functions to the hubspot_api app #106

@mbertrand

Description

@mbertrand

There are some functions and tasks in each of our hubspot-enabled applications that are almost identical but that reference the application's specific functions for handling its object models. Some of these could be made more generic (via adding the app_label and model name as input parameters) and centralized here instead, reducing code duplication.

For example, sync_contact_with_hubspot, sync_deal_with_hubspot, and sync_product_with_hubspot exist in xpro, mitxonline, and bootcamps, but could potentially be replaced by something like this in ol-django/hubspot_api:

def get_hubspot_function(func_name: str) -> Callable:
    """Given a string like my_app.api.do_something, return that function"""
    func_parts = func_name.split(".")
    func = apps.get_app_config(func_parts[0]).module
    for i in range(1, len(func_parts)):
        func = getattr(func, func_parts[i])
    return func


def sync_object_with_hubspot(
        object_id: int,
        msg_func: str,
        app_label: str,
        model_name: str,
        hubspot_type: str
) -> SimplePublicObject:
    """
    Sync a django object with a hubspot object

    Args:
        object_id(int): The django object id
        msg_func(str): The name of function that will generate the request message body (<app>.<module>.<function>)
        app_label(str): The app label for the object model
        model_name(str): The object model name
        hubspot_type(str): The hubspot object type (contact, deal, etc)

    Returns:
        SimplePublicObject: The hubspot object
    """
    body = get_hubspot_function(msg_func)(object_id)
    content_type = ContentType.objects.get_by_natural_key(app_label, model_name)

    return upsert_object_request(
        content_type, hubspot_type, object_id=object_id, body=body
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions