from django.core.management.base import NoArgsCommand
from django.core.mail import send_mail

from pombola.hansard.models import Alias


class Command(NoArgsCommand):
    help = 'Email a list of all the speaker names that have not been matched up to a real person'

    def handle_noargs(self, **options):
        unassigned = Alias.objects.unassigned()
        count = unassigned.count()

        if count is 0:
            return

        body = []
        body.append("There are {} Hansard speaker names that could not be matched to a person\n".format(count))
        body.append("Please go to http://info.mzalendo.com/admin/hansard/alias/ to update list\n")
        for alias in unassigned:
            body.append(u"\t'{}'".format(alias.alias))

        send_mail(
            '[Hansard] Unmatched speaker names report',
            '\n'.join(body),
            'no-reply@info.mzalendo.com',
            ['mzalendo-managers@mysociety.org'],
        )

Add command to email unassigned aliases
[KE] Add command to email unassigned aliases

This is currently done as part of the `bin/update_hansard.bash` script,
which runs the `hansard_list_unmatched_speakers` command. We want it to
be separate so that errors from running the Hansard update script go to
developers, and the unmatched aliases go to the Mzalendo managers.
