示例#1
0
    def exportXlsData(self):
        """get XLS file for all folder objects"""
        filename = datetime.now().strftime('ict_%Y%m%d%H%M%S.xls')
        f_handle, f_name = tempfile.mkstemp(filename)
        wbook = Workbook()
        wb_hosts = wbook.add_sheet('ddd')
        style0 = XFStyle()
        font0 = Font()
        font0.height = 6 * 20
        style0.num_format_str = '@'
        style0.font = font0
        style1 = XFStyle()
        font1 = Font()
        font1.height = 6 * 20
        style1.num_format_str = '@'
        style1.font = font1
        heading_pattern = xl.Pattern()
        heading_pattern.pattern = xl.Pattern.SOLID_PATTERN
        heading_pattern.pattern_back_colour = 0x5
        heading_pattern.pattern_fore_colour = 0x5
        fields = self.fields
        attrList = [fname for fname, fval in fields.items()]
        itemList = self.context.items()
        pos_y = 0
        pos_x = 0
        for attr in attrList:
            wb_data = Formula(u'"%s"' % attr)
            style0.pattern = heading_pattern
            wb_hosts.write(pos_y, pos_x, wb_data, style0)
            pos_x += 1
        # IntID
        wb_data = Formula(u'"IntID"')
        wb_hosts.write(pos_y, pos_x, wb_data, style0)
        pos_x += 1
        # objectID
        wb_data = Formula(u'"objectID"')
        wb_hosts.write(pos_y, pos_x, wb_data, style0)
        wb_hosts.col(pos_x).width *= 3
        pos_x += 1
        pos_y = 1
        #
        allAttributes = {}
        for interface in implementedBy(self.factory):
            for i_attrName in interface:
                i_attr = interface[i_attrName]
                if IField.providedBy(i_attr):
                    allAttributes[i_attrName] = i_attr
        #
        for item_n, item_v in itemList:
            pos_x = 0
            for attr in attrList:
                #                from zope.interface import implementedBy
                #                ff=self.factory
                #                tt=[i for i in implementedBy(ff)]
                #                it=tt[-1]
                #attrField = self.attrInterface[attr]
                attrField = allAttributes[attr]
                #                tmpFieldProperty = getattr(self.factory, attr)
                #                if hasattr(tmpFieldProperty, '_FieldProperty__field'):
                #                    attrField = getattr(self.factory, attr)._FieldProperty__field
                attrDm = datamanager.AttributeField(item_v, attrField)
                v_style = XFStyle()
                v_font = Font()
                v_font.height = 6 * 20
                v_style.font = v_font
                value = None
                if IChoice.providedBy(attrField):
                    v_style.num_format_str = '@'
                    dateValue = attrDm.get()
                    v_widget = getMultiAdapter(\
                                    (attrField,self.request),
                                    interfaces.IFieldWidget)
                    v_widget.context = item_v
                    #                    dm = zope.component.getMultiAdapter(
                    #                        (self.content, field.field), interfaces.IDataManager)
                    #                    zope.component.getMultiAdapter(
                    #                        (self.context,
                    #                         self.request,
                    #                         self.form,
                    #                         getattr(widget, 'field', None),
                    #                         widget),
                    #                        interfaces.IValidator).validate(fvalue)
                    #                    dm = zope.component.getMultiAdapter(
                    #                        (self.__context__, field), interfaces.IDataManager)
                    v_dataconverter = queryMultiAdapter(\
                                    (attrDm.field, v_widget),
                                    interfaces.IDataConverter)
                    #print u"ddd55: %s: %s" % (attr, dateValue)
                    if dateValue is not None:
                        value = v_dataconverter.toWidgetValue(dateValue)[0]
                        #print "value3->   %s: %s " % (attr, value)


#                elif ICollection.providedBy(attrField):
#                    v_style.num_format_str = '@'
#                    value = getattr(item_v, attr)
#                    print "ddd66: %s: %s" % (attr, value)
#                elif IBool.providedBy(attrField):
#                    v_style.num_format_str = '@'
#                    value = getattr(item_v, attr)
#                    print "value2->   %s: %s " % (attr, value)
                else:
                    v_style.num_format_str = '@'
                    dateValue = attrDm.get()
                    v_widget = getMultiAdapter(\
                                    (attrField,self.request),
                                    interfaces.IFieldWidget)
                    v_widget.context = item_v
                    v_dataconverter = queryMultiAdapter(\
                                    (attrDm.field, v_widget),
                                    interfaces.IDataConverter)
                    #d2 = queryMultiAdapter((attrDm.field, v_widget),interfaces.IDataConverter)
                    if dateValue is not None:
                        value = v_dataconverter.toWidgetValue(dateValue)
                    if type(value) is list:
                        value = u";".join(value)
                    #print u"value1->   %s: %s " % (attr, value)
                if value is not None:
                    #print u"wb_hosts.write(%s, %s, %s, %s)" % (pos_y, pos_x, value, v_style)
                    wb_hosts.write(pos_y, pos_x, value, v_style)
                pos_x += 1
            # IntID
            uidutil = queryUtility(IIntIds)
            wb_data = Formula(u'"%s"' % uidutil.getId(item_v))
            wb_hosts.write(pos_y, pos_x, wb_data, style0)
            pos_x += 1
            # objectID
            wb_data = Formula(u'"%s"' % item_v.objectID)
            wb_hosts.write(pos_y, pos_x, wb_data, style0)
            pos_x += 1
            pos_y += 1
        wbook.save(f_name)
        self.request.response.setHeader('Content-Type',
                                        'application/vnd.ms-excel')
        self.request.response.setHeader(\
            'Content-Disposition',
            'attachment; filename=\"%s\"' % filename)
        setNoCacheHeaders(self.request.response)
        datafile = open(f_name, "r")
        dataMem = datafile.read()
        datafile.close()
        os.remove(f_name)
        return dataMem
示例#2
0
    def exportXlsData(self, request, sheetName=u'ict', wbook=None):
        """get XLS file for all folder objects"""
        if wbook is None:
            localWbook = True
            filename = datetime.now().strftime('ict_%Y%m%d%H%M%S.xls')
            f_handle, f_name = tempfile.mkstemp(filename)
            wbook = Workbook()
        else:
            localWbook = False
        wb_hosts = wbook.add_sheet(getattr(self, 'ikName', sheetName))
        style0 = XFStyle()
        font0 = Font()
        font0.height = 6 * 20
        #style0.num_format_str = '@'
        style0.font = font0
        style1 = XFStyle()
        font1 = Font()
        font1.height = 6 * 20
        #style1.num_format_str = '@'
        style1.font = font1
        heading_pattern = xl.Pattern()
        heading_pattern.pattern = xl.Pattern.SOLID_PATTERN
        heading_pattern.pattern_back_colour = 0x5
        heading_pattern.pattern_fore_colour = 0x5
        fields = fieldsForFactory(self.contentFactory, ['objectID'])
        attrList = [fname for fname, fval in fields.items()]
        itemList = self.items()
        pos_y = 0
        pos_x = 0
        for attr in attrList:
            style0.pattern = heading_pattern
            wb_hosts.write(pos_y, pos_x, attr, style0)
            pos_x += 1
        # objectID
        wb_hosts.write(pos_y, pos_x, "objectID", style0)
        wb_hosts.col(pos_x).width *= 3
        pos_x += 1
        pos_y = 1
        #
        allAttributes = {}
        for interface in implementedBy(self.contentFactory):
            for i_attrName in interface:
                i_attr = interface[i_attrName]
                if IField.providedBy(i_attr):
                    allAttributes[i_attrName] = i_attr
        #
        for item_n, item_v in itemList:
            pos_x = 0
            for attr in attrList:
                attrField = allAttributes[attr]
                attrDm = datamanager.AttributeField(item_v, attrField)
                v_style = XFStyle()
                v_font = Font()
                v_font.height = 6 * 20
                v_style.font = v_font
                value = None
                if IChoice.providedBy(attrField):
                    v_style.num_format_str = '@'
                    #                    try:
                    dateValue = attrDm.get()
                    v_widget = getMultiAdapter(\
                                    (attrField,request),
                                    interfaces.IFieldWidget)
                    v_widget.context = item_v
                    v_dataconverter = queryMultiAdapter(\
                                    (attrDm.field, v_widget),
                                    interfaces.IDataConverter)
                    #                    except AttributeError, errText:
                    #                        print "Error2:  ### ", errText
                    #                        print "item_v: ", item_v
                    #                        print "item_v.ikName: ", item_v.ikName
                    #                        print "attr: ", attr
                    #                        dateValue = None
                    #                        #import pdb
                    #                        #pdb.set_trace()
                    if dateValue is not None:
                        #                        try:
                        valueVector = v_dataconverter.toWidgetValue(dateValue)
                        if len(valueVector) > 0:
                            value = valueVector[0]
#                        except IndexError, errText:
#                            print "Error3:  ### ", errText
#                            print "item_v: ", item_v
#                            print "item_v.ikName: ", item_v.ikName
#                            print "attr: ", attr
                else:
                    v_style.num_format_str = '@'
                    try:
                        #                        import pdb
                        #                        pdb.set_trace()
                        dateValue = attrDm.get()
                        v_widget = getMultiAdapter(\
                                        (attrField,request),
                                        interfaces.IFieldWidget)
                        v_widget.context = item_v
                        v_dataconverter = queryMultiAdapter(\
                                        (attrDm.field, v_widget),
                                        interfaces.IDataConverter)
                    except AttributeError, errText:
                        print "Error1:  ### ", errText
                        print "item_v: ", item_v
                        print "item_v.ikName: ", item_v.ikName
                        print "attr: ", attr
                        dateValue = None
                    if dateValue is not None:
                        try:
                            value = v_dataconverter.toWidgetValue(dateValue)
                        except AttributeError, errText:
                            print "Error4:  ### ", errText
                            print "item_v: ", item_v
                            print "item_v.ikName: ", item_v.ikName
                            print "attr: ", attr
                            value = None
                    if type(value) is list:
                        value = u";".join(value)
                if value is not None:
                    wb_hosts.write(pos_y, pos_x, value, v_style)
                pos_x += 1