Example #1
0
def save_fetched_table_if_changed(wfm: WfModule, new_table: DataFrame,
                                  error_message: str) -> datetime.datetime:
    """Store retrieved data table, if it is a change from wfm's existing data.

    "Change" here means either a changed table or changed error message.
    
    The WfModule's `status` and `error_msg` will be set, according to
    `error_message`.

    Set wfm.last_update_check, regardless.

    Create (and run) a ChangeDataVersionCommand.

    Notify the user.

    Return the timestamp (if changed) or None (if not).
    """

    with wfm.workflow.cooperative_lock():
        wfm.last_update_check = timezone.now()

        # Store this data only if it's different from most recent data
        version_added = wfm.store_fetched_table_if_different(new_table)

        if version_added:
            enforce_storage_limits(wfm)

        wfm.error_msg = error_message or ''
        wfm.status = (WfModule.ERROR if error_message else WfModule.READY)
        wfm.save()

    # un-indent: COMMIT, so we can notify the client and the client sees changes
    if version_added:
        ChangeDataVersionCommand.create(wfm,
                                        version_added)  # also notifies client
        if wfm.notifications == True:
            Notification.create(wfm, "New data version available")
    else:
        # no new data version, but we still want client to update WfModule status and last update check time
        notify_client_workflow_version_changed(wfm.workflow)

    return version_added