from config import DEFAULT_ATTACHMENT_LOCATION, DEFAULT_ATTACHMENT_TYPE from StringIO import StringIO import base64 class MaloneAttachment(object): def importRoundupFile(self, db, fileId, title): """ import an attached file from roundup as Malone requires a title for the attached file we take the summary of the message that contain the attachment (title) """ attachment = db.getnode('file', fileId) self.mimetype = attachment.get('type') self.name = attachment.get('name') self.content = StringIO(db.file.get(fileId, 'content')) self.title = title self.location = '%s/%s' % (DEFAULT_ATTACHMENT_LOCATION, self.name) def toXML(self, doc, xmlComment): """ this method is meant to be used in the bug after all comments """ #ATTACHMENT attachmentEl = doc.createElement("attachment") location = self.location.replace(" ", "%20") attachmentEl.setAttribute("href", location) xmlComment.appendChild(attachmentEl) # TYPE typeEl = doc.createElement("type") attachmentEl.appendChild(typeEl) type = doc.createTextNode(DEFAULT_ATTACHMENT_TYPE) typeEl.appendChild(type) # TITLE titleEl = doc.createElement("title") attachmentEl.appendChild(titleEl) title = doc.createTextNode(self.title) titleEl.appendChild(title) # MIMETYPE mimetypeEl = doc.createElement("mimetype") attachmentEl.appendChild(mimetypeEl) mimetype = doc.createTextNode(self.mimetype) mimetypeEl.appendChild(mimetype) # CONTENTS contentsEl = doc.createElement("contents") attachmentEl.appendChild(contentsEl) contentBase64 = base64.b64encode(self.content.read()) if contentBase64 == "": contentBase64 = base64.b64encode("Empty attachment") content = doc.createTextNode(contentBase64) contentsEl.appendChild(content)