The same issue caused a bug in the TSV export, which was fixed by this pull
request: https://github.com/web2py/web2py/pull/1766. Check out that code to
see the fix.
Anthony
On Sunday, September 24, 2017 at 12:38:02 PM UTC-4, Alex Beskopilny wrote:
>
> Thanks for you answer !
> (it's code works with web2py-14.6)
>
> db = DAL("postgres://user:pass@localhost:5432/dmdb", lazy_tables=False,
> migrate_enabled= True)
>
> db.define_table('sprf10',
> Field("spr",),
> )
>
> db.define_table('sprf13',
> Field("s13",),
> )
>
> db.define_table('addr',
> Field("f1", length= 96),
> Field("f2", length= 96),
> .......
> Field("ptr_r1", 'integer', label= 'id registr'),
> )
>
> db.define_table('registr',
> Field ("dfld2", label= "Регистрационный номер:2", length= 32),
> Field ("dfld3", 'date', label= "Дата внесения в реестр:3", requires =
> IS_DATE(format=XFMT), default = request.now.today(), ),
> Field ("dfld11", length= 512),
> Field ("dfld12", length= 96),
> Field ("dfld13", length= 1024),
> Field ("dfld14", length= 96),
> Field ("dfld15", length= 96),
> Field ("dfld16", length= 1024, readable=False,writable=False ),
> Field ("dfld17", length= 1024, readable=False,writable=False ),
> Field("p_client", 'reference reg_client', label= 'id reg_client',
> writable= False, readable= False),
> Field("p_addr", 'reference addr', label= 'id addr', writable= False,
> readable= False ),
> Field("p_sprf10", 'reference sprf10', label= 'id sprf10', writable=
> False, readable= False),
> Field("p_sprf13", 'reference sprf13', label= 'id sprf13', writable=
> False, readable= False),
> auth.signature,
> )
>
> --------------------------------------------------------------------------------------------
> sqlhtml.py
>
> exportManager = dict(
> csv=(ExporterXLS, 'XLS', T('xls')),
> xml=(ExporterXML, 'XML', T('XML export of columns shown')),
> html=(ExporterHTML, 'HTML', T('HTML export of visible
> columns')),
> json=(ExporterJSON, 'JSON', T('JSON export of visible
> columns')),
> )
> .....................
> class ExporterXLS(ExportClass):
> label = 'XLS'
> file_ext = "xls"
> content_type = ".xls"
>
> def __init__(self, rows):
> ExportClass.__init__(self, rows)
>
> def export(self):
> import xlwt, cStringIO
> if len(self.rows) == 0:
> return 'empty rows-table'
>
> (table_name,f)=self.rows.colnames[0].split('.')
> book = xlwt.Workbook()
> sheet = book.add_sheet(table_name)
>
> def list2xls(row_num,line_list):
> for col_num, value in enumerate(line_list):
> #if isinstance(value, long):
> # value= str(value)
> if isinstance(value, str):
> value= value.decode('utf8')
> if isinstance(value, datetime.datetime) or
> isinstance(value, datetime.date):
> value= value.strftime('%d.%m.%Y')
> sheet.write(row_num, col_num, value)
>
> fields=[]; labels=[]
> for col in self.rows.colnames:
> (t,f) = col.split('.')
> fields.append(f)
> labels.append( self.rows.db[t][f].label )
> list2xls(1,labels)
>
> for r_num, row in enumerate (self.rows, 2):
> llist=[ row[f] for f in fields]
> list2xls(r_num, llist)
>
> s = cStringIO.StringIO()
> book.save(s)
> return s.getvalue()
> --------------------------------------
> controllers/default.py
> def index():
>
> tbl= 'registr'
> query = db[tbl].id >0
>
> grid = SQLFORM.grid(query,showbuttontext=False, maxtextlength=200,
> deletable = False, create = False,
> links=[ lambda row: A('k',_href=URL('cp2_new',args=
> row.id), _class="btn btn-success" ), ] ,
> buttons_placement = 'left', paginate= PAGIN, )
> return dict(grid=grid)
>
> ------------------------------------------------------------------------------------------------------------
>
> Traceback (most recent call last):
> File "/home/w2p/web2py/gluon/restricted.py", line 219, in restricted
> exec(ccode, environment)
> File "/home/w2p/web2py/applications/dmlic/controllers/default.py", line
> 573, in <module>
> File "/home/w2p/web2py/gluon/globals.py", line 414, in <lambda>
> self._caller = lambda f: f()
> File "/home/w2p/web2py/gluon/tools.py", line 3981, in f
> return action(*a, **b)
> File "/home/w2p/web2py/applications/dmlic/controllers/default.py", line 78,
> in index
> buttons_placement = 'left', paginate= PAGIN, )
> File "/home/w2p/web2py/gluon/sqlhtml.py", line 2598, in grid
> raise HTTP(200, oExp.export(), **response.headers)
> File "/home/w2p/web2py/gluon/sqlhtml.py", line 3742, in export
> labels.append( self.rows.db[t][f].label )
> File "/home/w2p/web2py/gluon/packages/dal/pydal/base.py", line 663, in
> __getitem__
> return self.__getattr__(str(key))
> File "/home/w2p/web2py/gluon/packages/dal/pydal/base.py", line 670, in
> __getattr__
> return BasicStorage.__getattribute__(self, key)
> AttributeError: 'DAL' object has no attribute '"registr"'
>
>
>
>
> On Saturday, September 23, 2017 at 10:29:59 PM UTC+3, Anthony wrote:
>>
>> Please show the full traceback as well as the rest of the relevant code
>> (i.e., table definition and grid code).
>>
>> Anthony
>>
>> On Saturday, September 23, 2017 at 3:17:24 PM UTC-4, Alex Beskopilny
>> wrote:
>>>
>>>
>>> Hi All,
>>> Why It's code work from controller and does error from class Exporter ?
>>> How to access table_name ?
>>> ( AttributeError: 'DAL' object has no attribute '"table_name"'
>>> <https://sun.minfindnr.ru/admin/default/errors/dmlic#> )
>>>
>>> class ExporterXLS(ExportClass):
>>> label = 'XLS'
>>> file_ext = "xls"
>>> content_type = ".xls"
>>>
>>> def __init__(self, rows):
>>> ExportClass.__init__(self, rows)
>>>
>>> def export(self):
>>> import xlwt, cStringIO
>>>
>>> table_name= self.rows.colnames[0].split('.')[0]
>>> tbl2=[]; row_flds=[]; row_labs=[]
>>>
>>> for col in self.rows.colnames:
>>> (t,f) = col.split('.')
>>> row_flds.append(f)
>>> row_labs.append ( self.rows.db[t][f].label )
>>>
>>> tbl2.append(row_labs) # table headres
>>> for row in self.rows:
>>> llist=[ row[x] for x in row_flds]
>>> tbl2.append(llist)
>>> #------------------------------------------------------------
>>> book = xlwt.Workbook()
>>> sheet1 = book.add_sheet(table_name.decode('utf8'))
>>>
>>> for i, l in enumerate(tbl2):
>>> for j, col in enumerate(l):
>>> if isinstance(col, str):
>>> col=col.decode('utf8')
>>> elif isinstance(col, datetime.datetime) or
>>> isinstance(col, datetime.date):
>>> col = col.strftime('%d.%m.%Y')
>>> else:
>>> col=''
>>> s = cStringIO.StringIO()
>>> book.save(s)
>>>
>>> #-------------------------------------------------------------------------
>>> response.headers['Content-Type'] = '.xls'
>>> response.headers['Content-disposition'] = \
>>> 'attachment; filename=\"%s_%s.xls\"' % (
>>> request.now.strftime("%d.%m.%Y_%H%M%S") , table_name)
>>> return s.getvalue()
>>>
>>>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.