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 _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 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 _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 _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 _do_uploadPicture(self, req, userProfile, teamRosterData, req_arg_picture = 'tr_userProfile_picture' ): upload = req.args.get(req_arg_picture, None) if upload == None or not hasattr(upload, 'filename') or not upload.filename: return userProfile.picture_href 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.teamRoster_wikiPage) if not page.exists: page.text="= Team Roster Pictures =" page.save( 'trac', 'Page created by tracteamroster component', req.remote_addr) attachment = Attachment(self.env, 'wiki', self.teamRoster_wikiPage) attachment.author = get_reporter_id(req, 'author') attachment.ipnr = req.remote_addr attachment.insert('_'.join([userProfile.id, filename]), upload.file, size) return req.href('/'.join(['raw-attachment', 'wiki',self.teamRoster_wikiPage,attachment.filename]))
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 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_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 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 _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()