Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from roundup.cgi.actions import Action
- from roundup.cgi import templating
- from roundup import hyperdb
- import csv
- class ExportCSVNamesAction(Action):
- name = 'export'
- permissionType = 'View'
- def handle(self):
- ''' Export the specified search query as CSV. '''
- # figure the request
- request = templating.HTMLRequest(self.client)
- filterspec = request.filterspec
- sort = request.sort
- group = request.group
- columns = request.columns
- klass = self.db.getclass(request.classname)
- # full-text search
- if request.search_text:
- matches = self.db.indexer.search(
- re.findall(r'\b\w{2,25}\b', request.search_text), klass)
- else:
- matches = None
- h = self.client.additional_headers
- h['Content-Type'] = 'text/csv; charset=%s' % self.client.charset
- # some browsers will honor the filename here...
- h['Content-Disposition'] = 'inline; filename=query.csv'
- self.client.header()
- if self.client.env['REQUEST_METHOD'] == 'HEAD':
- # all done, return a dummy string
- return 'dummy'
- wfile = self.client.request.wfile
- if self.client.charset != self.client.STORAGE_CHARSET:
- wfile = codecs.EncodedFile(wfile,
- self.client.STORAGE_CHARSET, self.client.charset, 'replace')
- writer = csv.writer(wfile)
- self.client._socket_op(writer.writerow, columns)
- # Figure out Link columns
- represent = {}
- def repr_link(cls,col):
- def f(x):
- if x==None:
- return ""
- else:
- return str(cls.get(x,col))
- return f
- props = klass.getprops()
- for col in columns:
- represent[col] = str
- if isinstance(props[col], hyperdb.Link):
- cn = props[col].classname
- cl = self.db.getclass(cn)
- if cl.getprops().has_key('name'):
- represent[col] = repr_link(cl, 'name')
- elif cn == 'user':
- represent[col] = repr_link(cl, 'username')
- # and search
- for itemid in klass.filter(matches, filterspec, sort, group):
- self.client._socket_op(writer.writerow, [represent[col](klass.get(itemid, col)) for col in columns])
- return '\n'
- def init(instance):
- instance.registerAction('export_csv_names', ExportCSVNamesAction)
- # vim: set filetype=python sts=4 sw=4 et si
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement