extend CSV export: include waiting list info; display time stamp and use it for sorting
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3558 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
		
							parent
							
								
									11ca655192
								
							
						
					
					
						commit
						cb48096a3a
					
				
					 2 changed files with 43 additions and 12 deletions
				
			
		|  | @ -1,5 +1,5 @@ | ||||||
| # | # | ||||||
| #  Copyright (c) 2008 Helmut Merz helmutm@cy55.de | #  Copyright (c) 20098 Helmut Merz helmutm@cy55.de | ||||||
| # | # | ||||||
| #  This program is free software; you can redistribute it and/or modify | #  This program is free software; you can redistribute it and/or modify | ||||||
| #  it under the terms of the GNU General Public License as published by | #  it under the terms of the GNU General Public License as published by | ||||||
|  | @ -29,10 +29,11 @@ from zope import component | ||||||
| from zope.cachedescriptors.property import Lazy | from zope.cachedescriptors.property import Lazy | ||||||
| from cybertools.composer.interfaces import IInstance | from cybertools.composer.interfaces import IInstance | ||||||
| from cybertools.composer.schema.interfaces import ISchema | from cybertools.composer.schema.interfaces import ISchema | ||||||
|  | from cybertools.organize.browser.service import BaseView | ||||||
| from cybertools.stateful.interfaces import IStateful | from cybertools.stateful.interfaces import IStateful | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class RegistrationsExportCsv(object): | class RegistrationsExportCsv(BaseView): | ||||||
| 
 | 
 | ||||||
|     encoding = 'ISO8859-15' |     encoding = 'ISO8859-15' | ||||||
| 
 | 
 | ||||||
|  | @ -76,46 +77,72 @@ class RegistrationsExportCsv(object): | ||||||
|         withTemporary = self.request.get('with_temporary') |         withTemporary = self.request.get('with_temporary') | ||||||
|         context = self.context |         context = self.context | ||||||
|         services = context.getServices() |         services = context.getServices() | ||||||
|  |         withWaitingList = False | ||||||
|  |         for service in services: | ||||||
|  |             if service.waitingList: | ||||||
|  |                 withWaitingList = True | ||||||
|  |                 break | ||||||
|         schemas = [s for s in context.getClientSchemas() if ISchema.providedBy(s)] |         schemas = [s for s in context.getClientSchemas() if ISchema.providedBy(s)] | ||||||
|         yield (['Client ID'] |         headline = (['Client ID', 'Time Stamp'] | ||||||
|              + list(itertools.chain(*[[self.encode(f.title) |              + list(itertools.chain(*[[self.encode(f.title) | ||||||
|                                             for f in s.fields] |                                             for f in s.fields] | ||||||
|                                                     for s in schemas])) |                                                     for s in schemas])) | ||||||
|              + [self.encode(s.title) for s in services]) |              + [self.encode(s.title) for s in services]) | ||||||
|  |         if withWaitingList: | ||||||
|  |             headline += ['WL ' + self.encode(s.title) for s in services] | ||||||
|  |         #yield line | ||||||
|  |         lines = [] | ||||||
|         clients = context.getClients() |         clients = context.getClients() | ||||||
|         for name, client in clients.items(): |         for name, client in clients.items(): | ||||||
|             hasRegs = False |             hasRegs = False | ||||||
|             regs = [] |             regs = [] | ||||||
|  |             waiting = [] | ||||||
|  |             timeStamp = '' | ||||||
|             for service in services: |             for service in services: | ||||||
|                 reg = service.registrations.get(name) |                 reg = service.registrations.get(name) | ||||||
|                 if reg is None: |                 if reg is None: | ||||||
|                     regs.append(0) |                     regs.append(0) | ||||||
|  |                     waiting.append(0) | ||||||
|                 else: |                 else: | ||||||
|                     state = IStateful(reg).getStateObject() |                     state = IStateful(reg).getStateObject() | ||||||
|                     if state.name == 'temporary' and not withTemporary: |                     if state.name == 'temporary' and not withTemporary: | ||||||
|                         regs.append(0) |                         regs.append(0) | ||||||
|  |                         waiting.append(0) | ||||||
|                     else: |                     else: | ||||||
|                         number = reg.number |  | ||||||
|                         regs.append(reg.number) |                         regs.append(reg.number) | ||||||
|                         if number: |                         waiting.append(reg.numberWaiting) | ||||||
|  |                         if reg.number or reg.numberWaiting: | ||||||
|                             hasRegs = True |                             hasRegs = True | ||||||
|  |                         if reg.timeStamp < timeStamp: | ||||||
|  |                             timeStamp = reg.timeStamp | ||||||
|             if not hasRegs: |             if not hasRegs: | ||||||
|                 continue |                 continue | ||||||
|             result = [name] |             line = [name, timeStamp] | ||||||
|             for schema in schemas: |             for schema in schemas: | ||||||
|                 instance = IInstance(client) |                 instance = IInstance(client) | ||||||
|                 instance.template = schema |                 instance.template = schema | ||||||
|                 data = instance.applyTemplate() |                 data = instance.applyTemplate() | ||||||
|                 for f in schema.fields: |                 for f in schema.fields: | ||||||
|                     result.append(self.encode(data.get(f.name, ''))) |                     line.append(self.encode(data.get(f.name, ''))) | ||||||
|             result += regs |             line += regs | ||||||
|             yield result |             if withWaitingList: | ||||||
|  |                 line += waiting | ||||||
|  |             #yield line | ||||||
|  |             lines.append(line) | ||||||
|  |         lines.sort(key=lambda x: x[1]) | ||||||
|  |         for l in lines: | ||||||
|  |             l[1] = self.getFormattedDate(l[1], type='dateTime', variant='short') | ||||||
|  |         return [headline] + lines | ||||||
| 
 | 
 | ||||||
|     def render(self): |     def render(self): | ||||||
|         methodName = self.request.get('get_data_method', 'getAllDataInColumns') |         methodName = self.request.get('get_data_method', 'getAllDataInColumns') | ||||||
|         method = getattr(self, methodName, self.getData) |         method = getattr(self, methodName, self.getData) | ||||||
|         output = StringIO() |         output = StringIO() | ||||||
|  |         try: | ||||||
|             csv.writer(output, dialect='excel', delimiter=';').writerows(method()) |             csv.writer(output, dialect='excel', delimiter=';').writerows(method()) | ||||||
|  |         except: | ||||||
|  |             import traceback; traceback.print_exc() | ||||||
|  |             raise | ||||||
|         result = output.getvalue() |         result = output.getvalue() | ||||||
|         self.setHeaders(len(result)) |         self.setHeaders(len(result)) | ||||||
|         return result |         return result | ||||||
|  |  | ||||||
|  | @ -57,7 +57,11 @@ class BaseView(SchemaBaseView): | ||||||
| 
 | 
 | ||||||
|     # output formatting |     # output formatting | ||||||
| 
 | 
 | ||||||
|     def getFormattedDate(self, date=None, type='date', variant='medium'): |     def getFormattedDate(self, date=None, type='date', variant='medium', | ||||||
|  |                          adjustTz=False): | ||||||
|  |         if adjustTz: | ||||||
|  |             date = time.gmtime(date)[:6] | ||||||
|  |         else: | ||||||
|             date = time.localtime(date)[:6] |             date = time.localtime(date)[:6] | ||||||
|         date = datetime(*date) |         date = datetime(*date) | ||||||
|         return formatDate(date, type=type, variant=variant, lang=self.getLanguage()) |         return formatDate(date, type=type, variant=variant, lang=self.getLanguage()) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 helmutm
						helmutm