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})