Exemplo n.º 1
0
    def testSmallerImageObject(self):
        image, buffer = self._make_image(width=641, height=481)
        buffer.seek(0, os.SEEK_END)
        orig_size = buffer.tell()
        buffer.seek(0)

        cimage = CachedImage("test_small")
        metadata = {'content_type': 'image/png'}
        cimage.cache_put(buffer, metadata)

        smaller = IMAGE_SIZE_ORDERING[0:7]
        bigger = IMAGE_SIZE_ORDERING[7:-1]  # skip the original

        for size_key in smaller:
            self.assertTrue(cimage.can_size(size_key))

            cmeta, cstream = cimage.get(size_key=size_key)
            self.assertEqual(cmeta['size_key'], size_key)
            cstream.seek(0, os.SEEK_END)
            stream_size = cstream.tell()
            self.assertLess(stream_size, orig_size)

        for size_key in bigger:
            self.assertFalse(cimage.can_size(size_key),
                             msg="size key: %s shouldn't be sized" % size_key)
            cmeta, cstream = cimage.get(size_key=size_key)
            cstream.seek(0, os.SEEK_END)
            stream_size = cstream.tell()
            self.assertEqual(stream_size, orig_size)
            self.assertNotEqual(cmeta['size_key'], size_key)
            self.assertEqual(cmeta['size_key'], OBJECT_ORIGINAL)
Exemplo n.º 2
0
def get_cached_case_attachment(domain, case_id, attachment_id, is_image=False):
    attachment_cache_key = "%(case_id)s_%(attachment)s" % {
        "case_id": case_id,
        "attachment": attachment_id
    }

    from dimagi.utils.django.cached_object import CachedObject, CachedImage
    cobject = CachedImage(attachment_cache_key) if is_image else CachedObject(attachment_cache_key)
    if not cobject.is_cached():
        content = CaseAccessors(domain).get_attachment_content(case_id, attachment_id)
        stream = StringIO(content.content_body)
        metadata = {'content_type': content.content_type}
        cobject.cache_put(stream, metadata)

    return cobject
Exemplo n.º 3
0
def get_cached_case_attachment(domain, case_id, attachment_id, is_image=False):
    attachment_cache_key = "%(case_id)s_%(attachment)s" % {
        "case_id": case_id,
        "attachment": attachment_id
    }

    from dimagi.utils.django.cached_object import CachedObject, CachedImage
    cobject = CachedImage(attachment_cache_key) if is_image else CachedObject(attachment_cache_key)
    if not cobject.is_cached():
        content = CaseAccessors(domain).get_attachment_content(case_id, attachment_id)
        stream = StringIO(content.content_body)
        metadata = {'content_type': content.content_type}
        cobject.cache_put(stream, metadata)

    return cobject
Exemplo n.º 4
0
    def testSmallerImageObject(self):
        image, buffer = self._make_image(width=641, height=481)
        buffer.seek(0, os.SEEK_END)
        orig_size = buffer.tell()
        buffer.seek(0)

        cimage = CachedImage("test_small")
        metadata = {'content_type': 'image/png'}
        cimage.cache_image(buffer, metadata)

        smaller = IMAGE_SIZE_ORDERING[0:7]
        bigger = IMAGE_SIZE_ORDERING[7:-1] # skip the original

        for size_key in smaller:
            self.assertTrue(cimage.can_size(size_key))

            cmeta, cstream = cimage.get_size(size_key)
            self.assertEqual(cmeta['size_key'], size_key)
            cstream.seek(0, os.SEEK_END)
            stream_size = cstream.tell()
            self.assertLess(stream_size, orig_size)

        for size_key in bigger:
            self.assertFalse(cimage.can_size(size_key), msg="size key: %s shouldn't be sized" % size_key)
            cmeta, cstream = cimage.get_size(size_key)
            cstream.seek(0, os.SEEK_END)
            stream_size = cstream.tell()
            self.assertEqual(stream_size, orig_size)
            self.assertNotEqual(cmeta['size_key'], size_key)
            self.assertEqual(cmeta['size_key'], OBJECT_ORIGINAL)
Exemplo n.º 5
0
    def testHugeImageObject(self):
        image, buffer = self._make_image()
        buffer.seek(0, os.SEEK_END)
        orig_size = buffer.tell()
        buffer.seek(0)

        cimage = CachedImage("test_huge")
        metadata = {'content_type': 'image/png'}
        cimage.cache_image(buffer, metadata)

        for size in IMAGE_SIZE_ORDERING:
            self.assertTrue(cimage.can_size(size))

            cmeta, cstream = cimage.get_size(size)
            self.assertEqual(cmeta['size_key'], size)
            cstream.seek(0, os.SEEK_END)
            stream_size = cstream.tell()

            if size != OBJECT_ORIGINAL:
                self.assertLess(stream_size, orig_size)
            else:
                self.assertEqual(stream_size, orig_size)
Exemplo n.º 6
0
    def testHugeImageObject(self):
        image, buffer = self._make_image()
        buffer.seek(0, os.SEEK_END)
        orig_size = buffer.tell()
        buffer.seek(0)

        cimage = CachedImage("test_huge")
        metadata = {'content_type': 'image/png'}
        cimage.cache_put(buffer, metadata)

        for size in IMAGE_SIZE_ORDERING:
            self.assertTrue(cimage.can_size(size))

            cmeta, cstream = cimage.get(size_key=size)
            self.assertEqual(cmeta['size_key'], size)
            cstream.seek(0, os.SEEK_END)
            stream_size = cstream.tell()

            if size != OBJECT_ORIGINAL:
                self.assertLess(stream_size, orig_size)
            else:
                self.assertEqual(stream_size, orig_size)
Exemplo n.º 7
0
    def fetch_case_image(cls, case_id, attachment_key, filesize_limit=0, width_limit=0, height_limit=0, fixed_size=None):
        """
        Return (metadata, stream) information of best matching image attachment.
        attachment_key is the case property of the attachment
        attachment filename is the filename of the original submission - full extension and all.
        """
        if fixed_size is not None:
            size_key = fixed_size
        else:
            size_key = OBJECT_ORIGINAL

        constraint_dict = {}
        if filesize_limit:
            constraint_dict['content_length'] = filesize_limit

        if height_limit:
            constraint_dict['height'] = height_limit

        if width_limit:
            constraint_dict['width'] = width_limit
        do_constrain = bool(constraint_dict)

        # if size key is None, then one of the limit criteria are set
        attachment_cache_key = "%(case_id)s_%(attachment)s" % {
            "case_id": case_id,
            "attachment": attachment_key,
        }

        cached_image = CachedImage(attachment_cache_key)
        meta, stream = cls.cache_and_get_object(cached_image, case_id, attachment_key, size_key=size_key)

        # now that we got it cached, let's check for size constraints

        if do_constrain:
            #check this size first
            #see if the current size matches the criteria

            def meets_constraint(constraints, meta):
                for c, limit in constraints.items():
                    if meta[c] > limit:
                        return False
                return True

            if meets_constraint(constraint_dict, meta):
                #yay, do nothing
                pass
            else:
                #this meta is no good, find another one
                lesser_keys = IMAGE_SIZE_ORDERING[0:IMAGE_SIZE_ORDERING.index(size_key)]
                lesser_keys.reverse()
                is_met = False
                for lesser_size in lesser_keys:
                    less_meta, less_stream = cached_image.get_size(lesser_size)
                    if meets_constraint(constraint_dict, less_meta):
                        meta = less_meta
                        stream = less_stream
                        is_met = True
                        break
                if not is_met:
                    meta = None
                    stream = None

        return meta, stream
Exemplo n.º 8
0
    def fetch_case_image(cls,
                         case_id,
                         attachment_key,
                         filesize_limit=0,
                         width_limit=0,
                         height_limit=0,
                         fixed_size=None):
        """
        Return (metadata, stream) information of best matching image attachment.
        attachment_key is the case property of the attachment
        attachment filename is the filename of the original submission - full extension and all.
        """
        if fixed_size is not None:
            size_key = fixed_size
        else:
            size_key = OBJECT_ORIGINAL

        constraint_dict = {}
        if filesize_limit:
            constraint_dict['content_length'] = filesize_limit

        if height_limit:
            constraint_dict['height'] = height_limit

        if width_limit:
            constraint_dict['width'] = width_limit
        do_constrain = bool(constraint_dict)

        # if size key is None, then one of the limit criteria are set
        attachment_cache_key = "%(case_id)s_%(attachment)s" % {
            "case_id": case_id,
            "attachment": attachment_key,
        }

        cached_image = CachedImage(attachment_cache_key)
        meta, stream = cls.cache_and_get_object(cached_image,
                                                case_id,
                                                attachment_key,
                                                size_key=size_key)

        # now that we got it cached, let's check for size constraints

        if do_constrain:
            #check this size first
            #see if the current size matches the criteria

            def meets_constraint(constraints, meta):
                for c, limit in constraints.items():
                    if meta[c] > limit:
                        return False
                return True

            if meets_constraint(constraint_dict, meta):
                #yay, do nothing
                pass
            else:
                #this meta is no good, find another one
                lesser_keys = IMAGE_SIZE_ORDERING[0:IMAGE_SIZE_ORDERING.
                                                  index(size_key)]
                lesser_keys.reverse()
                is_met = False
                for lesser_size in lesser_keys:
                    less_meta, less_stream = cached_image.get_size(lesser_size)
                    if meets_constraint(constraint_dict, less_meta):
                        meta = less_meta
                        stream = less_stream
                        is_met = True
                        break
                if not is_met:
                    meta = None
                    stream = None

        return meta, stream