소스 검색

add: image as dict work without maintype specified
It's redundant to specify maintype as it should be image when we embed
such to the body. Also slightly reworded the exception message to
prevent printing image data.

Mikael Koli 4 년 전
부모
커밋
80a62226a5
1개의 변경된 파일9개의 추가작업 그리고 3개의 파일을 삭제
  1. 9 3
      redmail/email/body.py

+ 9 - 3
redmail/email/body.py

@@ -192,11 +192,17 @@ class HTMLBody(Body):
 
             elif isinstance(img, dict):
                 # Expecting dict explanation of bytes
-                # ie. {"maintype": "image", "subtype": "png", "content"}
-                required_keys = ("content", "maintype", "subtype")
+                # ie. {"maintype": "image", "subtype": "png", "content": b'...'}
+
+                # Setting defaults
+                img['maintype'] = img.get('maintype', 'image')
+
+                # Validation
+                required_keys = ("content", "subtype")
                 if any(key not in img for key in required_keys):
                     missing_keys = tuple(key for key in required_keys if key not in img)
-                    raise KeyError(f"Image {repr(img)} missing keys: {missing_keys}")
+                    raise KeyError(f"Dict representation of an image missing keys: {missing_keys}")
+                
                 img_content = img.pop("content")
                 kwds = img