def setUp(self): ProductResourceTestCase.setUp(self) self.global_env.path = os.path.join(tempfile.gettempdir(), "trac-tempenv") if os.path.exists(self.global_env.path): shutil.rmtree(self.global_env.path) os.mkdir(self.global_env.path) attachment = Attachment(self.global_env, "ticket", 1) attachment.description = "Global Bar" attachment.insert("foo.txt", StringIO(""), 0) attachment = Attachment(self.env1, "ticket", 1) attachment.description = "Product Bar" attachment.insert("foo.txt", StringIO(""), 0) self.resource = resource.Resource("ticket", 1).child("attachment", "foo.txt")
def addTicket(self, ticket, source): '''Add a ticket from a dict''' db = self._env.get_db_cnx() cursor = db.cursor() idCountRes = cursor.execute('select count(*) as count from ticket where id = %s', (ticket['id'],)) idCount = idCountRes.fetchone()[0] assert idCount == 0, 'Ticket %s found in %s' % (ticket['id'], self.name) insertMainTicketQuery = 'insert into ticket (id, type, time, changetime, component, severity, priority, owner, \ reporter, cc, version, milestone, status, resolution, summary, description, keywords) \ values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' insertMainTicketValues = (ticket['id'], ticket['type'], ticket['time'], ticket['changetime'], ticket['component'], \ ticket['severity'], ticket['priority'], ticket['owner'], ticket['reporter'], ticket['cc'], ticket['version'], \ ticket['milestone'], ticket['status'], ticket['resolution'], ticket['summary'], ticket['description'], ticket['keywords']) insertMainTicketRes = cursor.execute(insertMainTicketQuery, insertMainTicketValues) #so we know where the ticket came from insertTicketSourceQuery = 'insert into ticket_custom (ticket, name, value) values (%s, %s, %s)' insertTicketSourceValues = (ticket['id'], 'project', source) insertTicketSourceRes = cursor.execute(insertTicketSourceQuery, insertTicketSourceValues) insertTicketChangeQuery = 'insert into ticket_change (ticket, time, author, field, oldvalue, newvalue) values (%s, %s, %s, %s, %s, %s)' for ticket_change in ticket['ticket_change']: insertTicketChangeValues = (ticket['id'], ticket_change['time'], ticket_change['author'], ticket_change['field'], ticket_change['oldvalue'], ticket_change['newvalue']) insertTicketChangeRes = cursor.execute(insertTicketChangeQuery, insertTicketChangeValues) for a in ticket['attachment']: ticketAttach = Attachment(self._env, 'ticket', ticket['id']) ticketAttach.description = a['description'] ticketAttach.author = a['author'] ticketAttach.ipnr = a['ipnr'] ticketAttach.insert(a['filename'], a['fileobj'], a['size'], t=a['time']) db.commit()
def import_wiki_attachments(self, template_path): """Imports wiki attachments from template using the Attachment API.""" # check that there are attachments to import template_attachment_path = os.path.join(template_path, 'attachments', 'wiki') if os.path.isdir(template_attachment_path): # clear the wiki attachment table @self.env.with_transaction() def clear_attachments(db): """Clears any wiki attachments from the current attachment table.""" cursor = db.cursor() cursor.execute("DELETE FROM attachment WHERE type='wiki'") # move attachment file into the env and insert database row filepath = os.path.join(template_path, 'attachment.xml') tree = ET.ElementTree(file=filepath) for att in tree.getroot(): attachment = Attachment(self.env, 'wiki', att.attrib['parent_id']) attachment.description = att.text try: fileobj = open(os.path.join(template_attachment_path, att.attrib['parent_id'], unicode_quote(att.attrib['name']))) attachment.insert(att.attrib['name'], fileobj, att.attrib['size']) except IOError: self.log.info("Unable to import attachment %s", att.attrib['name'])
def _create_attachment(self, req, ticket, upload, description): if hasattr(upload, 'filename'): attachment = Attachment(self.env, 'ticket', ticket.id) if hasattr(upload.file, 'fileno'): size = os.fstat(upload.file.fileno())[6] else: upload.file.seek(0, 2) size = upload.file.tell() upload.file.seek(0) if size == 0: raise TracError(_("Can't upload empty file")) max_size = self.env.config.get('attachment', 'max_size') if max_size >= 0 and size > max_size: raise TracError( _('Maximum attachment size: %(num)s bytes', num=max_size), _('Upload failed')) filename = unicodedata.normalize('NFC', unicode(upload.filename, 'utf-8')) filename = filename.replace('\\', '/').replace(':', '/') filename = os.path.basename(filename) if not filename: raise TracError(_('No file uploaded')) attachment.description = description if 'author' in req.args: attachment.author = get_reporter_id(req, 'author') attachment.ipnr = req.remote_addr attachment.insert(filename, upload.file, size)
def image_setup(tc): add_pages(tc, ['page:fr']) from trac.attachment import Attachment tc.env.path = tempfile.mkdtemp(prefix='trac-tempenv-') attachment = Attachment(tc.env, 'wiki', 'page:fr') attachment.description = "image in page:fr" attachment.insert('img.png', StringIO(''), 0, 2)
def addWikiPage(self, page, attachments): '''Add a wiki page as a list of dictionaries''' db = self._env.get_db_cnx() cursor = db.cursor() pageCountRes = cursor.execute( 'select count(*) as count from wiki where name = %s', (page[0]['name'], )) pageCount = pageCountRes.fetchone()[0] assert pageCount == 0, 'Page %s found in %s' % (page[0]['name'], self.name) insertWikiQuery = 'insert into wiki (name, version, time, author, ipnr, text, comment, readonly) values (%s, %s, %s, %s, %s, %s, %s, %s)' for pV in page: insertValues = (pV['name'], pV['version'], pV['time'], pV['author'], pV['ipnr'], pV['text'], pV['comment'], pV['readonly']) insertRes = cursor.execute(insertWikiQuery, insertValues) for a in attachments: pageAttach = Attachment(self._env, 'wiki', pV['name']) pageAttach.description = a['description'] pageAttach.author = a['author'] pageAttach.ipnr = a['ipnr'] pageAttach.insert(a['filename'], a['fileobj'], a['size'], t=a['time']) db.commit()
def _process_attachment(self, req, config, build): resource_id = req.args['member'] == 'config' \ and build.config or build.resource.id upload = req.args['file'] if not upload.file: send_error(req, message="Attachment not received.") self.log.debug('Received attachment %s for attaching to build:%s', upload.filename, resource_id) # Determine size of file upload.file.seek(0, 2) # to the end size = upload.file.tell() upload.file.seek(0) # beginning again # Delete attachment if it already exists try: old_attach = Attachment(self.env, 'build', parent_id=resource_id, filename=upload.filename) old_attach.delete() except ResourceNotFound: pass # Save new attachment attachment = Attachment(self.env, 'build', parent_id=resource_id) attachment.description = req.args.get('description', '') attachment.author = req.authname attachment.insert(upload.filename, upload.file, size) self._send_response(req, 201, 'Attachment created', headers={ 'Content-Type': 'text/plain', 'Content-Length': str(len('Attachment created'))})
def _create_attachment(self, req, ticket, upload, description): if hasattr(upload, 'filename'): attachment = Attachment(self.env, 'ticket', ticket.id) if hasattr(upload.file, 'fileno'): size = os.fstat(upload.file.fileno())[6] else: upload.file.seek(0, 2) size = upload.file.tell() upload.file.seek(0) if size == 0: raise TracError(_("Can't upload empty file")) max_size = self.env.config.get('attachment', 'max_size') if max_size >= 0 and size > max_size: raise TracError(_('Maximum attachment size: %(num)s bytes', num=max_size), _('Upload failed')) filename = unicodedata.normalize('NFC', unicode(upload.filename, 'utf-8')) filename = filename.replace('\\', '/').replace(':', '/') filename = os.path.basename(filename) if not filename: raise TracError(_('No file uploaded')) attachment.description = description if 'author' in req.args: attachment.author = get_reporter_id(req, 'author') attachment.ipnr = req.remote_addr attachment.insert(filename, upload.file, size)
def putAttachmentEx(self, req, pagename, filename, description, data, replace=True): """ Attach a file to a Wiki page. Returns the (possibly transformed) filename of the attachment. Use this method if you don't care about WikiRPC compatibility. """ if not WikiPage(self.env, pagename).exists: raise ResourceNotFound, 'Wiki page "%s" does not exist' % pagename if replace: try: attachment = Attachment(self.env, 'wiki', pagename, filename) req.perm(attachment.resource).require('ATTACHMENT_DELETE') attachment.delete() except TracError: pass attachment = Attachment(self.env, 'wiki', pagename) req.perm(attachment.resource).require('ATTACHMENT_CREATE') attachment.author = req.authname attachment.description = description attachment.insert(filename, StringIO(data.data), len(data.data)) return attachment.filename
def test_download_zip(self): att = Attachment(self.env, 'parent_realm', 'parent_id') att.description = 'Blah blah' att.insert('foo.txt', StringIO('foo'), 3, datetime(2016, 9, 23, 12, 34, 56, tzinfo=utc)) att = Attachment(self.env, 'parent_realm', 'parent_id') att.insert('bar.jpg', StringIO('bar'), 3, datetime(2016, 12, 14, 23, 56, 30, tzinfo=utc)) module = AttachmentModule(self.env) req = MockRequest(self.env, args={'format': 'zip'}, path_info='/attachment/parent_realm/parent_id/') self.assertTrue(module.match_request(req)) self.assertRaises(RequestDone, module.process_request, req) z = zipfile.ZipFile(req.response_sent, 'r') self.assertEqual(['bar.jpg', 'foo.txt'], sorted(i.filename for i in z.infolist())) zinfo = z.getinfo('foo.txt') self.assertEqual('foo', z.read('foo.txt')) self.assertEqual(3, zinfo.file_size) self.assertEqual((2016, 9, 23, 12, 34, 56), zinfo.date_time) self.assertEqual('Blah blah', zinfo.comment) zinfo = z.getinfo('bar.jpg') self.assertEqual('bar', z.read('bar.jpg')) self.assertEqual(3, zinfo.file_size) self.assertEqual((2016, 12, 14, 23, 56, 30), zinfo.date_time) self.assertEqual('', zinfo.comment)
def image_setup(tc): add_pages(tc, ['page:fr']) from trac.attachment import Attachment tc.env.path = tempfile.mkdtemp(prefix='trac-tempenv-') attachment = Attachment(tc.env, 'wiki', 'page:fr') attachment.description = "image in page:fr" attachment.insert('img.png', StringIO(''), 0, 2)
def image_setup(tc): add_pages(tc, ['page:fr']) from trac.attachment import Attachment tc.env.path = os.path.join(tempfile.gettempdir(), 'trac-tempenv') attachment = Attachment(tc.env, 'wiki', 'page:fr') attachment.description = "image in page:fr" attachment.insert('img.png', StringIO(''), 0, 2)
def _create_attachment(self, req, tid, upload, description): attachment = Attachment(self.env, "ticket", tid) if hasattr(upload.file, "fileno"): size = os.fstat(upload.file.fileno())[6] else: upload.file.seek(0, 2) size = upload.file.tell() upload.file.seek(0) if size == 0: raise TracError(_("Can't upload empty file")) max_size = self.env.config.get("attachment", "max_size") if 0 <= max_size < size: raise TracError(_("Maximum attachment size: %(num)s bytes", num=max_size), _("Upload failed")) filename = _normalized_filename(upload.filename) if not filename: raise TracError(_("No file uploaded")) attachment.description = description attachment.author = get_reporter_id(req, "author") attachment.ipnr = req.remote_addr attachment.insert(filename, upload.file, size)
def setUp(self): ProductResourceTestCase.setUp(self) self.global_env.path = os.path.join(tempfile.gettempdir(), 'trac-tempenv') if os.path.exists(self.global_env.path): shutil.rmtree(self.global_env.path) os.mkdir(self.global_env.path) attachment = Attachment(self.global_env, 'ticket', 1) attachment.description = 'Global Bar' attachment.insert('foo.txt', StringIO(''), 0) attachment = Attachment(self.env1, 'ticket', 1) attachment.description = 'Product Bar' attachment.insert('foo.txt', StringIO(''), 0) self.resource = resource.Resource('ticket', 1).child('attachment', 'foo.txt')
def addAttachment(self, ticket_id, filename, datafile, filesize, author, description, upload_time): # copied from bugzilla2trac attachment = Attachment(self.env, 'ticket', ticket_id) attachment.author = author attachment.description = description attachment.insert(filename, datafile, filesize, upload_time) del attachment
def addAttachment(self, ticket_id, filename, datafile, filesize, author, description, upload_time): # copied from bugzilla2trac attachment = Attachment(self.env, 'ticket', ticket_id) attachment.author = author attachment.description = description attachment.insert(filename, datafile, filesize, upload_time) del attachment
def setUp(self): ProductResourceTestCase.setUp(self) self.global_env.path = os.path.join(tempfile.gettempdir(), 'trac-tempenv') if os.path.exists(self.global_env.path): shutil.rmtree(self.global_env.path) os.mkdir(self.global_env.path) attachment = Attachment(self.global_env, 'ticket', 1) attachment.description = 'Global Bar' attachment.insert('foo.txt', StringIO(''), 0) attachment = Attachment(self.env1, 'ticket', 1) attachment.description = 'Product Bar' attachment.insert('foo.txt', StringIO(''), 0) self.resource = resource.Resource('ticket', 1).child('attachment', 'foo.txt')
def image_setup(tc): add_pages(tc, ['page:fr']) from trac.attachment import Attachment tc.env.path = tempfile.mkdtemp(prefix='trac-tempenv-') attachment = Attachment(tc.env, 'wiki', 'page:fr') attachment.description = "image in page:fr" attachment.insert('img.png', StringIO(''), 0, 2) htdocs_location = 'http://assets.example.org/common' tc.context.req.chrome['htdocs_location'] = htdocs_location tc.env.config.set('trac', 'htdocs_location', htdocs_location)
def image_setup(tc): add_pages(tc, ['page:fr']) from trac.attachment import Attachment tc.env.path = tempfile.mkdtemp(prefix='trac-tempenv-') attachment = Attachment(tc.env, 'wiki', 'page:fr') attachment.description = "image in page:fr" attachment.insert('img.png', StringIO(''), 0, 2) tc.env.config.set('interwiki', 'shields', 'https://img.shields.io/') tc.env.config.set('interwiki', 'travis', 'https://travis-ci.org/$1?branch=$2') htdocs_location = 'http://assets.example.org/common' tc.context.req.chrome['htdocs_location'] = htdocs_location tc.env.config.set('trac', 'htdocs_location', htdocs_location)
def addAttachment(self, author, a): if a["filename"] != "": description = a["description"] id = a["bug_id"] filename = a["filename"] filedata = StringIO.StringIO(a["thedata"]) filesize = len(filedata.getvalue()) time = a["creation_ts"] print " ->inserting attachment '%s' for ticket %s -- %s" % (filename, id, description) attachment = Attachment(self.env, "ticket", id) attachment.author = author attachment.description = description attachment.insert(filename, filedata, filesize, datetime2epoch(time)) del attachment
def addAttachment(self, author, a): description = a['description'].encode('utf-8') id = a['bug_id'] filename = a['filename'].encode('utf-8') filedata = StringIO.StringIO(a['thedata'].tostring()) filesize = len(filedata.getvalue()) time = a['creation_ts'] print " ->inserting attachment '%s' for ticket %s -- %s" % \ (filename, id, description) attachment = Attachment(self.env, 'ticket', id) attachment.author = author attachment.description = description attachment.insert(filename, filedata, filesize, time.strftime('%s')) del attachment
def attach(self, ticket, image): attachment = Attachment(self.env, 'ticket', ticket.id) attachment.author = ticket['reporter'] attachment.description = ticket['summary'] image.file.seek(0,2) # seek to end of file size = image.file.tell() filename = image.filename image.file.seek(0) attachment.insert(filename, image.file, size) # XXX shouldn't this only be called for, like, the # first image or whenever you really want to set the default? from imagetrac.default_image import DefaultTicketImage if self.env.is_component_enabled(DefaultTicketImage): DefaultTicketImage(self.env).set_default(ticket.id, filename)
def addAttachment(self, author, a): if a['filename'] != '': description = a['description'] id = a['bug_id'] filename = a['filename'] filedata = StringIO.StringIO(a['thedata']) filesize = len(filedata.getvalue()) time = a['creation_ts'] print " ->inserting attachment '%s' for ticket %s -- %s" % \ (filename, id, description) attachment = Attachment(self.env, 'ticket', id) attachment.author = author attachment.description = description attachment.insert(filename, filedata, filesize, datetime2epoch(time)) del attachment
def attach(self, ticket, image): attachment = Attachment(self.env, 'ticket', ticket.id) attachment.author = ticket['reporter'] attachment.description = ticket['summary'] image.file.seek(0, 2) # seek to end of file size = image.file.tell() filename = image.filename image.file.seek(0) attachment.insert(filename, image.file, size) # XXX shouldn't this only be called for, like, the # first image or whenever you really want to set the default? from imagetrac.default_image import DefaultTicketImage if self.env.is_component_enabled(DefaultTicketImage): DefaultTicketImage(self.env).set_default(ticket.id, filename)
def addAttachment(self, author, a): description = a['description'].encode('utf-8') id = a['bug_id'] filename = a['filename'].encode('utf-8') filedata = StringIO.StringIO(a['thedata'].tostring()) filesize = len(filedata.getvalue()) time = a['creation_ts'] print " ->inserting attachment '%s' for ticket %s -- %s" % \ (filename, id, description) attachment = Attachment(self.env, 'ticket', id) attachment.author = author attachment.description = description attachment.insert(filename, filedata, filesize, time.strftime('%s')) del attachment
def putAttachment(self, req, ticket, filename, description, data, replace=True): """ Add an attachment, optionally (and defaulting to) overwriting an existing one. Returns filename.""" if not model.Ticket(self.env, ticket).exists: raise TracError, 'Ticket "%s" does not exist' % ticket if replace: try: attachment = Attachment(self.env, 'ticket', ticket, filename) attachment.delete() except TracError: pass attachment = Attachment(self.env, 'ticket', ticket) attachment.author = req.authname or 'anonymous' attachment.description = description attachment.insert(filename, StringIO(data.data), len(data.data)) return attachment.filename
def addAttachment(self, author, a): if a['filename'] != '': description = a['description'] id = a['bug_id'] filename = a['filename'] filedata = io.BytesIO(a['thedata']) filesize = len(filedata.getvalue()) time = a['creation_ts'] print(" ->inserting attachment '%s' for ticket %s -- %s" % (filename, id, description)) attachment = Attachment(self.env, 'ticket', id) attachment.author = author attachment.description = description attachment.insert(filename, filedata, filesize, datetime2epoch(time)) del attachment
def addTicket(self, ticket, source): '''Add a ticket from a dict''' db = self._env.get_db_cnx() cursor = db.cursor() idCountRes = cursor.execute( 'select count(*) as count from ticket where id = %s', (ticket['id'], )) idCount = idCountRes.fetchone()[0] assert idCount == 0, 'Ticket %s found in %s' % (ticket['id'], self.name) insertMainTicketQuery = 'insert into ticket (id, type, time, changetime, component, severity, priority, owner, \ reporter, cc, version, milestone, status, resolution, summary, description, keywords) \ values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' insertMainTicketValues = (ticket['id'], ticket['type'], ticket['time'], ticket['changetime'], ticket['component'], \ ticket['severity'], ticket['priority'], ticket['owner'], ticket['reporter'], ticket['cc'], ticket['version'], \ ticket['milestone'], ticket['status'], ticket['resolution'], ticket['summary'], ticket['description'], ticket['keywords']) insertMainTicketRes = cursor.execute(insertMainTicketQuery, insertMainTicketValues) #so we know where the ticket came from insertTicketSourceQuery = 'insert into ticket_custom (ticket, name, value) values (%s, %s, %s)' insertTicketSourceValues = (ticket['id'], 'project', source) insertTicketSourceRes = cursor.execute(insertTicketSourceQuery, insertTicketSourceValues) insertTicketChangeQuery = 'insert into ticket_change (ticket, time, author, field, oldvalue, newvalue) values (%s, %s, %s, %s, %s, %s)' for ticket_change in ticket['ticket_change']: insertTicketChangeValues = (ticket['id'], ticket_change['time'], ticket_change['author'], ticket_change['field'], ticket_change['oldvalue'], ticket_change['newvalue']) insertTicketChangeRes = cursor.execute(insertTicketChangeQuery, insertTicketChangeValues) for a in ticket['attachment']: ticketAttach = Attachment(self._env, 'ticket', ticket['id']) ticketAttach.description = a['description'] ticketAttach.author = a['author'] ticketAttach.ipnr = a['ipnr'] ticketAttach.insert(a['filename'], a['fileobj'], a['size'], t=a['time']) db.commit()
def putAttachmentEx(self, req, pagename, filename, description, data, replace=True): """ Attach a file to a Wiki page. Returns the (possibly transformed) filename of the attachment. Use this method if you don't care about WikiRPC compatibility. """ if not WikiPage(self.env, pagename).exists: raise TracError, 'Wiki page "%s" does not exist' % pagename if replace: try: attachment = Attachment(self.env, 'wiki', pagename, filename) attachment.delete() except TracError: pass attachment = Attachment(self.env, 'wiki', pagename) attachment.author = req.authname or 'anonymous' attachment.description = description attachment.insert(filename, StringIO(data.data), len(data.data)) return attachment.filename
def putAttachment(self, req, ticket, filename, description, data, replace=True): """ Add an attachment, optionally (and defaulting to) overwriting an existing one. Returns filename.""" if not model.Ticket(self.env, ticket).exists: raise ResourceNotFound('Ticket "%s" does not exist' % ticket) if replace: try: attachment = Attachment(self.env, 'ticket', ticket, filename) req.perm(attachment.resource).require('ATTACHMENT_DELETE') attachment.delete() except TracError: pass attachment = Attachment(self.env, 'ticket', ticket) req.perm(attachment.resource).require('ATTACHMENT_CREATE') attachment.author = req.authname attachment.description = description attachment.insert(filename, StringIO(data.data), len(data.data)) return attachment.filename
def addWikiPage(self, page, attachments): '''Add a wiki page as a list of dictionaries''' db = self._env.get_db_cnx() cursor = db.cursor() pageCountRes = cursor.execute('select count(*) as count from wiki where name = %s', (page[0]['name'],)) pageCount = pageCountRes.fetchone()[0] assert pageCount == 0, 'Page %s found in %s' % (page[0]['name'], self.name) insertWikiQuery = 'insert into wiki (name, version, time, author, ipnr, text, comment, readonly) values (%s, %s, %s, %s, %s, %s, %s, %s)' for pV in page: insertValues = (pV['name'], pV['version'], pV['time'], pV['author'], pV['ipnr'], pV['text'], pV['comment'], pV['readonly']) insertRes = cursor.execute(insertWikiQuery, insertValues) for a in attachments: pageAttach = Attachment(self._env, 'wiki', pV['name']) pageAttach.description = a['description'] pageAttach.author = a['author'] pageAttach.ipnr = a['ipnr'] pageAttach.insert(a['filename'], a['fileobj'], a['size'], t=a['time']) db.commit()
def get_uploaded_file_href(self, req, user, field, req_field): """Returns uploaded file's url @param req: trac.web.req @param user: tracusermanager.api.User @param field: str @param req_field: str @return: str """ # validate request field upload = req.args.get(req_field, None) if upload == None or not hasattr(upload, 'filename') or not upload.filename: return user[field] if hasattr(upload.file, 'fileno'): size = os.fstat(upload.file.fileno())[6] else: upload.file.seek(0, 2) # seek to end of file size = upload.file.tell() upload.file.seek(0) if size == 0: raise TracError(_("Can't upload empty file")) filename = upload.filename filename = filename.replace('\\', '/').replace(':', '/') filename = os.path.basename(filename) if not filename: raise TracError(_('No file uploaded')) page = WikiPage(self.env, self.attachments_wikiPage) if not page.exists: page.text="= UserManager's Attachments =" page.save( 'trac', 'Page created by tracusermanager.profile component', req.remote_addr) attachment = Attachment(self.env, 'wiki', self.attachments_wikiPage) attachment.author = get_reporter_id(req, 'author') attachment.ipnr = req.remote_addr attachment.description = (_("%s\'s Avatar") % (user.username)) attachment.insert('_'.join([user.username, filename]), upload.file, size) return req.href('/'.join(['raw-attachment', 'wiki',self.attachments_wikiPage, attachment.filename]))
def test_add_attachment_html_notification(self): ticket = Ticket(self.env) ticket['description'] = 'Some ticket description' ticket['summary'] = 'Some ticket summary' ticket['type'] = 'defect' ticket['status'] = 'new' ticket.insert() attachment = Attachment(self.env, ticket) attachment.description = "`Some` '''!WikiFormatted''' ''text''" attachment.filename = 'somefile.txt' event = TicketChangeEvent('ticket', 'changed', ticket, author='user1', attachment=attachment) actual = self.tf.format([], 'ticket', 'text/html', event) filename = resource_filename(__name__, 'attachment_notification.html') file = open(filename, 'r') expected = file.read() file.close() self.assertEqual(expected, actual)
def putAttachmentEx(self, req, pagename, filename, description, data, replace=True): """ Attach a file to a Wiki page. Returns the (possibly transformed) filename of the attachment. Use this method if you don't care about WikiRPC compatibility. """ if not WikiPage(self.env, pagename).exists: raise ResourceNotFound, 'Wiki page "%s" does not exist' % pagename if replace: try: attachment = Attachment(self.env, "wiki", pagename, filename) req.perm(attachment.resource).require("ATTACHMENT_DELETE") attachment.delete() except TracError: pass attachment = Attachment(self.env, "wiki", pagename) req.perm(attachment.resource).require("ATTACHMENT_CREATE") attachment.author = req.authname attachment.description = description attachment.insert(filename, StringIO(data.data), len(data.data)) return attachment.filename
def test_attachment(self): attachment = Attachment(self.env, 'ticket', 42) attachment.description = 'Summary line' attachment.author = 'Santa' attachment.ipnr = 'northpole.example.com' attachment.insert('foo.txt', StringIO('Lorem ipsum dolor sit amet'), 0) so = self._get_so() self.assertEquals('%s:attachment:ticket:42:foo.txt' % self.basename, so.doc_id) self.assertEquals('attachment', so.realm) self.assertEquals('foo.txt', so.id) self.assertEquals('ticket', so.parent_realm) self.assertEquals(42, so.parent_id) self.assertTrue('foo.txt' in so.title) self.assertEquals('Santa', so.author) self.assertEquals(attachment.date, so.created) self.assertEquals(attachment.date, so.changed) self.assertTrue('Santa' in so.involved) #self.assertTrue('Lorem ipsum' in so.oneline) # TODO self.assertTrue('Lorem ipsum' in so.body.read()) self.assertTrue('Summary line' in so.comments)
def create_sizes(self, ticket, attachment): """create the sizes for a ticket image""" filename = attachment.filename # add the specified sizes as attachments sizes = self.sizes() for name, size in sizes.items(): # crop the image image = Image.open(attachment.path) i = crop_resize(image, size) buffer = StringIO() i.save(buffer, image.format) buffer.seek(0,2) # seek to end of file filesize = buffer.tell() buffer.seek(0) a = Attachment(self.env, 'ticket', ticket.id) a.author = ticket['reporter'] a.description = ticket['summary'] f = ('.%sx%s.' % (size[0] or '', size[1] or '')).join(filename.rsplit('.', 1)) # XXX assumes the file has an extension a.insert(f, buffer, filesize)
def test_attachment(self): attachment = Attachment(self.env, 'ticket', 42) attachment.description = 'Summary line' attachment.author = 'Santa' attachment.ipnr = 'northpole.example.com' attachment.insert('foo.txt', StringIO('Lorem ipsum dolor sit amet'), 0) so = self._get_so() self.assertEquals('%s:attachment:ticket:42:foo.txt' % self.basename, so.doc_id) self.assertEquals('attachment', so.realm) self.assertEquals('foo.txt', so.id) self.assertEquals('ticket', so.parent_realm) self.assertEquals(42, so.parent_id) self.assertTrue('foo.txt' in so.title) self.assertEquals('Santa', so.author) self.assertEquals(attachment.date, so.created) self.assertEquals(attachment.date, so.changed) self.assertTrue('Santa' in so.involved) #self.assertTrue('Lorem ipsum' in so.oneline) # TODO self.assertTrue('Lorem ipsum' in so.body.read()) self.assertTrue('Summary line' in so.comments)
def addAttachment(self, author, a): if a['filename'] != '': description = a['description'].strip() id = a['bug_id'] filename = a['filename'].strip() filedata = StringIO(a['thedata']) filesize = len(filedata.getvalue()) time = a['creation_ts'] #print "author: " + author print "id: %d" % id print "filename: " + filename #print "filesize: %d" % filesize #print "description: " + description #print " ->inserting attachment '%s' for ticket %s -- %s" % \ # (filename, id, description) attachment = Attachment(self.env, 'ticket', id) attachment.author = author attachment.description = description attachment.insert(filename, filedata, filesize, datetime2epoch(time))
def _process_attachment(self, req, config, build): resource_id = req.args['member'] == 'config' \ and build.config or build.resource.id upload = req.args['file'] if not upload.file: send_error(req, message="Attachment not received.") self.log.debug('Received attachment %s for attaching to build:%s', upload.filename, resource_id) # Determine size of file upload.file.seek(0, 2) # to the end size = upload.file.tell() upload.file.seek(0) # beginning again # Delete attachment if it already exists try: old_attach = Attachment(self.env, 'build', parent_id=resource_id, filename=upload.filename) old_attach.delete() except ResourceNotFound: pass # Save new attachment attachment = Attachment(self.env, 'build', parent_id=resource_id) attachment.description = req.args.get('description', '') attachment.author = req.authname attachment.insert(upload.filename, upload.file, size) self._send_response(req, 201, 'Attachment created', headers={ 'Content-Type': 'text/plain', 'Content-Length': str(len('Attachment created')) })
def add_attachments(env, ticket, attachments): """add attachments to the ticket""" ctr = 1 for msg in attachments: attachment = Attachment(env, 'ticket', ticket.id) attachment.author = ticket['reporter'] attachment.description = ticket['summary'] payload = msg.get_payload() if msg.get('Content-Transfer-Encoding') == 'base64': payload = base64.b64decode(payload) size = len(payload) filename = msg.get_filename() or message.get('Subject') if not filename: filename = 'attachment-%d' % ctr extensions = KNOWN_MIME_TYPES.get(message.get_content_type()) if extensions: filename += '.%s' % extensions[0] ctr += 1 buffer = StringIO() print >> buffer, payload buffer.seek(0) attachment.insert(filename, buffer, size) os.chmod(attachment._get_path(), 0666)
def create_sizes(self, ticket, attachment): """create the sizes for a ticket image""" filename = attachment.filename # add the specified sizes as attachments sizes = self.sizes() for name, size in sizes.items(): # crop the image image = Image.open(attachment.path) i = crop_resize(image, size) buffer = StringIO() i.save(buffer, image.format) buffer.seek(0, 2) # seek to end of file filesize = buffer.tell() buffer.seek(0) a = Attachment(self.env, 'ticket', ticket.id) a.author = ticket['reporter'] a.description = ticket['summary'] f = ('.%sx%s.' % (size[0] or '', size[1] or '')).join( filename.rsplit('.', 1)) # XXX assumes the file has an extension a.insert(f, buffer, filesize)
def test_add_attachment_html_notification(self): ticket = Ticket(self.env) ticket['description'] = 'Some ticket description' ticket['summary'] = 'Some ticket summary' ticket['type'] = 'defect' ticket['status'] = 'new' ticket.insert() attachment = Attachment(self.env, ticket) attachment.description = "`Some` '''!WikiFormatted''' ''text''" attachment.filename = 'somefile.txt' event = TicketChangeEvent('ticket', 'changed', ticket, author='user1', attachment=attachment) actual = self.tf.format([], 'ticket', 'text/html', event) filename = resource_filename(__name__, 'attachment_notification.html') file = open(filename, 'r') expected = file.read() file.close() self.assertEqual(expected, actual)
def add_attachments(env, ticket, attachments): """add attachments to the ticket""" ctr = 1 for msg in attachments: attachment = Attachment(env, 'ticket', ticket.id) attachment.author = ticket['reporter'] attachment.description = ticket['summary'] payload = msg.get_payload() if msg.get('Content-Transfer-Encoding') == 'base64': payload = base64.b64decode(payload) size = len(payload) filename = msg.get_filename() or message.get('Subject') if not filename: filename = 'attachment-%d' % ctr extensions = KNOWN_MIME_TYPES.get(message.get_content_type()) if extensions: filename += '.%s' % extensions[0] ctr += 1 buffer = StringIO() print >> buffer, payload buffer.seek(0) attachment.insert(filename, buffer, size) os.chmod(attachment._get_path(), 0666)
def _upload_attachment(env, req, uploadfile): sid = req.session.sid parent_id = None parent_realm = 'build' filename = None parent_realm = Resource(parent_realm) Attachment.delete_all(env, 'iTest', sid) attachment = Attachment(env, 'iTest', sid) attachment.description = sid if not hasattr(uploadfile, 'filename') or not uploadfile.filename: raise TracError(_('No file uploaded1')) if hasattr(uploadfile.file, 'fileno'): size = os.fstat(uploadfile.file.fileno())[6] else: uploadfile.file.seek(0, 2) # seek to end of file size = uploadfile.file.tell() uploadfile.file.seek(0) if size == 0: raise TracError(_("Can't upload empty file")) # We try to normalize the filename to unicode NFC if we can. # Files uploaded from OS X might be in NFD. filename = unicodedata.normalize('NFC', unicode(uploadfile.filename, 'utf-8')) filename = filename.replace('\\', '/').replace(':', '/') filename = os.path.basename(filename) if not filename: raise TracError(_('No file uploaded2')) attachment.insert(filename, uploadfile.file, size) return attachment
def putAttachment(self, req, ticket, filename, description, data, replace=True): """ Add an attachment, optionally (and defaulting to) overwriting an existing one. Returns filename.""" if not model.Ticket(self.env, ticket).exists: raise ResourceNotFound('Ticket "%s" does not exist' % ticket) if replace: try: attachment = Attachment(self.env, 'ticket', ticket, filename) req.perm(attachment.resource).require('ATTACHMENT_DELETE') attachment.delete() except TracError: pass attachment = Attachment(self.env, 'ticket', ticket) req.perm(attachment.resource).require('ATTACHMENT_CREATE') attachment.author = req.authname attachment.description = description attachment.insert(filename, StringIO(data.data), len(data.data)) return attachment.filename
def add_attachment(tc, realm, id, file): attachment = Attachment(tc.env, realm, id) attachment.description = "image in %s" % id attachment.insert(file, StringIO(''), 0, 2)
def _parse_multipart(self, author, msg): body = '' # delete all attachement at message-id Attachment.delete_all(self.env, 'mailarchive', self.id, self.db) for part in msg.walk(): content_type = part.get_content_type() self.log.debug('Content-Type:' + content_type) file_counter = 1 if content_type == 'multipart/mixed': pass elif content_type == 'text/html' and self._is_file(part) == False: if body != '': body += "\n------------------------------\n\n" body = part.get_payload(decode=True) charset = part.get_content_charset() self.log.debug('charset:' + str(charset)) # Todo:need try if charset != None: body = self._to_unicode(body, charset) elif content_type == 'text/plain' and self._is_file(part) == False: #body = part.get_payload(decode=True) if body != '': body += "\n------------------------------\n\n" current_body = part.get_payload(decode=True) charset = part.get_content_charset() self.log.debug('charset:' + str(charset)) # Todo:need try if charset != None: #body = self._to_unicode(body, charset) body += self._to_unicode(current_body, charset) else: body += current_body elif part.get_payload(decode=True) == None: pass # file attachment else: self.log.debug(part.get_content_type()) # get filename # Applications should really sanitize the given filename so that an # email message can't be used to overwrite important files filename = self._get_filename(part) if not filename: import mimetypes ext = mimetypes.guess_extension(part.get_content_type()) if not ext: # Use a generic bag-of-bits extension ext = '.bin' filename = 'part-%03d%s' % (file_counter, ext) file_counter += 1 self.log.debug("filename:" + filename.encode(OUTPUT_ENCODING)) # make attachment tmp = os.tmpfile() tempsize = len(part.get_payload(decode=True)) tmp.write(part.get_payload(decode=True)) tmp.flush() tmp.seek(0,0) attachment = Attachment(self.env, 'mailarchive', self.id) attachment.description = '' # req.args.get('description', '') attachment.author = author #req.args.get('author', '') attachment.ipnr = '127.0.0.1' try: attachment.insert(filename, tmp, tempsize, None, self.db) except Exception, e: try: ext = filename.split('.')[-1] if ext == filename: ext = '.bin' else: ext = '.' + ext filename = 'part-%03d%s' % (file_counter, ext) file_counter += 1 attachment.description += ', Original FileName: %s' % filename attachment.insert(filename, tmp, tempsize, None, self.db) self.log.warn('As name is too long, the attached file is renamed : ' + filename) except Exception, e: self.log.error('Exception at attach file of Message-ID:' + self.messageid) traceback.print_exc(e) tmp.close()
def add_attachment(tc, realm, id, file): attachment = Attachment(tc.env, realm, id) attachment.description = "image in %s" % id attachment.insert(file, io.BytesIO(), 0, 2)
report.insert(db=db) # Collect attachments from the request body for attach_elem in elem.children(Recipe.ATTACH): attach_elem = list(attach_elem.children('file'))[0] # One file only filename = attach_elem.attr.get('filename') resource_id = attach_elem.attr.get('resource') == 'config' \ and build.config or build.resource.id try: # Delete attachment if it already exists old_attach = Attachment(self.env, 'build', parent_id=resource_id, filename=filename) old_attach.delete() except ResourceNotFound: pass attachment = Attachment(self.env, 'build', parent_id=resource_id) attachment.description = attach_elem.attr.get('description') attachment.author = req.authname fileobj = StringIO(attach_elem.gettext().decode('base64')) attachment.insert(filename, fileobj, fileobj.len, db=db) # If this was the last step in the recipe we mark the build as # completed if last_step: self.log.info('Slave %s completed build %d ("%s" as of [%s])', build.slave, build.id, build.config, build.rev) build.stopped = step.stopped # Determine overall outcome of the build by checking the outcome # of the individual steps against the "onerror" specification of # each step in the recipe for num, recipe_step in enumerate(recipe):
for attach_elem in elem.children(Recipe.ATTACH): attach_elem = list( attach_elem.children('file'))[0] # One file only filename = attach_elem.attr.get('filename') resource_id = attach_elem.attr.get('resource') == 'config' \ and build.config or build.resource.id try: # Delete attachment if it already exists old_attach = Attachment(self.env, 'build', parent_id=resource_id, filename=filename) old_attach.delete() except ResourceNotFound: pass attachment = Attachment(self.env, 'build', parent_id=resource_id) attachment.description = attach_elem.attr.get('description') attachment.author = req.authname fileobj = StringIO(attach_elem.gettext().decode('base64')) attachment.insert(filename, fileobj, fileobj.len, db=db) # If this was the last step in the recipe we mark the build as # completed if last_step: self.log.info('Slave %s completed build %d ("%s" as of [%s])', build.slave, build.id, build.config, build.rev) build.stopped = step.stopped # Determine overall outcome of the build by checking the outcome # of the individual steps against the "onerror" specification of # each step in the recipe for num, recipe_step in enumerate(recipe):