def parse_postmark(self, obj): from_field = (obj['FromFull']['Name'], obj['FromFull']['Email']) tos = [(o['Name'], o['Email']) for o in obj['ToFull']] ccs = [(o['Name'], o['Email']) for o in obj['CcFull']] attachments = [] for a in obj['Attachments']: attachment = BytesIO(base64.b64decode(a['Content'])) attachment.content_type = a['ContentType'] attachment.size = a['ContentLength'] attachment.name = a['Name'] attachment.create_date = None attachment.mod_date = None attachment.read_date = None attachments.append(attachment) return { 'msgobj': obj, 'date': self.parse_date(obj['Date']), 'subject': obj['Subject'], 'body': obj['TextBody'], 'html': obj['HtmlBody'], 'from': from_field, 'to': tos, 'cc': ccs, 'resent_to': [], 'resent_cc': [], 'attachments': attachments }
def parse_postmark(self, obj): from_field = (obj["FromFull"]["Name"], obj["FromFull"]["Email"]) tos = [(o["Name"], o["Email"]) for o in obj["ToFull"]] ccs = [(o["Name"], o["Email"]) for o in obj["CcFull"]] attachments = [] for a in obj["Attachments"]: attachment = BytesIO(base64.b64decode(a["Content"])) attachment.content_type = a["ContentType"] attachment.size = a["ContentLength"] attachment.name = a["Name"] attachment.create_date = None attachment.mod_date = None attachment.read_date = None attachments.append(attachment) return { "msgobj": obj, "date": self.parse_date(obj["Date"]), "subject": obj["Subject"], "body": obj["TextBody"], "html": obj["HtmlBody"], "from": from_field, "to": tos, "cc": ccs, "resent_to": [], "resent_cc": [], "attachments": attachments, }
def parse_attachment(self, message_part): content_disposition = message_part.get("Content-Disposition", None) if content_disposition: dispo_type, dispo_dict = self.parse_dispositions( content_disposition) if dispo_type == "attachment" or (dispo_type == 'inline' and 'filename' in dispo_dict): content_type = message_part.get("Content-Type", None) file_data = message_part.get_payload(decode=True) if file_data is None: payloads = message_part.get_payload() file_data = '\n\n'.join([p.as_string() for p in payloads]) try: file_data = file_data.encode('utf-8') except: pass attachment = BytesIO(file_data) attachment.content_type = message_part.get_content_type() attachment.size = len(file_data) attachment.name = None attachment.create_date = None attachment.mod_date = None attachment.read_date = None if "filename" in dispo_dict: attachment.name = dispo_dict['filename'] if content_type: _, content_dict = self.parse_dispositions(content_type) if 'name' in content_dict: attachment.name = content_dict['name'] if attachment.name is None and content_type == 'message/rfc822': p = Parser() msgobj = p.parse(BytesIO(attachment.getvalue())) subject = self.parse_header_field(msgobj['Subject']) if subject: attachment.name = '%s.eml' % subject[:45] if "create-date" in dispo_dict: attachment.create_date = dispo_dict[ 'create-date'] # TODO: datetime if "modification-date" in dispo_dict: attachment.mod_date = dispo_dict[ 'modification-date'] # TODO: datetime if "read-date" in dispo_dict: attachment.read_date = dispo_dict[ 'read-date'] # TODO: datetime return attachment return None
def create_image(filename, size=(100, 100), image_mode='RGB', image_format='png'): data = BytesIO() Image.new(image_mode, size).save(data, image_format) data.name = filename data.seek(0) return data
def parse_attachment(self, message_part): content_disposition = message_part.get("Content-Disposition", None) if content_disposition: dispo_type, dispo_dict = self.parse_dispositions(content_disposition) if dispo_type == "attachment" or (dispo_type == 'inline' and 'filename' in dispo_dict): content_type = message_part.get("Content-Type", None) file_data = message_part.get_payload(decode=True) if file_data is None: payloads = message_part.get_payload() file_data = '\n\n'.join([p.as_string() for p in payloads]) try: file_data = file_data.encode('utf-8') except: pass attachment = BytesIO(file_data) attachment.content_type = message_part.get_content_type() attachment.size = len(file_data) attachment.name = None attachment.create_date = None attachment.mod_date = None attachment.read_date = None if "filename" in dispo_dict: attachment.name = dispo_dict['filename'] if content_type: _, content_dict = self.parse_dispositions(content_type) if 'name' in content_dict: attachment.name = content_dict['name'] if attachment.name is None and content_type == 'message/rfc822': p = Parser() msgobj = p.parse(BytesIO(attachment.getvalue())) subject = self.parse_header_field(msgobj['Subject']) if subject: attachment.name = '%s.eml' % subject[:45] if "create-date" in dispo_dict: attachment.create_date = dispo_dict['create-date'] # TODO: datetime if "modification-date" in dispo_dict: attachment.mod_date = dispo_dict['modification-date'] # TODO: datetime if "read-date" in dispo_dict: attachment.read_date = dispo_dict['read-date'] # TODO: datetime return attachment return None
def parse_attachment(self, message_part): content_disposition = message_part.get("Content-Disposition", None) if content_disposition: dispo_type, dispo_dict = self.parse_dispositions(content_disposition) if dispo_type == "attachment" or (dispo_type == "inline" and "filename" in dispo_dict): content_type = message_part.get("Content-Type", None) file_data = message_part.get_payload(decode=True) if file_data is None: payloads = message_part.get_payload() file_data = "\n\n".join([p.as_string() for p in payloads]).encode("utf-8") attachment = BytesIO(file_data) attachment.content_type = message_part.get_content_type() attachment.size = len(file_data) attachment.name = None attachment.create_date = None attachment.mod_date = None attachment.read_date = None if "filename" in dispo_dict: attachment.name = dispo_dict["filename"] if content_type: _, content_dict = self.parse_dispositions(content_type) if "name" in content_dict: attachment.name = content_dict["name"] if attachment.name is None and content_type == "message/rfc822": p = Parser() msgobj = p.parse(BytesIO(attachment.getvalue())) subject = self.parse_header_field(msgobj["Subject"]) if subject: attachment.name = "%s.eml" % subject[:45] if "create-date" in dispo_dict: attachment.create_date = dispo_dict["create-date"] # TODO: datetime if "modification-date" in dispo_dict: attachment.mod_date = dispo_dict["modification-date"] # TODO: datetime if "read-date" in dispo_dict: attachment.read_date = dispo_dict["read-date"] # TODO: datetime return attachment return None
def test_comment_image_upload_invalid(self): """ comment image upload, invalid image """ utils.login(self) image = BytesIO(b'BAD\x02D\x01\x00;') image.name = 'image.gif' image.content_type = 'image/gif' files = {'image': SimpleUploadedFile(image.name, image.read()), } response = self.client.post(reverse('spirit:comment:image-upload-ajax'), HTTP_X_REQUESTED_WITH='XMLHttpRequest', data=files) res = json.loads(response.content.decode('utf-8')) self.assertIn('error', res.keys()) self.assertIn('image', res['error'].keys())
def test_comment_image_upload_invalid(self): """ comment image upload, invalid image """ utils.login(self) image = BytesIO(b"BAD\x02D\x01\x00;") image.name = "image.gif" image.content_type = "image/gif" files = {"image": SimpleUploadedFile(image.name, image.read())} response = self.client.post( reverse("spirit:comment:image-upload-ajax"), HTTP_X_REQUESTED_WITH="XMLHttpRequest", data=files ) res = json.loads(response.content.decode("utf-8")) self.assertIn("error", res.keys()) self.assertIn("image", res["error"].keys())
def _open(self, name, mode='rb'): """Open a file from database. @param name filename or relative path to file based on base_url. path should contain only "/", but not "\". Apache sends pathes with "/". If there is no such file in the db, returs None """ assert mode == 'rb', "You've tried to open binary file without specifying binary mode! You specified: %s"%mode row = self.cursor.execute("SELECT %s from %s where %s = '%s'"%(self.blob_column,self.db_table,self.fname_column,name) ).fetchone() if row is None: return None inMemFile = BytesIO(row[0]) inMemFile.name = name inMemFile.mode = mode retFile = File(inMemFile) return retFile