def get_dossier_attributes(self): return [ { 'id': 'Title', 'title': _('label_title', default=u'title'), 'transform': to_unicode }, { 'id': 'start', 'title': _(u'label_start', default=u'Opening Date'), 'transform': format_datetime, 'style': get_date_style() }, { 'id': 'end', 'title': _(u'label_end', default=u'Closing Date'), 'transform': format_datetime, 'style': get_date_style() }, { 'id': 'responsible', 'title': _(u'label_responsible', default='Responsible'), 'transform': readable_author }, { 'id': 'review_state', 'title': _('label_review_state', default='Review state'), 'transform': StringTranslater(self.request, 'plone').translate }, { 'id': 'reference', 'title': _(u'label_reference_number', default=u'Reference Number') }, ]
def render(self): tasks = get_selected_items(self.context, self.request) tasks = [tt for tt in tasks] if not tasks: msg = _( u'error_no_items', default=u'You have not selected any Items') IStatusMessage(self.request).addStatusMessage(msg, type='error') if self.request.get('orig_template'): return self.request.RESPONSE.redirect( self.request.form['orig_template']) else: return self.request.RESPONSE.redirect( self.context.absolute_url()) task_attributes = [ {'id':'title', 'title':_('label_task_title')}, {'id':'review_state', 'title':_('review_state'), 'transform':StringTranslater( self.context.REQUEST, 'plone').translate}, {'id':'deadline', 'title':_('label_deadline'), 'transform':format_datetime, 'style':get_date_style()}, {'id':'completed', 'title':_('label_completed'), 'transform':format_datetime, 'style':get_date_style()}, {'id': 'containing_dossier', 'title':_('label_dossier_title')}, {'id':'issuer', 'title':_('label_issuer'), 'transform':readable_author}, {'id':'responsible', 'title':_('label_responsible'), 'transform':readable_author}, {'id':'task_type', 'title':_('label_task_type'), 'transform':task_type_helper}, {'id':'client_id', 'title':_('label_client_id')}, {'id':'sequence_number', 'title':_('label_sequence_number')}, ] reporter = XLSReporter( self.context.REQUEST, task_attributes, tasks, sheet_title=translate( _('label_tasks', default=u'Tasks'), context=self.request), footer='%s %s' % ( datetime.now().strftime('%d.%m.%Y %H:%M'), get_client_id()) ) data = reporter() if not data: msg = _(u'Could not generate the report') IStatusMessage(self.request).addStatusMessage( msg, type='error') return self.request.RESPONSE.redirect(self.context.absolute_url()) response = self.request.RESPONSE response.setHeader('Content-Type', 'application/vnd.ms-excel') set_attachment_content_disposition(self.request, "task_report.xls") return data
def test_xlsreporter(self): contact_info = self.stub() self.mock_utility(contact_info, IContactInformation, name=u"") self.expect(contact_info.is_user(ANY)).result(True) self.expect( contact_info.describe(ANY)).result('Describe text for a user') request = self.mocker.mock() brains = [] for i in range(2): brain = self.stub() self.expect(brain.Title).result('Objekt %i' % (i)) self.expect(brain.missing).result(MissingValue) self.expect(brain.start).result( datetime(2012, 2, 25) + timedelta(i)) self.expect(brain.responsible).result('Test user %i' % (i)) self.expect(brain.review_state).result('dossier-state-activ') brains.append(brain) self.replay() translation_request = Environment(('de', 'de')) test_attributes = [ {'id': 'Title', 'title': _('label_title', default='Title')}, #test missingvalue {'id': 'missing', 'missing': 'Missing', }, {'id': 'start', 'title': _('label_start', default='Start'), 'transform': format_datetime, 'style': get_date_style()}, {'id': 'responsible', 'title': _('label_responsible', default='Responsible'), 'transform': readable_author}, {'id': 'review_state', 'title': _('label_review_state', default='Review state'), 'transform': StringTranslater( translation_request, 'plone').translate}, ] # generate the report.xls reporter = XLSReporter(request, test_attributes, brains) data = reporter() # check the generate xls with the xlrd module wb = xlrd.open_workbook(file_contents=data) sheet = wb.sheets()[0] labels = sheet.row(0) self.assertEquals( [cell.value for cell in labels], [u'Title', u'', u'Start', u'Responsible', u'Review state']) row1 = sheet.row(1) self.assertEquals( [cell.value for cell in row1], [u'Objekt 0', u'', u'25.02.2012', u'Describe text for a user', u'dossier-state-activ']) row2 = sheet.row(2) self.assertEquals( [cell.value for cell in row2], [u'Objekt 1', u'', u'26.02.2012', u'Describe text for a user', u'dossier-state-activ'])
def render(self): tasks = get_selected_items(self.context, self.request) tasks = [tt for tt in tasks] if not tasks: msg = _(u'error_no_items', default=u'You have not selected any Items') IStatusMessage(self.request).addStatusMessage(msg, type='error') if self.request.get('orig_template'): return self.request.RESPONSE.redirect( self.request.form['orig_template']) else: return self.request.RESPONSE.redirect( self.context.absolute_url()) task_attributes = [ { 'id': 'title', 'title': _('label_task_title') }, { 'id': 'review_state', 'title': _('review_state'), 'transform': StringTranslater(self.context.REQUEST, 'plone').translate }, { 'id': 'deadline', 'title': _('label_deadline'), 'transform': format_datetime, 'style': get_date_style() }, { 'id': 'completed', 'title': _('label_completed'), 'transform': format_datetime, 'style': get_date_style() }, { 'id': 'containing_dossier', 'title': _('label_dossier_title') }, { 'id': 'issuer', 'title': _('label_issuer'), 'transform': readable_author }, { 'id': 'responsible', 'title': _('label_responsible'), 'transform': readable_author }, { 'id': 'task_type', 'title': _('label_task_type'), 'transform': task_type_helper }, { 'id': 'client_id', 'title': _('label_client_id') }, { 'id': 'sequence_number', 'title': _('label_sequence_number') }, ] reporter = XLSReporter( self.context.REQUEST, task_attributes, tasks, sheet_title=translate(_('label_tasks', default=u'Tasks'), context=self.request), footer='%s %s' % (datetime.now().strftime('%d.%m.%Y %H:%M'), get_client_id())) data = reporter() if not data: msg = _(u'Could not generate the report') IStatusMessage(self.request).addStatusMessage(msg, type='error') return self.request.RESPONSE.redirect(self.context.absolute_url()) response = self.request.RESPONSE response.setHeader('Content-Type', 'application/vnd.ms-excel') set_attachment_content_disposition(self.request, "task_report.xls") return data
def test_xlsreporter(self): contact_info = self.stub() self.mock_utility(contact_info, IContactInformation, name=u"") self.expect(contact_info.is_user(ANY)).result(True) self.expect( contact_info.describe(ANY)).result('Describe text for a user') request = self.mocker.mock() brains = [] for i in range(2): brain = self.stub() self.expect(brain.Title).result('Objekt %i' % (i)) self.expect(brain.missing).result(MissingValue) self.expect( brain.start).result(datetime(2012, 2, 25) + timedelta(i)) self.expect(brain.responsible).result('Test user %i' % (i)) self.expect(brain.review_state).result('dossier-state-active') brains.append(brain) self.replay() translation_request = Environment(('de', 'de')) test_attributes = [ { 'id': 'Title', 'title': _('label_title', default='Title') }, #test missingvalue { 'id': 'missing', 'missing': 'Missing', }, { 'id': 'start', 'title': _('label_start', default='Start'), 'transform': format_datetime, 'style': get_date_style() }, { 'id': 'responsible', 'title': _('label_responsible', default='Responsible'), 'transform': readable_author }, { 'id': 'review_state', 'title': _('label_review_state', default='Review state'), 'transform': StringTranslater(translation_request, 'plone').translate }, ] # generate the report.xls reporter = XLSReporter(request, test_attributes, brains) data = reporter() # check the generate xls with the xlrd module wb = xlrd.open_workbook(file_contents=data) sheet = wb.sheets()[0] labels = sheet.row(0) self.assertEquals( [cell.value for cell in labels], [u'Title', u'', u'Start', u'Responsible', u'Review state']) row1 = sheet.row(1) self.assertEquals([cell.value for cell in row1], [ u'Objekt 0', u'', u'25.02.2012', u'Describe text for a user', u'dossier-state-active' ]) row2 = sheet.row(2) self.assertEquals([cell.value for cell in row2], [ u'Objekt 1', u'', u'26.02.2012', u'Describe text for a user', u'dossier-state-active' ])