Example #1
0
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)
Example #2
0
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)
Example #3
0
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})
Example #4
0
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})
Example #5
0
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})
Example #6
0
File: dev.py Project: leonidg/perma
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})