def handle_noargs(self, **options):
        # Imports are here to avoid an import loop created when the Hansard
        # search indexes are checked
        from core.models import Person
        from scorecards.models import Category, Entry

        # create the category
        try:
            category = Category.objects.get(slug="hansard-appearances")
        except Category.DoesNotExist:
            raise ImproperlyConfigured(
                "Please create a scorecard category with the slug 'hansard-appearances'"
            )

        # Find all the people we should score for
        people = Person.objects.all().is_politician()

        # How far back should we look for hansard appearances?
        duration_string = "six months"
        lower_limit = datetime.date.today() - datetime.timedelta(183)

        for person in people:
            # NOTE: We could certainly do all this in a single query.
            hansard_count = person.hansard_entries.filter(
                sitting__start_date__gte=lower_limit).count()

            try:
                entry = person.scorecard_entries.get(category=category)
            except Entry.DoesNotExist:
                entry = Entry(content_object=person, category=category)

            if hansard_count < 6:
                entry.score = -1

                # deal with the various ways we need to phrase this
                if hansard_count == 0:
                    entry.remark = "Has not spoken in parliament in the last %s" % (
                        duration_string)
                elif hansard_count == 1:
                    entry.remark = "Only spoke once in parliament in the last %s" % (
                        duration_string)
                else:
                    entry.remark = "Hardly ever spoke in parliament, only %u times in the last %s" % (
                        hansard_count, duration_string)

            elif hansard_count < 60:
                entry.score = 0
                entry.remark = "Sometimes spoke in parliament, %u times in the last %s" % (
                    hansard_count, duration_string)
            else:
                entry.score = 1
                entry.remark = "Frequently spoke in parliament, %u times in the last %s" % (
                    hansard_count, duration_string)

            entry.date = datetime.date.today()

            entry.save()
    def handle_noargs(self, **options):
        # Imports are here to avoid an import loop created when the Hansard
        # search indexes are checked
        from core.models import Person
        from scorecards.models import Category, Entry

        # create the category
        try:
            category = Category.objects.get(slug="hansard-appearances")
        except Category.DoesNotExist:
            raise ImproperlyConfigured("Please create a scorecard category with the slug 'hansard-appearances'")

        # Find all the people we should score for
        people = Person.objects.all().is_mp()
        
        # How far back should we look for hansard appearances?
        duration_string = "six months"
        lower_limit = datetime.date.today() - datetime.timedelta(183)

        for person in people:
            # NOTE: We could certainly do all this in a single query.
            hansard_count = person.hansard_entries.filter(sitting__start_date__gte=lower_limit).count()

            try:
                entry = person.scorecard_entries.get(category=category)
            except Entry.DoesNotExist:
                entry = Entry(content_object=person, category=category)

            if hansard_count < 6:
                entry.score = -1

                # deal with the various ways we need to phrase this
                if hansard_count == 0:
                    entry.remark = "Has not spoken in parliament in the last %s" % ( duration_string )
                elif hansard_count == 1:
                    entry.remark = "Only spoke once in parliament in the last %s" % ( duration_string )
                else:
                    entry.remark = "Hardly ever spoke in parliament, only %u times in the last %s" % ( hansard_count, duration_string )

            elif hansard_count < 60:
                entry.score = 0
                entry.remark = "Sometimes spoke in parliament, %u times in the last %s" % ( hansard_count, duration_string )
            else:
                entry.score = 1
                entry.remark = "Frequently spoke in parliament, %u times in the last %s" % ( hansard_count, duration_string )
            
            entry.date = datetime.date.today()

            entry.save()
    def handle_noargs(self, **options):

        # Imports are here to avoid an import loop created when the Hansard
        # search indexes are checked
        from core.models import Person
        from scorecards.models import Category, Entry

        # create the category
        try:
            category = Category.objects.get(slug="contactability")
        except Category.DoesNotExist:
            raise ImproperlyConfigured(
                "Please create a scorecard category with the slug 'contactability'"
            )

        # Find all the people we should score for
        # TODO - limit to just some people (mps, candidates, etc)
        people = Person.objects.all()

        for person in people:
            # count the number of different forms of contact they have - sum them up
            # manually as trying to do the distinct(kind) on the queryset was overly
            # complicated.
            contact_kinds = {}
            for c in person.contacts.all():
                contact_kinds[c.kind.slug] = 1

            contact_count = len(contact_kinds.keys())

            # turn the count into a score
            score = -1
            if contact_count >= 3: score = 0
            if contact_count >= 4: score = 1

            try:
                entry = person.scorecard_entries.get(category=category)
            except Entry.DoesNotExist:
                entry = Entry(content_object=person, category=category)

            entry.date = datetime.date.today()
            entry.score = score

            if score == -1:
                entry.remark = "There are few ways to reach this person"
            elif score == 0:
                entry.remark = "There are some ways to reach this person"
            else:
                entry.remark = "There are many ways to reach this person"

            entry.save()
    def handle_noargs(self, **options):

        # Imports are here to avoid an import loop created when the Hansard
        # search indexes are checked
        from core.models import Person
        from scorecards.models import Category, Entry

        # create the category
        try:
            category = Category.objects.get(slug = "contactability")
        except Category.DoesNotExist:
            raise ImproperlyConfigured("Please create a scorecard category with the slug 'contactability'")

        # Find all the people we should score for
        # TODO - limit to just some people (mps, candidates, etc)
        people = Person.objects.all()
        
        for person in people:
            # count the number of different forms of contact they have - sum them up
            # manually as trying to do the distinct(kind) on the queryset was overly
            # complicated.
            contact_kinds = {}
            for c in person.contacts.all():
                contact_kinds[c.kind.slug] = 1
            
            contact_count = len( contact_kinds.keys() )
        
            # turn the count into a score
            score = -1
            if contact_count >= 3: score = 0
            if contact_count >= 4: score = 1
            
            try:
                entry = person.scorecard_entries.get( category=category )
            except Entry.DoesNotExist:
                entry = Entry(content_object=person, category=category )
            
            entry.date = datetime.date.today()
            entry.score = score

            if score == -1:
                entry.remark = "There are few ways to reach this person"
            elif score == 0:
                entry.remark = "There are some ways to reach this person"
            else:
                entry.remark = "There are many ways to reach this person"

            entry.save()