def generate_highcharts_config(self, docs, args):
        report = self.generate_report(docs, args)

        params = args.get('params') or {}
        chart_params = params.get('chart') or {}
        items = report.get('items') or []

        title = chart_params.get('title') or 'Changes to Featuremedia'
        subtitle = chart_params.get(
            'subtitle') or ChartConfig.gen_subtitle_for_dates(params)

        translations = ChartConfig.get_translations(['task.user', 'operation'])
        user_translations = (translations.get('task_user')
                             or {}).get('names') or {}
        operation_translations = (translations.get('operation')
                                  or {}).get('names') or {}
        rows = []

        def gen_date_str(date):
            return utc_to_local(app.config['DEFAULT_TIMEZONE'],
                                date).strftime('%d/%m/%Y %H:%M')

        for item in items:
            original_image = item.get('original_image') or {}
            updates = []

            for update in item.get('updates') or []:
                updates.append('{} - {} by {}'.format(
                    gen_date_str(parse_date(update.get('operation_created'))),
                    operation_translations.get(update.get('operation'))
                    or update.get('operation'),
                    user_translations.get(update.get('user')) or ''))

            rows.append([
                gen_date_str(item.get('versioncreated')),
                user_translations.get(item.get('original_creator')) or '',
                item.get('slugline') or '',
                original_image.get('headline')
                or original_image.get('alt_text') or '',
                '<ul><li>{}</li></ul>'.format('</li><li>'.join(updates))
            ])

        return {
            'highcharts': [{
                'id':
                'featuremedia_updates',
                'type':
                'table',
                'title':
                title,
                'subtitle':
                subtitle,
                'headers': [
                    'Date', 'Creator', 'Slugline', 'Original Image',
                    'Featuremedia History'
                ],
                'rows':
                rows
            }]
        }
    def _format_docs(self, docs):
        date_fields = {}

        for doc in docs:
            resource = 'events' if doc['type'] == 'event' else doc['type']

            if not date_fields.get(resource):
                date_fields[resource] = self._get_date_fields(resource)

            # Format root level date types
            for field in date_fields[resource]:
                if isinstance(doc.get(field), str):
                    doc[field] = parse_date(doc[field])

            # Format nested date types
            if resource == 'events' and doc.get('dates'):
                if doc['dates'].get('start'):
                    doc['dates']['start'] = parse_date(doc['dates']['start'])
                if doc['dates'].get('end'):
                    doc['dates']['end'] = parse_date(doc['dates']['end'])
                if (doc['dates'].get('recurring_rule') or {}).get('until'):
                    doc['dates']['recurring_rule']['until'] = parse_date(
                        doc['dates']['recurring_rule']['until'])
Ejemplo n.º 3
0
def parse_date_str(date):
    if date and isinstance(date, str):
        return parse_date(date)
    return date
Ejemplo n.º 4
0
 def test_parse_date_with_null(self):
     date = parse_date(None)
     self.assertIsNone(date)
Ejemplo n.º 5
0
 def test_parse_date(self):
     date = parse_date('2013-11-06T07:56:01.414944+00:00')
     self.assertIsInstance(date, datetime)
     self.assertEqual('07:56+0000', date.strftime('%H:%M%z'))
            def calc_timestamp(entry):
                entry['operation_timestamp'] = parse_date(
                    entry.get('operation_created')).timestamp()

                return entry
Ejemplo n.º 7
0
 def parse_display_dates(date):
     if type(date) == datetime:
         return date
     if date and type(date) == str:
         return parse_date(date)
Ejemplo n.º 8
0
def parse_dates(item):
    for field in ['firstcreated', 'versioncreated']:
        if item.get(field) and type(item[field]) == str:
            item[field] = parse_date(item[field])
Ejemplo n.º 9
0
 def test_parse_date_with_null(self):
     date = parse_date(None)
     self.assertIsNone(date)
Ejemplo n.º 10
0
 def test_parse_date(self):
     date = parse_date('2013-11-06T07:56:01.414944+00:00')
     self.assertIsInstance(date, datetime)
     self.assertEqual('07:56+0000', date.strftime('%H:%M%z'))