瀏覽代碼

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