A simple representation of a file, which just has content, size, and a name.
| 129 | |
| 130 | |
| 131 | class SimpleUploadedFile(InMemoryUploadedFile): |
| 132 | """ |
| 133 | A simple representation of a file, which just has content, size, and a |
| 134 | name. |
| 135 | """ |
| 136 | |
| 137 | def __init__(self, name, content, content_type="text/plain"): |
| 138 | content = content or b"" |
| 139 | super().__init__( |
| 140 | BytesIO(content), None, name, content_type, len(content), None, None |
| 141 | ) |
| 142 | |
| 143 | @classmethod |
| 144 | def from_dict(cls, file_dict): |
| 145 | """ |
| 146 | Create a SimpleUploadedFile object from a dictionary with keys: |
| 147 | - filename |
| 148 | - content-type |
| 149 | - content |
| 150 | """ |
| 151 | return cls( |
| 152 | file_dict["filename"], |
| 153 | file_dict["content"], |
| 154 | file_dict.get("content-type", "text/plain"), |
| 155 | ) |
no outgoing calls