def email_subscribed(sender, **kwargs): f1_id = kwargs.get('merges').get('f1id') if f1_id: p = People() p.subscribe(f1_id) else: mail_admins('Person Subscribe: %s' % kwargs.get('email',''), '%s' % kwargs)
def handle(self, *args, **options): if not settings.MAILCHIMP_API_KEY: raise CommandError('No MAILCHIMP_API_KEY has been defined in settings.') chimp = mailchimp.chimpy.Connection(settings.MAILCHIMP_API_KEY) lists_to_sync = EmailList.objects.all() for list_to_sync in lists_to_sync: subscribe = [] unsubscribe = [] errors = { 'no_email' : [], 'missing_data' : [], 'wrong_attribute' : [], } count = 0 page = 1 last_sync_date = None try: last_sync_date = SyncLog.objects.filter(email_list=list_to_sync).latest('date_added').date_added except SyncLog.DoesNotExist: pass self.stdout.write('Syncing with list: %s, FellowshipOne attribute: %s' % (list_to_sync, list_to_sync.f1_attribute_id)) while True: p = People() search_args = { 'includeInactive' : False, 'includeDeceased' : False, 'attribute' : list_to_sync.f1_attribute_id, 'page' : page, } if last_sync_date: search_args['lastUpdatedDate'] = last_sync_date.strftime('%Y-%m-%d') content = p.search_and_include(['communications','attributes'], **search_args) for person in content.get('results').get('person', []): if not self.has_attribute(person, list_to_sync.f1_attribute_id): errors['wrong_attribute'].append(person) continue email = self.get_email_communication(person) if not email: errors['no_email'].append(person) continue if not person.get('firstName') or not person.get('lastName') or not person.get('@id'): errors['missing_data'].append(person) continue new_person = { 'F1ID' : person.get('@id'), 'FNAME' : person.get('firstName'), 'LNAME' : person.get('lastName'), 'EMAIL' : email, } if person.get('unsubscribed') == 'true': unsubscribe.append(new_person) else: subscribe.append(new_person) count += 1 if int(content.get('results').get('@additionalPages')) == 0: break else: page += 1 mailchimp_lists = chimp.lists() mailchimp_list_id = None for m_list in mailchimp_lists: if m_list.get('name') == list_to_sync.mailchimp_list_name: mailchimp_list_id = m_list.get('id') if not mailchimp_list_id: raise CommandError('Mailchimp list with name "%s" does not exist.' % list_to_sync.mailchimp_list_name) if len(subscribe) > 0: sub_chunks = chunks(subscribe,5000) for chunk in sub_chunks: chimp.list_batch_subscribe(mailchimp_list_id,chunk,double_optin=False,update_existing=True,replace_interests=False) if len(unsubscribe) > 0: unsub_chunks = chunks(unsubscribe,5000) for chunk in unsub_chunks: chimp.list_batch_unsubscribe(mailchimp_list_id,[e['EMAIL'] for e in chunk],delete_member=True,send_goodbye=False,send_notify=False) log_statement = '%d records processed | Subscribes: %d | Unsubscribes: %d | Errors: email:%d missing_data:%d wrong_attribute:%d' % ( count, len(subscribe), len(unsubscribe), len(errors.get('no_email')), len(errors.get('missing_data')),len(errors.get('wrong_attribute')) ) log = SyncLog.objects.create(email_list=list_to_sync,statement='--- %s | %s ' % (list_to_sync.mailchimp_list_name,log_statement)) self.stdout.write(log.statement.replace(' | ','\n'))