Example #1
0
    def generate_email(self, node=None):
        if not node:
            # No genera mail de staff
            return None
        catalog = DataJson(node.catalog_url,
                           catalog_format=node.catalog_format)
        validation = catalog.validate_catalog(only_errors=True)
        validation_time = self._format_date(timezone.now())
        if validation['status'] == 'OK':
            msg = "Catálogo {} válido.".format(node.catalog_id)
            self.report_task.info(self.report_task, msg)
            return None
        context = {
            'validation_time': validation_time,
            'status': validation['status'],
            'catalog': validation['error']['catalog'],
            'dataset_list': validation['error']['dataset']
        }

        mail = self.render_templates(context)
        subject = u'[{}] Validacion de catálogo {}: {}'.format(
            settings.ENV_TYPE, node.catalog_id, validation_time)
        mail.subject = subject

        with NamedTemporaryFile(suffix='.xlsx') as tmpfile:
            catalog.validate_catalog(export_path=tmpfile.name)
            mail.attach('reporte_validacion_{}.xlsx'.format(node.catalog_id),
                        tmpfile.read())

        return mail
    def setUpTestData(cls):
        # set mock env
        settings.ENV_TYPE = 'tst'

        config = DynamicEmailConfiguration.get_solo()
        config.from_email = '*****@*****.**'
        config.save()

        # set mock nodes
        cls.node1 = Node.objects.create(
            catalog_id='id1',
            catalog_url=cls.get_sample('several_assorted_errors.json'),
            indexable=True)
        cls.node2 = Node.objects.create(
            catalog_id='id2',
            catalog_url=cls.get_sample('full_data.json'),
            indexable=True)

        cls.node1.admins.create(username='******',
                                password='******',
                                email='*****@*****.**',
                                is_staff=False)
        cls.node2.admins.create(username='******',
                                password='******',
                                email='*****@*****.**',
                                is_staff=False)

        cls.report_task = ValidationReportTask.objects.create()

        cls.validation_report_generator = ValidationReportGenerator(
            cls.report_task)

        catalog = DataJson(cls.get_sample('several_assorted_errors.json'))
        cls.report = catalog.validate_catalog(only_errors=True)