MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / put

Method put

mongoengine/fields.py:1920–1982  ·  view source on GitHub ↗

Insert a image in database applying field properties (size, thumbnail_size)

(self, file_obj, **kwargs)

Source from the content-addressed store, hash-verified

1918 """Proxy for ImageField"""
1919
1920 def put(self, file_obj, **kwargs):
1921 """
1922 Insert a image in database
1923 applying field properties (size, thumbnail_size)
1924 """
1925 field = self.instance._fields[self.key]
1926 # Handle nested fields
1927 if hasattr(field, "field") and isinstance(field.field, FileField):
1928 field = field.field
1929
1930 try:
1931 img = Image.open(file_obj)
1932 img_format = img.format
1933 except Exception as e:
1934 raise ValidationError("Invalid image: %s" % e)
1935
1936 # Progressive JPEG
1937 # TODO: fixme, at least unused, at worst bad implementation
1938 progressive = img.info.get("progressive") or False
1939
1940 if (
1941 kwargs.get("progressive")
1942 and isinstance(kwargs.get("progressive"), bool)
1943 and img_format == "JPEG"
1944 ):
1945 progressive = True
1946 else:
1947 progressive = False
1948
1949 if field.size and (
1950 img.size[0] > field.size["width"] or img.size[1] > field.size["height"]
1951 ):
1952 size = field.size
1953
1954 if size["force"]:
1955 img = ImageOps.fit(img, (size["width"], size["height"]), LANCZOS)
1956 else:
1957 img.thumbnail((size["width"], size["height"]), LANCZOS)
1958
1959 thumbnail = None
1960 if field.thumbnail_size:
1961 size = field.thumbnail_size
1962
1963 if size["force"]:
1964 thumbnail = ImageOps.fit(img, (size["width"], size["height"]), LANCZOS)
1965 else:
1966 thumbnail = img.copy()
1967 thumbnail.thumbnail((size["width"], size["height"]), LANCZOS)
1968
1969 if thumbnail:
1970 thumb_id = self._put_thumbnail(thumbnail, img_format, progressive)
1971 else:
1972 thumb_id = None
1973
1974 w, h = img.size
1975
1976 io = BytesIO()
1977 img.save(io, img_format, progressive=progressive)

Callers

nothing calls this directly

Calls 6

_put_thumbnailMethod · 0.95
ValidationErrorClass · 0.90
thumbnailMethod · 0.80
getMethod · 0.45
saveMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected