コード例 #1
0
def fetch_case_image(domain, case_id, attachment_id, filesize_limit=0, width_limit=0, height_limit=0, fixed_size=None):
    """
    Return (metadata, stream) information of best matching image attachment.

    :param attachment_id: the identifier of the attachment to fetch
    """
    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)

    cached_image = get_cached_case_attachment(domain, case_id, attachment_id, is_image=True)
    meta, stream = cached_image.get(size_key=size_key)

    if do_constrain:

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

        if not meets_constraint(constraint_dict, meta):
            # 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
コード例 #2
0
def fetch_case_image(domain, case_id, attachment_id, filesize_limit=0, width_limit=0, height_limit=0, fixed_size=None):
    """
    Return (metadata, stream) information of best matching image attachment.

    :param attachment_id: the identifier of the attachment to fetch
    """
    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)

    cached_image = get_cached_case_attachment(domain, case_id, attachment_id, is_image=True)
    meta, stream = cached_image.get(size_key=size_key)

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

        if not meets_constraint(constraint_dict, meta):
            # 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
コード例 #3
0
ファイル: models.py プロジェクト: dslowikowski/commcare-hq
    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