コード例 #1
0
def sync_donations(test_run):
    global error_count
    global success_count
    donations = Donation.objects.all()
    logger.info("Syncing {0} Donation objects.".format(donations.count()))

    for donation in donations:
        logger.info("Syncing Donation: {0}".format(donation.id))

        # Find the corresponding SF donation.
        try:
            sfdonation = SalesforceDonation.objects.get(external_id_donation=donation.id)
        except SalesforceDonation.DoesNotExist:
            sfdonation = SalesforceDonation()

        # Initialize Salesforce objects.
        if donation.user:
            try:
                sfContact = SalesforceContact.objects.get(external_id=donation.user.id)
                sfdonation.receiver = sfContact
            except SalesforceContact.DoesNotExist:
                logger.error("Unable to find contact id {0} in Salesforce for donation id {1}".format(donation.user.id, donation.id))
        if donation.project:
            try:
                sfProject = SalesforceProject.objects.get(external_id=donation.project.id)
                sfdonation.project = sfProject
            except SalesforceProject.DoesNotExist:
                logger.error("Unable to find project id {0} in Salesforce for donation id {1}".format(donation.project.id, donation.id))

        # SF Layout: Donation Information section.
        sfdonation.amount = "%01.2f" % (float(donation.amount) / 100)
        sfdonation.close_date = donation.created

        if donation.user and sfContact.last_name:
            if sfContact.first_name:
                sfdonation.name = sfContact.first_name + " " + sfContact.last_name
            else:
                sfdonation.name = sfContact.last_name
        else:
            sfdonation.name = "1%MEMBER"

        # Unknown - sfdonation.payment_method =

        sfdonation.stage_name = DonationStatuses.values[donation.status]
        # TODO: Should we use "Recurring" instead of Monthly?
        sfdonation.opportunity_type = donation.DonationTypes.values[donation.donation_type]

        # SF Layout: System Information section.
        sfdonation.donation_created_date = donation.created
        sfdonation.external_id_donation = donation.id
        sfdonation.record_type = "012A0000000ZK6FIAW"

        # Save the object to Salesforce
        if not test_run:
            try:
                sfdonation.save()
                success_count += 1
            except Exception as e:
                error_count += 1
                logger.error("Error while saving donation id {0}: ".format(donation.id) + str(e))
コード例 #2
0
def sync_donations(test_run):
    donations = Donation.objects.all()
    logger.info("Syncing {0} Donation objects.".format(donations.count()))

    for donation in donations:
        logger.info("Syncing Donation: {0}".format(donation))

        # Find the corresponding SF donation.
        try:
            sfdonation = SalesforceDonation.objects.filter(external_id=donation.id).get()
        except SalesforceDonation.DoesNotExist:
            sfdonation = SalesforceDonation()

        # Initialize Salesforce objects.
        sfContact = SalesforceContact.objects.filter(external_id=donation.user.id).get()
        sfProject = SalesforceProject.objects.filter(external_id=donation.project.id).get()

        # SF Layout: Donation Information section.
        sfdonation.amount = donation.amount
        sfdonation.close_date = donation.created
        sfdonation.name = sfContact.first_name + " " + sfContact.last_name
        # Unknown - sfdonation.payment_method =
        # Unknown - sfdonation.organization = SalesforceOrganization.objects.filter(external_id=1).get()
        sfdonation.project = sfProject
        sfdonation.stage_name = donation.status
        sfdonation.opportunity_type = str(donation.donation_type)
        # SF Layout: Additional Information section.

        # SF Layout: Description Information section.

        # SF Layout: System Information section.
        sfdonation.donation_created_date = donation.created

        # SF: Other.
        sfdonation.external_id = donation.id
        sfdonation.receiver = sfContact

        # Save the SF donation.
        if not test_run:
            sfdonation.save()
コード例 #3
0
ファイル: sync.py プロジェクト: gannetson/onepercentclub-site
def sync_donations(dry_run, sync_from_datetime, loglevel):
    logger.setLevel(loglevel)
    error_count = 0
    success_count = 0

    donations = Donation.objects.all()
    if sync_from_datetime:
        donations = donations.filter(updated__gte=sync_from_datetime)

    logger.info("Syncing {0} Donation objects.".format(donations.count()))

    for donation in donations:
        logger.debug("Syncing Donation: {0}".format(donation.id))

        # Find the corresponding SF donation.
        try:
            sfdonation = SalesforceDonation.objects.get(external_id_donation=donation.id)
        except SalesforceDonation.DoesNotExist:
            sfdonation = SalesforceDonation()

        # Initialize Salesforce objects.
        if donation.user:
            try:
                sfContact = SalesforceContact.objects.get(external_id=donation.user.id)
                sfdonation.receiver = sfContact
            except SalesforceContact.DoesNotExist:
                logger.error("Unable to find contact id {0} in Salesforce for donation id {1}".format(
                    donation.user.id, donation.id))
        if donation.project:
            try:
                sfProject = SalesforceProject.objects.get(external_id=donation.project.id)
                sfdonation.project = sfProject
            except SalesforceProject.DoesNotExist:
                logger.error("Unable to find project id {0} in Salesforce for donation id {1}".format(
                    donation.project.id, donation.id))

        # SF Layout: Donation Information section.
        sfdonation.amount = "%01.2f" % (float(donation.amount) / 100)
        sfdonation.close_date = donation.created.date()

        if donation.user and donation.user.get_full_name() != '':
            sfdonation.name = donation.user.get_full_name()
        else:
            sfdonation.name = "1%MEMBER"

        # Get the payment method from the associated order / payment
        sfdonation.payment_method = payment_method_mapping['']  # Maps to Unknown for DocData.
        if donation.order:
            lp = donation.order.latest_payment
            if lp and lp.latest_docdata_payment:
                if lp.latest_docdata_payment.payment_method in payment_method_mapping:
                    sfdonation.payment_method = payment_method_mapping[lp.latest_docdata_payment.payment_method]

        sfdonation.stage_name = DonationStatuses.values[donation.status].title()
        sfdonation.opportunity_type = donation.DonationTypes.values[donation.donation_type].title()

        # SF Layout: System Information section.
        sfdonation.donation_created_date = donation.created.date()
        sfdonation.external_id_donation = donation.id
        sfdonation.record_type = "012A0000000ZK6FIAW"

        # Save the object to Salesforce
        if not dry_run:
            try:
                sfdonation.save()
                success_count += 1
            except Exception as e:
                error_count += 1
                logger.error("Error while saving donation id {0}: ".format(donation.id) + str(e))

    return success_count, error_count
コード例 #4
0
def sync_donations(dry_run, sync_from_datetime, loglevel):
    logger.setLevel(loglevel)
    error_count = 0
    success_count = 0

    donations = Donation.objects.all()
    if sync_from_datetime:
        donations = donations.filter(updated__gte=sync_from_datetime)

    logger.info("Syncing {0} Donation objects.".format(donations.count()))

    for donation in donations:
        logger.debug("Syncing Donation: {0}".format(donation.id))

        # Find the corresponding SF donation.
        try:
            sfdonation = SalesforceDonation.objects.get(external_id_donation=donation.id)
        except SalesforceDonation.DoesNotExist:
            sfdonation = SalesforceDonation()
        except Exception as e:
            logger.error("Error while loading sfdonation id {0} - stopping: ".format(donation.id) + str(e))
            return success_count, error_count+1

        # Poplate the data
        sfdonation.external_id_donation = donation.id
        sfdonation.amount = "%01.2f" % donation.amount

        if donation.user:
            try:
                sfdonation.donor = SalesforceContact.objects.get(external_id=donation.order.user.id)
            except SalesforceContact.DoesNotExist:
                logger.error("Unable to find contact id {0} in Salesforce for donation id {1}".format(
                    donation.order.user.id, donation.id))
        if donation.project:
            try:
                sfdonation.project = SalesforceProject.objects.get(external_id=donation.project.id)
            except SalesforceProject.DoesNotExist:
                logger.error("Unable to find project id {0} in Salesforce for donation id {1}".format(
                    donation.project.id, donation.id))
        if donation.fundraiser:
            try:
                sfdonation.fundraiser = SalesforceFundraiser.objects.get(external_id=donation.fundraiser.id)
            except SalesforceFundraiser.DoesNotExist:
                logger.error("Unable to find fundraiser id {0} in Salesforce for donation id {1}".format(
                    donation.fundraiser.id, donation.id))

        sfdonation.stage_name = donation.order.get_status_display()
        sfdonation.close_date = donation.created
        sfdonation.donation_created_date = donation.created
        sfdonation.donation_updated_date = donation.updated
        sfdonation.donation_ready_date = donation.completed or None

        sfdonation.type = donation.order.order_type

        if donation.user and donation.order.user.get_full_name() != '':
            sfdonation.name = donation.order.user.get_full_name()
        else:
            sfdonation.name = "Anonymous"

        sfdonation.record_type = "012A0000000ZK6FIAW"

        # Get the payment method from the associated order / payment
        sfdonation.payment_method = payment_method_mapping['']  # Maps to Unknown for DocData.
        if donation.order:
            lp = OrderPayment.get_latest_by_order(donation.order)
            if lp and lp.payment_method in payment_method_mapping:
                sfdonation.payment_method = payment_method_mapping[lp.payment_method]

        # Save the object to Salesforce
        if not dry_run:
            try:
                sfdonation.save()
                success_count += 1
            except Exception as e:
                error_count += 1
                logger.error("Error while saving donation id {0}: ".format(donation.id) + str(e))

    return success_count, error_count