python Help with classes

47 views
Skip to first unread message

António Ramos

unread,
Jul 20, 2017, 5:45:58 PM7/20/17
to web...@googlegroups.com
hello i tested this simple code outside web2py

class a():
    def a1(self):
        temp=getattr(eval("b")(), "b1")('testing result')
        return temp
    
class b():
    def b1(self,param):
        s=somevar2.somesubvar1
        return param

print a().a1() 


if i remove s=somevar2.somesubvar1 the code prints "'testing result'" otherwise i get this error
global name 'somevar2' is not defined


I´m using this kind of code with virtual fields like this
db.py
from wf_validates import Validate
db.define_table(
"status",
...
Field.Virtual('isavailable', lambda row: Validate().status('status','isavailable',row,db)),

)

wf_validates.py

class Validate(object):


def status(self,table,field,row,db):
wf=db.workflows(id=row[table]["workflow"])["workflow"]
cb=getattr(eval(wf)(), table+"_"+row[table]["title"].replace(" ","_"))('xx',db)
return cb

class Ficheiros(object):
def status_Draft(self,row,db):
session=current.session
return True

here session=current.session is the offending line probably because session is not available.
So far so good.
The problem is that it does not throw any error and i was chasing it for too many time until i discovered it because the field "isavailable" was not created but the row object was created.

Why web2py does not generate an admin error with

global name 'session' is not defined  ???

Regards










António Ramos

unread,
Jul 21, 2017, 6:41:25 AM7/21/17
to web...@googlegroups.com
any help ?

Anthony

unread,
Jul 21, 2017, 8:39:05 AM7/21/17
to web2py-users
Do you have the following in your module:

from gluon import current

Anthony

António Ramos

unread,
Jul 21, 2017, 8:54:29 AM7/21/17
to web...@googlegroups.com
yes i have 

from gluon import current 

in my wf_validates.py


Anthony

--
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 web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

António Ramos

unread,
Jul 21, 2017, 9:22:14 AM7/21/17
to web...@googlegroups.com
this is my real code in wf_validates.py


from gluon import current

class Validate(object):
def status(self,table,field,row,db):
wf=db.workflows(id=row[table]["workflow"])["workflow"]
if wf in ["Ficheiros","Load","Pessoa"]:
cb=getattr(eval("Ficheiros")(), "status"+"_"+"Draft")('xx','db')
return cb
else:
return ""

class Ficheiros(object):
def status_Draft(self,row,db):
db.log.insert(msg="status_draft")
session=current.session
this_time=datetime.datetime.utcnow() +datetime.timedelta(hours=-1)#session.utc)
check_time=this_time.replace( hour=18, minute=0, second=0, microsecond=0 )
return this_time>check_time
return True


Anthony

unread,
Jul 21, 2017, 9:44:05 AM7/21/17
to web2py-users
What is the traceback?

António Ramos

unread,
Jul 21, 2017, 10:46:26 AM7/21/17
to web...@googlegroups.com
There is no traceback. the record is returned like this  if i remove  session=current.session

<Row {'completion': '0', 'help': 'This document is still in Draft.\r\nYou should send it to Approval', 'workflow': 4L, 'workflow_comment': <Set (workflow_comment.step = 8)>, 'titleenter': 'Review', 'title': 'Draft', 'titleaction': 'Draft', 'entities_extra': <Set (entities_extra.status = 8)>, 'entities': <Set (entities.status = 8)>, 'rgb': '#00c0ef', 'rules': <Set (rules.step = 8)>, 'details': 'Person', 'id': 8L, 'isavailable': True, 'icon': 'fa-user', 'button': 'btn-info btn-xs', 'color': 'bg-blue'}>

but if i have 
session=current.session inside the  status_Draft  function  i get this result 


<Row {'completion': '0', 'help': 'This document is still in Draft.\r\nYou should send it to Approval', 'workflow': 4L, 'workflow_comment': <Set (workflow_comment.step = 8)>, 'titleenter': 'Review', 'title': 'Draft', 'titleaction': 'Draft', 'entities_extra': <Set (entities_extra.status = 8)>, 'entities': <Set (entities.status = 8)>, 'rgb': '#00c0ef', 'rules': <Set (rules.step = 8)>, 'details': 'Person', 'id': 8L, 'isavailable': True, 'icon': 'fa-user', 'button': 'btn-info btn-xs', 'color': 'bg-blue'}>

there is no "isavailable" 

2017-07-21 14:44 GMT+01:00 Anthony <abas...@gmail.com>:
What is the traceback?

Jim S

unread,
Jul 21, 2017, 11:13:24 AM7/21/17
to web2py-users
there is no "isavailable" 

Yes there is.  Did you paste the wrong result?   'isavailable' is True in the second result set too.

-Jim

What is the traceback?
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages