Ejemplo n.º 1
0
    def _get_value(self):
        value = os.environ.get(self.environment_name)
        if value:
            value = yaml_load(stream=value)
        else:
            value = self.default

        return yaml_dump(
            data=value, allow_unicode=True, default_flow_style=True, width=999
        ).replace('...\n', '').replace('\n', '')
Ejemplo n.º 2
0
    def serialize_value(value):
        result = yaml_dump(
            data=Setting.express_promises(value), allow_unicode=True,
            default_flow_style=False,
        )
        # safe_dump returns bytestrings
        # Disregard the last 3 dots that mark the end of the YAML document
        if force_text(s=result).endswith('...\n'):
            result = result[:-4]

        return result
Ejemplo n.º 3
0
    def dump_data(cls, filter_term=None, namespace=None):
        dictionary = {}

        if not namespace:
            namespace_dictionary = {}
            for _namespace in SettingNamespace.get_all():
                namespace_dictionary[_namespace.name] = {
                    'version': _namespace.version
                }

            dictionary[SMART_SETTINGS_NAMESPACES_NAME] = namespace_dictionary

        for setting in cls.get_all():
            # If a namespace is specified, filter the list by that namespace
            # otherwise return always True to include all (or not None == True)
            if (namespace and setting.namespace.name == namespace) or not namespace:
                if (filter_term and filter_term.lower() in setting.global_name.lower()) or not filter_term:
                    dictionary[setting.global_name] = Setting.express_promises(setting.value)

        return yaml_dump(
            data=dictionary, default_flow_style=False
        )
Ejemplo n.º 4
0
    def test_metadata_yaml_attachment(self):
        TEST_METADATA_VALUE_1 = 'test value 1'
        TEST_METADATA_VALUE_2 = 'test value 2'

        test_metadata_type_1 = MetadataType.objects.create(
            name='test_metadata_type_1')
        test_metadata_type_2 = MetadataType.objects.create(
            name='test_metadata_type_2')
        self.test_document_type.metadata.create(
            metadata_type=test_metadata_type_1)
        self.test_document_type.metadata.create(
            metadata_type=test_metadata_type_2)

        test_metadata_yaml = yaml_dump(
            data={
                test_metadata_type_1.name: TEST_METADATA_VALUE_1,
                test_metadata_type_2.name: TEST_METADATA_VALUE_2,
            })

        # Create email with a test attachment first, then the metadata.yaml
        # attachment
        with mail.get_connection(
                backend='django.core.mail.backends.locmem.EmailBackend'
        ) as connection:
            email_message = mail.EmailMultiAlternatives(
                body='test email body',
                connection=connection,
                subject='test email subject',
                to=['*****@*****.**'],
            )

            email_message.attach(
                filename='test_attachment',
                content='test_content',
            )

            email_message.attach(
                filename='metadata.yaml',
                content=test_metadata_yaml,
            )

            email_message.send()

        self._create_email_source()
        self.source.store_body = True
        self.source.save()

        EmailBaseModel.process_message(source=self.source,
                                       message_text=mail.outbox[0].message())

        self.assertEqual(Document.objects.count(), 2)

        for document in Document.objects.all():
            self.assertEqual(
                document.metadata.get(
                    metadata_type=test_metadata_type_1).value,
                TEST_METADATA_VALUE_1)
            self.assertEqual(
                document.metadata.get(
                    metadata_type=test_metadata_type_2).value,
                TEST_METADATA_VALUE_2)