Skip to main content

A set of helper functions for CSV to Salesforce procedures, with reporting in AWS S3, based in a Django project

Project description

Overview

A set of helper functions for CSV to Salesforce procedures, with reporting in AWS S3, based in a Django project. The use case is extremely specific, but the helpers should be modular so they can be cherry-picked.

Typical use case:

  • Receive an S3 event
  • Download the S3 object
  • Serialize the file into JSON
  • Bulk upsert the JSON data to Salesforce
  • Parse the results of the upsert for errors
  • Construct a CSV error report
  • Move the triggering S3 object to an archive folder
  • Push the error report to an error folder in the same bucket
  • Push an object to Salesforce that details information about the above execution

High-level Example

Using the Orchestrator class, you can skip manually setting up a lot of the above steps. This class is intended to be subclassed, and should provide plenty of options for overriding methods to better suit your use-case.

Inheriting the Orchestrator

# orchestrator.py
from django.conf import settings
from django_s3_csv_2_sfdc.classes import Orchestrator as BaseOrchestrator


class Orchestrator(BaseOrchestrator):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        # This must be defined in the child class because your Salesforce object could be named anything
        self.execution_object_name = "Integration_Execution__c"

    @property
    def execution_sfdc_hash(self):
        # And it could have any number of fields
        return {
            "Number_of_Errors__c": self.error_count,
            "Error_Report__c": self.error_report_link,
            "Data_File__c": self.s3_object_key,
        }

    @property
    def error_report_link(self):
        return f"https://{self.bucket_name}.{settings.AWS_REGION}.amazonaws.com/{self.error_file_s3_key}"

Using the Orchestrator

# biz_logic.py
from django.conf import settings
from django_s3_csv_2_sfdc.classes import SfClient

# import the custom Orchestrator defined above
from .orchestrator import Orchestrator

salesforce = SfClient()
orchestrator = Orchestrator("some/s3/key/file.csv", settings.S3_BUCKET, sf_client=salesforce)

upsert_key = "My_External_ID__c"
accounts_data = [{"Name": "A name", upsert_key: "123"}]
results = salesforce.bulk.Account.upsert(results, upsert_key)

# You'll call log_batch for each batch you upload. This method
# will parse the results in search of errors
orchestrator.log_batch(results, accounts_data, "Account", upsert_key)

# This will create the error report, archive the source s3 file, and push
# the integration object to Salesforce. You'll definitely want to customize
# this by overriding this method or the methods it invokes
orchestrator.automagically_finish_up()

Low-level Example

from django_s3_csv_2_sfdc.csv_helpers import create_error_report
from django_s3_csv_2_sfdc.s3_helpers import download_file, respond_to_s3_event, upload_file
from django_s3_csv_2_sfdc.sfdc_helpers import extract_errors_from_results


# handler for listening to s3 events
def handler(event, context):
    respond_to_s3_event(event, download_and_process)


def download_and_process(s3_object_key, bucket_name):
    download_path = download_file(s3_object_key, bucket_name)

    # This function contains your own biz logic; does not come from this library
    results = serialize_and_push_to_sfdc(download_path)

    sucesses, errors = parse_bulk_upsert_results(results)

    report_path, errors_count = create_error_report([errors])

    upload_file(report_path, bucket_name)

Just take what'cha need!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-s3-csv-2-sfdc-0.4.6.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_s3_csv_2_sfdc-0.4.6-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file django-s3-csv-2-sfdc-0.4.6.tar.gz.

File metadata

  • Download URL: django-s3-csv-2-sfdc-0.4.6.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Windows/10

File hashes

Hashes for django-s3-csv-2-sfdc-0.4.6.tar.gz
Algorithm Hash digest
SHA256 e10b891c69c5d6dcff9d05ce8f55b6f45e9f49893229f5dba71d53fe14c0a2b0
MD5 f5c048e70c4b0fdceaafc763ab434317
BLAKE2b-256 ab3a4812ccd0a08af259f750a1d50945620a5386480a89846f1232cedf3007c1

See more details on using hashes here.

File details

Details for the file django_s3_csv_2_sfdc-0.4.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_s3_csv_2_sfdc-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b57dced2ee3e438688d43c8f567e6e892f7b6ec879c67d9b8128ca229b58a4c5
MD5 5e1c7a710b50f362f44514d65d4016d1
BLAKE2b-256 f8b4ee02c02a61357833425c8523e2cfd1640cf621dc10e1173fe27598e5e13d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page