def sync_users(dry_run, sync_from_datetime, loglevel): logger.setLevel(loglevel) error_count = 0 success_count = 0 users = BlueBottleBaseUser.objects.all() if sync_from_datetime: users = users.filter(updated__gte=sync_from_datetime) logger.info("Syncing {0} User objects.".format(users.count())) for user in users: logger.debug("Syncing User: {0}".format(user.id)) # Find the corresponding SF user. try: contact = SalesforceContact.objects.get(external_id=user.id) except SalesforceContact.DoesNotExist: contact = SalesforceContact() # Determine and set user type (person, group, foundation, school, company, ... ) contact.category1 = BlueBottleBaseUser.UserType.values[user.user_type].title() # SF Layout: Profile section. contact.first_name = user.first_name if user.last_name.strip(): contact.last_name = user.last_name else: contact.last_name = "1%MEMBER" if user.gender == "male": contact.gender = BlueBottleBaseUser.Gender.values['male'].title() elif user.gender == "female": contact.gender = BlueBottleBaseUser.Gender.values['female'].title() else: contact.gender = "" if user.availability: contact.availability = BlueBottleBaseUser.Availability.values[user.availability].title() else: contact.availability = "" contact.user_name = user.username contact.is_active = user.is_active contact.close_date = user.deleted contact.member_since = user.date_joined contact.date_joined = user.date_joined contact.last_login = user.last_login contact.why_one_percent_member = user.why contact.about_me_us = user.about contact.location = user.location contact.birth_date = user.birthdate contact.available_to_share_time_and_knowledge = user.share_time_knowledge contact.available_to_donate = user.share_money # SF Layout: Contact Information section. contact.email = user.email contact.website = user.website # Bank details of recurring payments try: recurring_payment = RecurringDirectDebitPayment.objects.get(user=user) contact.bank_account_city = recurring_payment.city contact.bank_account_holder = recurring_payment.name contact.bank_account_number = recurring_payment.account except RecurringDirectDebitPayment.DoesNotExist: contact.bank_account_city = '' contact.bank_account_holder = '' contact.bank_account_number = '' # Determine if the user has activated himself, by default assume not # if this is a legacy record, by default assume it has activated contact.has_activated = False try: rp = RegistrationProfile.objects.get(user_id=user.id) contact.tags = rp.activation_key if rp.activation_key == RegistrationProfile.ACTIVATED: contact.has_activated = True except RegistrationProfile.DoesNotExist: if not user.is_active and user.date_joined == user.last_login: contact.has_activated = False else: contact.has_activated = True if user.address: contact.mailing_city = user.address.city contact.mailing_street = user.address.line1 + '\n' + user.address.line2 if user.address.country: contact.mailing_country = user.address.country.name else: contact.mailing_country = '' contact.mailing_postal_code = user.address.postal_code contact.mailing_state = user.address.state else: contact.mailing_city = '' contact.mailing_street = '' contact.mailing_country = '' contact.mailing_postal_code = '' contact.mailing_state = '' # The default: Organization(Account) will be 'Individual' without setting a specific value # contact.organization_account = SalesforceOrganization.objects.get(external_id=contact.organization.id) # SF Layout: My Settings section. contact.receive_newsletter = user.newsletter contact.primary_language = user.primary_language # SF: Other contact.external_id = user.id # Save the object to Salesforce if not dry_run: try: contact.save() success_count += 1 except Exception as e: error_count += 1 logger.error("Error while saving contact id {0}: ".format(user.id) + str(e)) return success_count, error_count
def sync_users(test_run): global error_count global success_count users = BlueBottleUser.objects.all() logger.info("Syncing {0} User objects.".format(users.count())) for user in users: logger.info("Syncing User: {0}".format(user.id)) # Find the corresponding SF user. try: contact = SalesforceContact.objects.get(external_id=user.id) except SalesforceContact.DoesNotExist: contact = SalesforceContact() # Determine and set user type (person, group, foundation, school, company, ... ) contact.category1 = BlueBottleUser.UserType.values[user.user_type] # SF Layout: Profile section. contact.first_name = user.first_name if user.last_name: contact.last_name = user.last_name else: contact.last_name = "1%MEMBER" if user.gender == "male": contact.gender = BlueBottleUser.Gender.values['male'] elif user.gender == "female": contact.gender = BlueBottleUser.Gender.values['female'] else: contact.gender = "" contact.user_name = user.username contact.is_active = user.is_active contact.member_since = user.date_joined contact.why_one_percent_member = user.why contact.about_me_us = user.about contact.location = user.location contact.birth_date = user.birthdate # TODO: check field contact.available_time = user.available_time ? # contact.which_1_would_you_like_to_contribute = # TODO: contact.tags - should be copied? - 20130530 - discussed with Suzanne, check with Liane # contact.tags = user.tags.get() # SF Layout: Contact Information section. contact.email = user.email contact.website = user.website if user.address: contact.mailing_city = user.address.city contact.mailing_street = user.address.line1 + '\n' + user.address.line2 if user.address.country: contact.mailing_country = user.address.country.name else: contact.mailing_country = '' contact.mailing_postal_code = user.address.postal_code contact.mailing_state = user.address.state else: contact.mailing_city = '' contact.mailing_street = '' contact.mailing_country = '' contact.mailing_postal_code = '' contact.mailing_state = '' # The default: Organization(Account) will be 'Individual' without setting a specific value # contact.organization_account = SalesforceOrganization.objects.get(external_id=contact.organization.id) # Note: Fill with the number of activities of a member # contact.activity_number = user. # SF Layout: Contact Activity section. # -- back-end calculations with Ben #contact.amount_of_single_donations # -- 20130530 - Not used: discussed with Suzanne,Ben #contact.has_n_friends #contact.has_given_n_vouchers #contact.is_doing_n_tasks #contact.number_of_donations #contact.support_n_projects #contact.total_amount_of_donations # SF Layout: My Settings section. contact.receive_newsletter = user.newsletter contact.primary_language = user.primary_language # TODO: contact expertise on website by web-team (20130530) #contact.administration_finance = #contact.agriculture_environment #contact.architecture #contact.computer_ict #contact.design #contact.economy_business #contact.education #contact.fund_raising #contact.graphic_design #contact.health #contact.internet_research #contact.law_and_politics #contact.marketing_pr #contact.online_marketing #contact.photo_video #contact.physics_technique #contact.presentations #contact.project_management #contact.psychology #contact.social_work #contact.sport_and_development #contact.tourism #contact.trade_transport #contact.translating_writing #contact.web_development #contact.writing_proposals # SF: Other contact.external_id = user.id # Save the object to Salesforce if not test_run: try: contact.save() success_count += 1 except Exception as e: error_count += 1 logger.error("Error while saving contact id {0}: ".format(user.id) + str(e))
def sync_users(dry_run, sync_from_datetime, loglevel): logger.setLevel(loglevel) error_count = 0 success_count = 0 users = Member.objects.all() if sync_from_datetime: users = users.filter(updated__gte=sync_from_datetime) logger.info("Syncing {0} User objects.".format(users.count())) for user in users: logger.debug("Syncing User: {0}".format(user.id)) # Find the corresponding SF user. try: contact = SalesforceContact.objects.get(external_id=user.id) except SalesforceContact.DoesNotExist: contact = SalesforceContact() except Exception as e: logger.error("Error while loading sfcontact id {0} - stopping: ".format(user.id) + str(e)) return success_count, error_count+1 # Populate the data from the source contact.external_id = user.id contact.user_name = user.username if re_email.match(user.email.upper()): contact.email = user.email else: logger.error("User has invalid e-mail address '{0}', member id {1}. " "Ignoring e-mail address field.".format(user.email, user.id)) contact.is_active = user.is_active contact.member_since = user.date_joined contact.date_joined = user.date_joined contact.deleted = user.deleted contact.category1 = Member.UserType.values[user.user_type].title() contact.first_name = user.first_name if user.last_name.strip(): contact.last_name = user.last_name else: contact.last_name = "1%MEMBER" contact.location = user.location contact.website = user.website contact.picture_location = "" if user.picture: contact.picture_location = str(user.picture) contact.about_me_us = user.about contact.why_one_percent_member = user.why contact.availability = user.available_time contact.facebook = user.facebook contact.twitter = user.twitter contact.skype = user.skypename contact.primary_language = user.primary_language contact.receive_newsletter = user.newsletter contact.phone = user.phone_number contact.birth_date = user.birthdate if user.gender == "male": contact.gender = Member.Gender.values['male'].title() elif user.gender == "female": contact.gender = Member.Gender.values['female'].title() else: contact.gender = "" contact.tags = "" for tag in user.tags.all(): contact.tags = str(tag) + ", " + contact.tags if user.address: contact.mailing_city = user.address.city contact.mailing_street = user.address.line1 + ' ' + user.address.line2 if user.address.country: contact.mailing_country = user.address.country.name else: contact.mailing_country = '' contact.mailing_postal_code = user.address.postal_code contact.mailing_state = user.address.state else: contact.mailing_city = '' contact.mailing_street = '' contact.mailing_country = '' contact.mailing_postal_code = '' contact.mailing_state = '' # Determine if the user has activated himself, by default assume not # if this is a legacy record, by default assume it has activated contact.has_activated = False try: rp = RegistrationProfile.objects.get(user_id=user.id) contact.tags = rp.activation_key if rp.activation_key == RegistrationProfile.ACTIVATED: contact.has_activated = True except RegistrationProfile.DoesNotExist: if not user.is_active and user.date_joined == user.last_login: contact.has_activated = False else: contact.has_activated = True contact.last_login = user.last_login # Bank details of recurring payments try: monthly_donor = MonthlyDonor.objects.get(user=user) contact.bank_account_city = monthly_donor.city contact.bank_account_holder = monthly_donor.name contact.bank_account_number = '' contact.bank_account_iban = monthly_donor.iban contact.bank_account_active_recurring_debit = monthly_donor.active except MonthlyDonor.DoesNotExist: contact.bank_account_city = '' contact.bank_account_holder = '' contact.bank_account_number = '' contact.bank_account_iban = '' contact.bank_account_active_recurring_debit = False # Save the object to Salesforce if not dry_run: try: contact.save() success_count += 1 except Exception as e: error_count += 1 logger.error("Error while saving contact id {0}: ".format(user.id) + str(e)) return success_count, error_count
def sync_users(test_run): users = BlueBottleUser.objects.all() logger.info("Syncing {0} User objects.".format(users.count())) for user in users: logger.info("Syncing User: {0}".format(user)) # Find the corresponding SF user. try: contact = SalesforceContact.objects.filter(external_id=user.id).get() except SalesforceContact.DoesNotExist: contact = SalesforceContact() # SF Layout: Subscription section - Set all the fields needed to save the user to the SF user. contact.category1 = user.user_type # Selectable type in Salesforce english contact.email = user.email contact.user_name = user.username contact.is_active = user.is_active # SF Layout: Profile section. # Note: SF last_name is required. if user.last_name: contact.last_name = user.last_name else: contact.last_name = "1%MEMBER" contact.first_name = user.first_name contact.member_since = user.date_joined contact.why_one_percent_member = user.why contact.about_me_us = user.about contact.location = user.location # The default: Organization(Account) will be 'Individual' as current. # Future purpose deactivate and put the Organization website group value #contact.organization_account = SalesforceOrganization.objects.filter(external_id=contact.organization.id).get() contact.website = user.website # SF Layout: Contact Information section. # Note: Fill with the number of activities of a member #contact.activity_number = # SF Layout: Contact Activity section. # -- back-end calculations with Ben #contact.amount_of_single_donations # -- 20130530 - Not used: discussed with Suzanne,Ben #contact.has_n_friends #contact.has_given_n_vouchers #contact.is_doing_n_tasks #contact.number_of_donations #contact.support_n_projects #contact.total_amount_of_donations # -- 20130530 - Not used: discussed with Suzanne,Ben #contact.total_number_of_received_messages # -- 20130530 - Not used: discussed with Suzanne,Ben #contact.total_number_of_sent_messages # SF Layout: Administrative (private) section. contact.birth_date = user.birthdate # Salesforce: needs to be updated to male/female/<empty string> instead m/f contact.gender = user.gender if user.address: contact.mailing_city = user.address.city contact.mailing_street = user.address.line1 + '\n' + user.address.line2 if user.address.country: contact.mailing_country = user.address.country.name else: contact.mailing_country = '' contact.mailing_postal_code = user.address.postal_code contact.mailing_state = user.address.state else: contact.mailing_city = '' contact.mailing_street = '' contact.mailing_country = '' contact.mailing_postal_code = '' contact.mailing_state = '' # SF Layout: My Skills section. # -- Unknown: #contact.which_1_would_you_like_to_contribute = # -- Unknown: #contact.available_time = # -- 20130530 - Not used: discussed with Suzanne,Ben #contact.where # SF Layout: My Settings section. contact.receive_newsletter = user.newsletter contact.primary_language = user.primary_language # SF Layout: All expertise section. -> 20130530 - TODO website by web-team #contact.administration_finance = #contact.agriculture_environment #contact.architecture #contact.computer_ict #contact.design #contact.economy_business #contact.education #contact.fund_raising #contact.graphic_design #contact.health #contact.internet_research #contact.law_and_politics #contact.marketing_pr #contact.online_marketing #contact.photo_video #contact.physics_technique #contact.presentations #contact.project_management #contact.psychology #contact.social_work #contact.sport_and_development #contact.tourism #contact.trade_transport #contact.translating_writing #contact.web_development #contact.writing_proposals # SF: Other contact.external_id = user.id # -- 20130530 - discussed with Suzanne, check at Liane #contact.tags = user.tags.get() # Save the SF user. if not test_run: contact.save()