Exemplo n.º 1
0
    def generate_highcharts_config(self, docs, args):
        params = args.get('params') or {}
        aggs = args.get('aggs') or {}
        group = aggs.get('group') or {}
        subgroup = aggs.get('subgroup') or {}
        translations = args.get('translations') or {}

        chart = params.get('chart') or {}
        chart_type = chart.get('type') or 'bar'

        report = self.generate_report(docs, args)

        chart_config = ChartConfig('content_publishing', chart_type)

        group_keys = list(report['groups'].keys())
        if len(group_keys) == 1 and report.get('subgroups'):
            chart_config.add_source(
                subgroup.get('field'),
                report['subgroups']
            )
            chart_config.load_translations(group.get('field'))
        else:
            chart_config.add_source(group.get('field'), report.get('groups'))
            if report.get('subgroups'):
                chart_config.add_source(subgroup.get('field'), report['subgroups'])

        def gen_title():
            if chart.get('title'):
                return chart['title']

            group_type = group.get('field')
            group_title = chart_config.get_source_name(group_type)

            if len(group_keys) == 1 and report.get('subgroups'):
                data_name = chart_config.get_source_title(
                    group_type,
                    group_keys[0]
                )

                return 'Published Stories for {}: {}'.format(
                    group_title,
                    data_name
                )

            return 'Published Stories per {}'.format(group_title)

        def gen_subtitle():
            return ChartConfig.gen_subtitle_for_dates(params)

        chart_config.get_title = gen_title
        chart_config.get_subtitle = gen_subtitle
        chart_config.sort_order = chart.get('sort_order') or 'desc'

        chart_config.translations = translations

        report['highcharts'] = [chart_config.gen_config()]

        return report
    def test_translate_source(self):
        chart = ChartConfig('source', 'bar')
        chart.add_source('source', {'aap': 3, 'ftp': 1, 'ap': 5})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'source': {
                'title': 'Source',
                'names': {}
            }
        })
    def test_translate_authors(self):
        chart = ChartConfig('author', 'bar')
        chart.add_source('authors.parent', {'user1': 3, 'user2': 4, 'user3': 5})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'authors_parent': {
                'title': 'Author',
                'names': {
                    'user1': 'first user',
                    'user2': 'second user',
                    'user3': 'last user'
                }
            }
        })
    def test_translate_user(self):
        chart = ChartConfig('user', 'bar')
        chart.add_source('task.user', {'user1': 3, 'user2': 4, 'user3': 5})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'task_user': {
                'title': 'User',
                'names': {
                    'user1': 'first user',
                    'user2': 'second user',
                    'user3': 'last user'
                }
            }
        })
    def test_translate_desk(self):
        chart = ChartConfig('desk', 'bar')
        chart.add_source('task.desk', {'desk1': 4, 'desk2': 5, 'desk3': 1})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'task_desk': {
                'title': 'Desk',
                'names': {
                    'desk1': 'Politic Desk',
                    'desk2': 'Sports Desk',
                    'desk3': 'System Desk'
                }
            }
        })
    def test_translate_genre(self):
        chart = ChartConfig('genre', 'bar')
        chart.add_source('genre.qcode', {'Article': 4, 'Sidebar': 5, 'Factbox': 1})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'genre_qcode': {
                'title': 'Genre',
                'names': {
                    'Article': 'Article (news)',
                    'Sidebar': 'Sidebar',
                    'Factbox': 'Factbox'
                }
            }
        })
    def test_translate_anpa_category(self):
        chart = ChartConfig('catrgory', 'bar')
        chart.add_source('anpa_category.qcode', {'a': 3, 'b': 4, 'c': 1})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'anpa_category_qcode': {
                'title': 'Category',
                'names': {
                    'a': 'Advisories',
                    'b': 'Basketball',
                    'c': 'Cricket'
                }
            }
        })
    def test_translate_state(self):
        chart = ChartConfig('state', 'bar')
        chart.add_source('state', {'published': 3, 'killed': 1, 'updated': 5})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'state': {
                'title': 'State',
                'names': {
                    'published': 'Published',
                    'killed': 'Killed',
                    'corrected': 'Corrected',
                    'updated': 'Updated',
                }
            }
        })
    def test_translate_urgency(self):
        chart = ChartConfig('urgency', 'bar')
        chart.add_source('urgency', {1: 4, 3: 4, 5: 1})

        self.assertEqual(chart.translations, {})

        chart.load_translations()
        self.assertEqual(chart.translations, {
            'urgency': {
                'title': 'Urgency',
                'names': {
                    1: 1,
                    2: 2,
                    3: 3,
                    4: 4,
                    5: 5
                }
            }
        })