Ejemplo n.º 1
0
 def test_send_file(self):
     data = {
         "geom": "POINT(0 0)",
         "properties": {
             self.property_key: 'data:image/png;name=change.png;base64,xxxxxxxxxxxxxxxxxxxxxxxxxx=='
         }
     }
     store_feature_files(self.feature_with_file_name, {})
     storage = get_storage()
     old_property_value = self.feature_with_file_name.properties.get(self.property_key)
     old_storage_file_path = old_property_value.split(';name=')[-1].split(';')[0]
     old_thumbnail = thumbnail_backend.get_thumbnail(old_storage_file_path, "500x500", crop='noop', upscale=False)
     self.assertTrue(storage.exists(old_thumbnail.name))
     self.assertTrue(storage.exists(old_storage_file_path))
     response = self.client.put(
         reverse('feature-detail',
                 args=(self.feature_with_file_name.layer_id,
                       self.feature_with_file_name.identifier)),
         data=data,
         format="json")
     self.assertFalse(storage.exists(old_storage_file_path))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     storage_file_path = generate_storage_file_path(self.property_key,
                                                    data['properties'].get(self.property_key),
                                                    self.feature_with_file_name)
     self.assertTrue(storage.exists(storage_file_path))
Ejemplo n.º 2
0
def stored_image_base64(value):
    """ As data-url file are stored in custom storage and not in b64, we need to prepare data to use """
    infos, content = get_info_content(value)
    infos = infos.split(';')
    data_type = infos[0]
    data_path = infos[1].strip('name=')
    storage = get_storage()
    with storage.open(data_path, 'rb') as data_file:
        file_bytes = data_file.read()
        file_b64 = base64.encodebytes(file_bytes)
        result = f"{data_type};base64," + file_b64.decode()
        return result
Ejemplo n.º 3
0
class Migration(migrations.Migration):

    dependencies = [
        ('terra_geocrud', '0028_auto_20191017_1351'),
    ]

    operations = [
        migrations.AlterField(
            model_name='featurepicture',
            name='image',
            field=sorl.thumbnail.fields.ImageField(
                storage=get_storage(),
                upload_to=terra_geocrud.models.feature_picture_directory_path),
        ),
    ]
Ejemplo n.º 4
0
class Migration(migrations.Migration):

    dependencies = [
        ('terra_geocrud',
         '0027_attachmentcategory_featureattachment_featurepicture'),
    ]

    operations = [
        migrations.AlterField(
            model_name='featureattachment',
            name='file',
            field=models.FileField(storage=get_storage(),
                                   upload_to=terra_geocrud.models.
                                   feature_attachment_directory_path),
        ),
        migrations.AlterField(
            model_name='featurepicture',
            name='image',
            field=sorl.thumbnail.fields.ImageField(
                storage=get_storage(),
                upload_to=terra_geocrud.models.
                feature_attachment_directory_path),
        ),
    ]
Ejemplo n.º 5
0
 def test_send_file(self):
     data = {
         "geom": "POINT(0 0)",
         "properties": {
             self.property_key: 'data:image/png;name=toto.png;base64,xxxxxxxxxxxxxxxxxxxxxxxxxx=='
         }
     }
     response = self.client.put(
         reverse('feature-detail',
                 args=(self.feature_with_file_name.layer_id,
                       self.feature_with_file_name.identifier)),
         data=data,
         format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     storage = get_storage()
     storage_file_path = generate_storage_file_path(self.property_key,
                                                    self.feature_with_file_name.properties.get(self.property_key),
                                                    self.feature_with_file_name)
     self.assertTrue(storage.exists(storage_file_path))
    def get_thumbnail(self, file_, geometry_string, **options):
        """
        Returns thumbnail as an ImageFile instance for file with geometry and
        options given. First it will try to get it from the key value store,
        secondly it will create it.

        Override from ThumbnailBackend to use different storage backend for image source
        """
        logger.debug('Getting thumbnail for file [%s] at [%s]', file_, geometry_string)

        if file_:
            source = ImageFile(file_, storage=get_storage())
        else:
            raise ValueError('falsey file_ argument in get_thumbnail()')

        # preserve image filetype
        if settings.THUMBNAIL_PRESERVE_FORMAT:
            options.setdefault('format', self._get_format(source))

        for key, value in self.default_options.items():
            options.setdefault(key, value)

        # For the future I think it is better to add options only if they
        # differ from the default settings as below. This will ensure the same
        # filenames being generated for new options at default.
        for key, attr in self.extra_options:
            value = getattr(settings, attr)
            if value != getattr(default_settings, attr):
                options.setdefault(key, value)

        name = self._get_thumbnail_filename(source, geometry_string, options)
        thumbnail = ImageFile(name, default.storage)
        cached = default.kvstore.get(thumbnail)

        if cached:
            return cached

        # We have to check exists() because the Storage backend does not
        # overwrite in some implementations.
        if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists():
            try:
                source_image = default.engine.get_image(source)
            except IOError as e:
                logger.exception(e)
                if settings.THUMBNAIL_DUMMY:
                    return DummyImageFile(geometry_string)
                else:
                    # if S3Storage says file doesn't exist remotely, don't try to
                    # create it and exit early.
                    # Will return working empty image type; 404'd image
                    logger.warning(
                        'Remote file [%s] at [%s] does not exist',
                        file_, geometry_string,
                    )
                    return thumbnail

            # We might as well set the size since we have the image in memory
            image_info = default.engine.get_image_info(source_image)
            options['image_info'] = image_info
            size = default.engine.get_image_size(source_image)
            source.set_size(size)

            try:
                self._create_thumbnail(source_image, geometry_string, options,
                                       thumbnail)
                self._create_alternative_resolutions(source_image, geometry_string,
                                                     options, thumbnail.name)
            finally:
                default.engine.cleanup(source_image)

        # If the thumbnail exists we don't create it, the other option is
        # to delete and write but this could lead to race conditions so I
        # will just leave that out for now.
        default.kvstore.get_or_set(source)
        default.kvstore.set(thumbnail, source)
        return thumbnail
Ejemplo n.º 7
0
from django.db.utils import IntegrityError
from django.test import override_settings
from django.test.testcases import TestCase
from geostore.models import Feature
from geostore.tests.factories import LayerFactory

from terra_geocrud.models import AttachmentCategory, feature_attachment_directory_path, \
    feature_picture_directory_path, CrudViewProperty, FeatureAttachment, PropertyEnum
from terra_geocrud.properties.files import get_storage
from terra_geocrud.tests import factories
from terra_geocrud.tests.factories import CrudViewFactory, FeaturePictureFactory, FeatureAttachmentFactory, \
    RoutingSettingsFactory, RoutingInformationFactory
from .. import models
from ..properties.schema import sync_layer_schema, sync_ui_schema

storage = get_storage()


class CrudModelMixinTestCase(TestCase):
    def test_str_method(self):
        class MyTestModel(models.CrudModelMixin):
            name = 'test'

        a = MyTestModel()
        self.assertEqual(a.name, str(a))


class CrudViewTestCase(TestCase):
    def setUp(self) -> None:
        self.crud_view = factories.CrudViewFactory(
            layer__schema={