def test_registrar_users_plus_stats(self): ''' Returns data in the expected format. ''' r_list = registrar_users_plus_stats() self.assertIsInstance(r_list, list) self.assertGreater(len(r_list), 0) for user in r_list: self.assertIsInstance(user, dict) expected_keys = [ 'email', 'first_name', 'last_name', 'most_active_org', 'registrar_email', 'registrar_id', 'registrar_name', 'registrar_users', 'total_links', 'year_links' ] self.assertEqual(sorted(user.keys()), expected_keys) for key in [ 'email', 'first_name', 'last_name', 'registrar_email', 'registrar_name' ]: self.assertIsInstance(user[key], str) self.assertTrue(user[key]) perma_user = LinkUser.objects.get(email=user['email']) self.assertTrue(perma_user.registrar) self.assertTrue(perma_user.is_active) self.assertTrue(perma_user.is_confirmed) self.assertIsInstance(user['total_links'], int) self.assertIsInstance(user['year_links'], int) self.assertIsInstance(user['registrar_id'], int) self.assertIsInstance(user['most_active_org'], (Organization, type(None))) self.assertIsInstance(user['registrar_users'], QuerySet) self.assertGreaterEqual(len(user['registrar_users']), 1) for user in user['registrar_users']: self.assertIsInstance(user, LinkUser)
def test_registrar_users_plus_stats_cm(self): ''' Returns data in the expected format for Campaign Monitor. ''' r_list = registrar_users_plus_stats(destination="cm") self.assertEqual(type(r_list), list) self.assertGreater(len(r_list), 0) for user in r_list: self.assertEqual(type(user), dict) self.assertEqual(sorted(user.keys()), ['CustomFields','EmailAddress','Name' ]) for key in ['Name', 'EmailAddress']: self.assertEqual(type(user[key]), unicode) self.assertTrue(user[key]) self.assertEqual(type(user['CustomFields']), list) self.assertEqual(len(user['CustomFields']), 7) custom_field_list = [ 'MostActiveOrg', 'RegistrarId', 'RegistrarEmail', 'RegistrarName', 'RegistrarUsers', 'YearLinks', 'TotalLinks' ] for custom_field in user['CustomFields']: self.assertEqual(type(custom_field), dict) self.assertEqual(sorted(custom_field.keys()), ['Key', 'Value']) self.assertIn(custom_field['Key'], custom_field_list) self.assertEqual(type(custom_field['Value']), unicode) self.assertTrue(custom_field['Value']) perma_user = LinkUser.objects.get(email=user['EmailAddress']) self.assertTrue(perma_user.registrar) self.assertTrue(perma_user.is_active) self.assertTrue(perma_user.is_confirmed)
def test_registrar_users_plus_stats_cm(self): ''' Returns data in the expected format for Campaign Monitor. ''' r_list = registrar_users_plus_stats(destination="cm") self.assertEqual(type(r_list), list) self.assertGreater(len(r_list), 0) for user in r_list: self.assertEqual(type(user), dict) self.assertEqual(sorted(user.keys()), ['CustomFields', 'EmailAddress', 'Name']) for key in ['Name', 'EmailAddress']: self.assertEqual(type(user[key]), unicode) self.assertTrue(user[key]) self.assertEqual(type(user['CustomFields']), list) self.assertEqual(len(user['CustomFields']), 7) custom_field_list = [ 'MostActiveOrg', 'RegistrarId', 'RegistrarEmail', 'RegistrarName', 'RegistrarUsers', 'YearLinks', 'TotalLinks' ] for custom_field in user['CustomFields']: self.assertEqual(type(custom_field), dict) self.assertEqual(sorted(custom_field.keys()), ['Key', 'Value']) self.assertIn(custom_field['Key'], custom_field_list) self.assertEqual(type(custom_field['Value']), unicode) self.assertTrue(custom_field['Value']) perma_user = LinkUser.objects.get(email=user['EmailAddress']) self.assertTrue(perma_user.registrar) self.assertTrue(perma_user.is_active) self.assertTrue(perma_user.is_confirmed)
def test_registrar_users_plus_stats(self): ''' Returns data in the expected format. ''' r_list = registrar_users_plus_stats() self.assertEqual(type(r_list), list) self.assertGreater(len(r_list), 0) for user in r_list: self.assertEqual(type(user), dict) expected_keys = [ 'email', 'first_name', 'last_name', 'most_active_org', 'registrar_email', 'registrar_id', 'registrar_name', 'registrar_users', 'total_links', 'year_links' ] self.assertEqual(sorted(user.keys()), expected_keys) for key in ['email', 'first_name', 'last_name', 'registrar_email', 'registrar_name']: self.assertEqual(type(user[key]), unicode) self.assertTrue(user[key]) perma_user = LinkUser.objects.get(email=user['email']) self.assertTrue(perma_user.registrar) self.assertTrue(perma_user.is_active) self.assertTrue(perma_user.is_confirmed) self.assertEqual(type(user['total_links']), long) self.assertEqual(type(user['year_links']), int) self.assertEqual(type(user['registrar_id']), long) self.assertIsInstance(user['most_active_org'], (Organization, type(None))) self.assertEqual(type(user['registrar_users']), QuerySet) self.assertGreaterEqual(len(user['registrar_users']), 1) for user in user['registrar_users']: self.assertEqual(type(user), LinkUser)
def test_registrar_users_plus_stats_specific_registrars(self): ''' Returns data in the expected format. ''' r_list = registrar_users_plus_stats(registrars=Registrar.objects.filter(email='*****@*****.**')) self.assertEqual(type(r_list), list) self.assertEqual(len(r_list), 1) self.assertEqual(r_list[0]['registrar_email'], '*****@*****.**')
def cm_sync(): ''' Sync our current list of registrar users plus some associated metadata to Campaign Monitor. Run daily at 3am by celerybeat ''' reports = sync_cm_list(settings.CAMPAIGN_MONITOR_REGISTRAR_LIST, registrar_users_plus_stats(destination='cm')) if reports["import"]["duplicates_in_import_list"]: logger.error( "Duplicate reigstrar users sent to Campaign Monitor. Check sync logic." ) send_admin_email("Registrar Users Synced to Campaign Monitor", settings.DEFAULT_FROM_EMAIL, HttpRequest(), 'email/admin/sync_to_cm.txt', {"reports": reports}) return json.dumps(reports)
def cm_sync(): ''' Sync our current list of registrar users plus some associated metadata to Campaign Monitor. Run daily at 3am by celerybeat ''' reports = sync_cm_list(settings.CAMPAIGN_MONITOR_REGISTRAR_LIST, registrar_users_plus_stats(destination='cm')) if reports["import"]["duplicates_in_import_list"]: logger.error("Duplicate reigstrar users sent to Campaign Monitor. Check sync logic.") send_admin_email("Registrar Users Synced to Campaign Monitor", settings.DEFAULT_FROM_EMAIL, HttpRequest(), 'email/admin/sync_to_cm.txt', {"reports": reports}) return json.dumps(reports)
def ping_registrar_users(): ''' Sends an email to our current registrar users. See templates/email/registrar_user_ping.txt ''' import json, logging from django.http import HttpRequest from perma.email import send_user_email, send_admin_email, registrar_users_plus_stats logger = logging.getLogger(__name__) users = registrar_users_plus_stats() logger.info("Begin emailing registrar users.") send_count = 0 failed_list = [] for user in users: succeeded = send_user_email(user['email'], 'email/registrar_user_ping.txt', user) if succeeded: send_count += 1 else: failed_list.append(user.id) # Another option is to use Django's send_mass_email. # It's unclear which would be more performant in real life. # send_count = send_mass_user_email('email/registrar_user_ping.txt', # [(user['email'], user) for user in users]) logger.info("Done emailing registrar users.") if len(users) != send_count: if failed_list: msg = "Some registrar users were not emailed: {}. Check log for fatal SMTP errors.".format( str(failed_list)) else: msg = "Some registrar users were not emailed. Check log for fatal SMTP errors." logger.error(msg) result = "incomplete" else: result = "ok" send_admin_email("Registrar Users Emailed", settings.DEFAULT_FROM_EMAIL, HttpRequest(), 'email/admin/pinged_registrar_users.txt', { "users": users, "result": result }) return json.dumps({"result": result, "send_count": send_count})
def ping_registrar_users(): """ Sends an email to our current registrar users. See templates/email/registrar_user_ping.txt """ import json, logging from django.http import HttpRequest from perma.email import send_user_email, send_admin_email, registrar_users_plus_stats logger = logging.getLogger(__name__) users = registrar_users_plus_stats() logger.info("Begin emailing registrar users.") send_count = 0 failed_list = [] for user in users: succeeded = send_user_email(user["email"], "email/registrar_user_ping.txt", user) if succeeded: send_count += 1 else: failed_list.append(user.id) # Another option is to use Django's send_mass_email. # It's unclear which would be more performant in real life. # send_count = send_mass_user_email('email/registrar_user_ping.txt', # [(user['email'], user) for user in users]) logger.info("Done emailing registrar users.") if len(users) != send_count: if failed_list: msg = "Some registrar users were not emailed: {}. Check log for fatal SMTP errors.".format(str(failed_list)) else: msg = "Some registrar users were not emailed. Check log for fatal SMTP errors." logger.error(msg) result = "incomplete" else: result = "ok" send_admin_email( "Registrar Users Emailed", settings.DEFAULT_FROM_EMAIL, HttpRequest(), "email/admin/pinged_registrar_users.txt", {"users": users, "result": result}, ) return json.dumps({"result": result, "send_count": send_count})
def ping_registrar_users(limit_to="", limit_by_tag="", exclude="", exclude_by_tag=""): ''' Sends an email to our current registrar users. See templates/email/registrar_user_ping.txt Arguments should be strings, with multiple values separated by semi-colons e.g. fab ping_registrar_users:limit_to="14;27;30",exclude_by_tag="opted_out" Limit filters are applied before exclude filters. ''' import json, logging from django.http import HttpRequest from perma.models import Registrar from perma.email import send_user_email, send_admin_email, registrar_users_plus_stats logger = logging.getLogger(__name__) registrars = Registrar.objects.all() if limit_to: registrars = registrars.filter(id__in=limit_to.split(";")) if limit_by_tag: registrars = registrars.filter( tags__name__in=limit_by_tag.split(";")).distinct() if exclude: registrars = registrars.exclude(id__in=exclude.split(";")) if exclude_by_tag: registrars = registrars.exclude( tags__name__in=exclude_by_tag.split(";")).distinct() users = registrar_users_plus_stats(registrars=registrars) logger.info("Begin emailing registrar users.") send_count = 0 failed_list = [] for user in users: succeeded = send_user_email(user['email'], 'email/registrar_user_ping.txt', user) if succeeded: send_count += 1 else: failed_list.append(user.id) # Another option is to use Django's send_mass_email. # It's unclear which would be more performant in real life. # send_count = send_mass_user_email('email/registrar_user_ping.txt', # [(user['email'], user) for user in users]) logger.info("Done emailing registrar users.") if len(users) != send_count: if failed_list: msg = "Some registrar users were not emailed: {}. Check log for fatal SMTP errors.".format( str(failed_list)) else: msg = "Some registrar users were not emailed. Check log for fatal SMTP errors." logger.error(msg) result = "incomplete" else: result = "ok" send_admin_email("Registrar Users Emailed", settings.DEFAULT_FROM_EMAIL, HttpRequest(), 'email/admin/pinged_registrar_users.txt', { "users": users, "result": result }) return json.dumps({"result": result, "send_count": send_count})
def ping_registrar_users(limit_to="", limit_by_tag="", exclude="", exclude_by_tag="", email="stats", year=""): ''' Sends an email to our current registrar users. See templates/email/registrar_user_ping.txt Arguments should be strings, with multiple values separated by semi-colons e.g. fab ping_registrar_users:limit_to="14;27;30",exclude_by_tag="opted_out",email="special" Limit filters are applied before exclude filters. ''' import json, logging from datetime import datetime from django.http import HttpRequest from perma.models import Registrar from perma.email import send_user_email, send_self_email, registrar_users, registrar_users_plus_stats logger = logging.getLogger(__name__) registrars = Registrar.objects.all() if limit_to: registrars = registrars.filter(id__in=limit_to.split(";")) if limit_by_tag: registrars = registrars.filter( tags__name__in=limit_by_tag.split(";")).distinct() if exclude: registrars = registrars.exclude(id__in=exclude.split(";")) if exclude_by_tag: registrars = registrars.exclude( tags__name__in=exclude_by_tag.split(";")).distinct() if year: year = int(year) else: year = datetime.now().year - 1 if email == 'stats': template = 'email/registrar_user_ping.txt' users = registrar_users_plus_stats(registrars=registrars, year=year) elif email == 'special': # update special template as desired, to send one-off emails # update email.registrar_users if you need more context variables template = 'email/special.txt' users = registrar_users(registrars=registrars) else: NotImplementedError() logger.info("Begin emailing registrar users.") send_count = 0 failed_list = [] for user in users: context = {} context.update(user) context["year"] = year succeeded = send_user_email(user['email'], template, context) if succeeded: send_count += 1 else: failed_list.append(user.id) # Another option is to use Django's send_mass_email. # It's unclear which would be more performant in real life. # send_count = send_mass_user_email('email/registrar_user_ping.txt', # [(user['email'], user) for user in users]) logger.info("Done emailing registrar users.") if len(users) != send_count: if failed_list: msg = "Some registrar users were not emailed: {}. Check log for fatal SMTP errors.".format( str(failed_list)) else: msg = "Some registrar users were not emailed. Check log for fatal SMTP errors." logger.error(msg) result = "incomplete" else: result = "ok" send_self_email("Registrar Users Emailed", HttpRequest(), 'email/admin/pinged_registrar_users.txt', { "users": users, "result": result }) return json.dumps({"result": result, "send_count": send_count})
def ping_registrar_users(limit_to="", limit_by_tag="", exclude="", exclude_by_tag="", email="stats", year=""): ''' Sends an email to our current registrar users. See templates/email/registrar_user_ping.txt Arguments should be strings, with multiple values separated by semi-colons e.g. fab ping_registrar_users:limit_to="14;27;30",exclude_by_tag="opted_out",email="special" Limit filters are applied before exclude filters. ''' import json, logging from datetime import datetime from django.http import HttpRequest from perma.models import Registrar from perma.email import send_user_email, send_admin_email, registrar_users, registrar_users_plus_stats logger = logging.getLogger(__name__) registrars = Registrar.objects.all() if limit_to: registrars = registrars.filter(id__in=limit_to.split(";")) if limit_by_tag: registrars = registrars.filter(tags__name__in=limit_by_tag.split(";")).distinct() if exclude: registrars = registrars.exclude(id__in=exclude.split(";")) if exclude_by_tag: registrars = registrars.exclude(tags__name__in=exclude_by_tag.split(";")).distinct() if year: year = int(year) else: year = datetime.now().year - 1 if email == 'stats': template = 'email/registrar_user_ping.txt' users = registrar_users_plus_stats(registrars=registrars, year=year) elif email == 'special': # update special template as desired, to send one-off emails # update email.registrar_users if you need more context variables template = 'email/special.txt' users = registrar_users(registrars=registrars) else: NotImplementedError() logger.info("Begin emailing registrar users.") send_count = 0 failed_list = [] for user in users: context = {} context.update(user) context["year"] = year succeeded = send_user_email(user['email'], template, context) if succeeded: send_count += 1 else: failed_list.append(user.id) # Another option is to use Django's send_mass_email. # It's unclear which would be more performant in real life. # send_count = send_mass_user_email('email/registrar_user_ping.txt', # [(user['email'], user) for user in users]) logger.info("Done emailing registrar users.") if len(users) != send_count: if failed_list: msg = "Some registrar users were not emailed: {}. Check log for fatal SMTP errors.".format(str(failed_list)) else: msg = "Some registrar users were not emailed. Check log for fatal SMTP errors." logger.error(msg) result = "incomplete" else: result = "ok" send_admin_email("Registrar Users Emailed", settings.DEFAULT_FROM_EMAIL, HttpRequest(), 'email/admin/pinged_registrar_users.txt', {"users": users, "result": result}) return json.dumps({"result": result, "send_count": send_count})