def test_get_filename_form_filtered_dataset(self):
        """
        Test get filename from data_value for a filtered dataset.
        """
        obj = MagicMock()
        serializer = XFormManifestSerializer()

        obj.data_value = "xform 1 clinics"
        self.assertEqual(serializer.get_filename(obj), 'clinics.csv')
Пример #2
0
    def test_get_filename_form_filtered_dataset(self):
        """
        Test get filename from data_value for a filtered dataset.
        """
        obj = MagicMock()
        serializer = XFormManifestSerializer()

        obj.data_value = "xform 1 clinics"
        self.assertEqual(serializer.get_filename(obj), 'clinics.csv')
Пример #3
0
    def test_get_filename_from_url(self):
        """
        Test get filename from a URL.
        """
        obj = MagicMock()
        serializer = XFormManifestSerializer()

        obj.data_value = "http://example.com/"
        self.assertEqual(serializer.get_filename(obj), 'example.com')

        obj.data_value = "http://example.com/clinics.csv"
        self.assertEqual(serializer.get_filename(obj), 'clinics.csv')
    def test_get_filename_from_url(self):
        """
        Test get filename from a URL.
        """
        obj = MagicMock()
        serializer = XFormManifestSerializer()

        obj.data_value = "http://example.com/"
        self.assertEqual(serializer.get_filename(obj), 'example.com')

        obj.data_value = "http://example.com/clinics.csv"
        self.assertEqual(serializer.get_filename(obj), 'clinics.csv')
    def manifest(self, request, *args, **kwargs):
        self.object = self.get_object()
        object_list = MetaData.objects.filter(data_type='media',
                                              xform=self.object)
        context = self.get_serializer_context()
        serializer = XFormManifestSerializer(object_list, many=True,
                                             context=context)

        return Response(serializer.data, headers=self.get_openrosa_headers())
Пример #6
0
    def manifest(self, request, *args, **kwargs):
        self.object = self.get_object()
        object_list = MetaData.objects.filter(data_type='media',
                                              object_id=self.object.pk)
        context = self.get_serializer_context()
        context[GROUP_DELIMETER_TAG] = ExportBuilder.GROUP_DELIMITER_DOT
        serializer = XFormManifestSerializer(object_list,
                                             many=True,
                                             context=context)

        return Response(serializer.data, headers=self.get_openrosa_headers())
Пример #7
0
    def manifest(self, request, *args, **kwargs):
        self.object = self.get_object()
        object_list = MetaData.objects.filter(
            data_type__in=MetaData.MEDIA_FILES_TYPE, xform=self.object)
        context = self.get_serializer_context()
        serializer = XFormManifestSerializer(object_list,
                                             many=True,
                                             context=context)

        return Response(serializer.data,
                        headers=self.get_openrosa_headers(request,
                                                          location=False))
Пример #8
0
    def test_get_hash(self):
        """
        Test get hash for filtered dataset and unchanged hash for other
        media files
        """
        # create xform and make submissions
        self._create_user_and_login()
        self._publish_transportation_form()
        self._make_submissions()

        # filtered dataset hash regenerated
        obj = MagicMock()
        serializer = XFormManifestSerializer()

        obj.data_value = "xform {} test_dataset".format(self.xform.id)

        obj.file_hash = u'md5:b9cc8695c526f3c7aaa882234f3b9484'
        obj.data_file = ""
        self.assertNotEqual(serializer.get_hash(obj), obj.file_hash)

        # any other dataset with media files have their hashes unchanged
        obj.data_value = "an image upload.png"
        obj.data_file = "data file"
        self.assertEqual(serializer.get_hash(obj), obj.file_hash)
Пример #9
0
    def manifest(self, request, *args, **kwargs):
        self.object = self.get_object()
        media_files = {}
        expired_objects = False
        # Retrieve all media files for the current form
        queryset = MetaData.objects.filter(
            data_type__in=MetaData.MEDIA_FILES_TYPE, xform=self.object)
        object_list = queryset.all()

        # Keep only media files that are not considered as expired.
        # Expired files may have an out-of-date hash which needs to be refreshed
        # before being exposed to the serializer
        for obj in object_list:
            if not obj.has_expired:
                media_files[obj.pk] = obj
                continue
            expired_objects = True

        # Retrieve all media files for the current form again except non
        # expired ones. The expired objects should have an up-to-date hash now.
        if expired_objects:
            refreshed_object_list = queryset.exclude(pk__in=media_files.keys())
            for refreshed_object in refreshed_object_list.all():
                media_files[refreshed_object.pk] = refreshed_object

        # Sort objects all the time because EE calculates a hash of the
        # whole manifest to detect any changes the next time EE downloads it.
        # If no files changed, but the order did, the hash of the manifest
        # would be different and EE would display:
        # > "A new version of this form has been downloaded"
        media_files = dict(sorted(media_files.items()))
        context = self.get_serializer_context()
        serializer = XFormManifestSerializer(media_files.values(),
                                             many=True,
                                             context=context)

        return Response(serializer.data, headers=self.get_openrosa_headers())