def most_seen_creators_by_works_card(work_kind=None, role_name=None, num=10):
    """
    Displays a card showing the Creators that are associated with the most Works.

    e.g.:

      {% most_seen_creators_by_works_card work_kind='movie' role_name='Director' num=5 %}
    """
    object_list = most_seen_creators_by_works(
                            work_kind=work_kind, role_name=role_name, num=num)

    object_list = chartify(object_list, 'num_works', cutoff=1)

    # Attempt to create a sensible card title...

    if role_name:
        # Yes, this pluralization is going to break at some point:
        creators_name = '{}s'.format(role_name.capitalize())
    else:
        creators_name = 'People/groups'

    if work_kind:
        works_name = Work.get_kind_name_plural(work_kind).lower()
    else:
        works_name = 'works'

    card_title = '{} with most {}'.format(creators_name, works_name)

    return {
        'card_title': card_title,
        'score_attr': 'num_works',
        'object_list': object_list,
    }
    def test_handle_empty_chart(self):
        "There was an error if all items in chart met the cutoff value."
        creator = IndividualCreatorFactory()
        creator.num_readings = 1

        chart = chartify([creator], 'num_readings', cutoff=1)

        self.assertEqual(len(chart), 0)
    def test_cutoff_value(self):
        "Should be possible to set a custom cutoff value."
        chart = chartify(self.creators, 'num_readings', cutoff=6)

        self.assertEqual(len(chart), 3)
        self.assertEqual(chart[0].chart_position, 1)
        self.assertEqual(chart[1].chart_position, 2)
        self.assertEqual(chart[2].chart_position, 2)
    def test_default_list(self):
        chart = chartify(self.creators, 'num_readings')

        self.assertEqual(len(chart), 4)
        self.assertEqual(chart[0].chart_position, 1)
        self.assertEqual(chart[1].chart_position, 2)
        self.assertEqual(chart[2].chart_position, 2)
        self.assertEqual(chart[3].chart_position, 4)
    def test_ensure_chartiness(self):
        "By default list should be empty if all objects have the same score."
        creators = IndividualCreatorFactory.create_batch(3)
        for c in creators:
            c.num_readings = 10

        chart = chartify(creators, 'num_readings')

        self.assertEqual(len(chart), 0)
    def test_ensure_chartiness_false(self):
        "Should be possible to disable the behaviour."
        creators = IndividualCreatorFactory.create_batch(3)

        for c in creators:
            c.num_readings = 10

        chart = chartify(creators, 'num_readings', ensure_chartiness=False)

        self.assertEqual(len(chart), 3)
    def test_cutoff_is_none(self):
        "Should include the 0-scoring item."
        chart = chartify(self.creators, 'num_readings', cutoff=None)

        self.assertEqual(len(chart), 5)
        self.assertEqual(chart[0].chart_position, 1)
        self.assertEqual(chart[1].chart_position, 2)
        self.assertEqual(chart[2].chart_position, 2)
        self.assertEqual(chart[3].chart_position, 4)
        self.assertEqual(chart[4].chart_position, 5)
def most_seen_creators_card(event_kind=None, num=10):
    """
    Displays a card showing the Creators that are associated with the most Events.
    """
    object_list = most_seen_creators(event_kind=event_kind, num=num)

    object_list = chartify(object_list, 'num_events', cutoff=1)

    return {
        'card_title': 'Most seen people/groups',
        'score_attr': 'num_events',
        'object_list': object_list,
    }
def most_seen_works_card(kind=None, num=10):
    """
    Displays a card showing the Works that are associated with the most Events.
    """
    object_list = most_seen_works(kind=kind, num=num)

    object_list = chartify(object_list, 'num_views', cutoff=1)

    if kind:
        card_title = 'Most seen {}'.format(
                                    Work.get_kind_name_plural(kind).lower())
    else:
        card_title = 'Most seen works'

    return {
        'card_title': card_title,
        'score_attr': 'num_views',
        'object_list': object_list,
        'name_attr': 'title',
        'use_cite': True,
    }