DAL issues ....

58 views
Skip to first unread message

Ben Duncan

unread,
Dec 6, 2018, 8:42:27 AM12/6/18
to Web2py
How can something that seems so simple, be so hard?

I can't seem to get a SINGLE field from the database from DAL:

First is the Error Message (Or on of the many types - but the latest one), then the code

What AM I doing wrong here ?

Als always, thanks ...

Error Message:

Error ticket for "Mec"

Ticket ID

10.13.69.144.2018-12-06.07-32-29.8fda8fd6-fe14-47f4-b508-14ce2346465e

<type 'exceptions.TypeError'> 'NoneType' object has no attribute '__getitem__'

Version

web2py™ Version 2.17.2-stable+timestamp.2018.10.06.18.54.02
Python Python 2.7.13: /usr/bin/python (prefix: /opt/rh/python27/root/usr)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Traceback (most recent call last):
File "/data/web2py/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment)
File "/data/web2py/web2py/applications/Mec/controllers/default.py", line 96, in <module>
File "/data/web2py/web2py/gluon/globals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "/data/web2py/web2py/applications/Mec/controllers/default.py", line 87, in company_login
session.company_name = DbRows['company_name']
TypeError: 'NoneType' object has no attribute '__getitem__'

Error snapshot help

<type 'exceptions.TypeError'>('NoneType' object has no attribute '__getitem__')

-------------------- Code Here  --------------------------------------------------
 Ok - Code for controller/default.py

def company_login():
company.company_name,orderby=db.company.company_name)    form = SQLFORM.factory(
        Field('username', label = 'User Name', requires=IS_NOT_EMPTY()),
        Field('password', 'password', label = "Password",requires=IS_NOT_EMPTY()),
        Field('Company', label = 'Court ID',
                 requires=IS_IN_DB(db,db.company.company_number,'%(company_name)s')),
        submit_button=' Login ',
        )

  if form.process().accepted:
       response.flash = 'form accepted'
   elif form.errors:
       response.flash = T('form has errors')

   session.company = request.vars['Company']   
   Company_ID = request.vars['Company']
   DbRows = db(db.company.company_number==Company_ID).select(db.company.company_name).first()
    session.company_name = DbRows['company_name']

    return dict(form=form)





Thanks again ...


Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

Leonel Câmara

unread,
Dec 6, 2018, 10:09:42 AM12/6/18
to web2py-users
What's happening here is that DbRows can become None in this line

   DbRows = db(db.company.company_number==Company_ID).select(db.company.company_name).first()

So when you do DbRows['company_name'] you can get that error.

Basically you need to check if DbRows is None and raise HTTP(404)

Ben Duncan

unread,
Dec 6, 2018, 1:22:30 PM12/6/18
to Web2py
Well I was thinking that, BUT:  I'm ALMOST convinced there is a bug in the latest web2py:

CODE ------------------------------------------------------------------------------------------------------------------------------
Snippet of controllers/default.py

session.company = request.vars['Company']
    Company_ID = "This is a %s of type %s " % (session.company, type(session.company))
    Company_ID = session.company

    DbRows = db(db.company.company_number==Company_ID).select(db.company.company_name).first()
    # Co_Name = DbRows['company_name']

    session.company_data = DbRows

From view/default/company_login.html:
{{extend 'layout.html'}}
<!-- {{session.company = request.vars['Company']}} -->

<INPUT type="button" value="HOME" onclick="location.href='{{=URL('index')}}'"/>

<a href="{{=URL('index')}}">HOME</a>

<h4>Input form</h4>
{{=form}}
<h4 style="margin:0;">Submitted variables</h4>
{{=BEAUTIFY(request.vars)}}
{{=(request.vars)}}

<h4 style="margin:0;">Company vars: {{=(request.vars['Company'])}}</h4>
<h4 style="margin:0;">Company session: {{=(session.company)}}</h4>
<h4 style="margin:0;">session data: {{=(session.company_data)}}</h4>

Will Display The following:
(top of form omitted ...)
.....

Submitted variables

Company:
5
password:
makeit1
username:
<Storage {'username': 'be...@linux4ms.net', 'Company': '5', 'password': 'makeit1', '_formkey': '65ee8396-aac0-4b5c-a255-00be7e7b01ca', '_formname': 'no_table/create'}>

Company vars: 5

Company session: 5

session data: <Row {'company_name': 'Number 5 Company'}>

___________________________________________________________________________________________________________________________________

However, any type of referencing or attempt to do anything with either session.company_date or DbRows
always gives a "Nonetype" error of various subtypes (such as __getitem__)

Uncommenting the controller code  of  "# Co_Name = DbRows['company_name']"
gives the error:

Ticket ID

10.13.69.144.2018-12-06.12-17-59.81fd7ae1-6523-47ad-94c7-7b1b480179bd

<type 'exceptions.TypeError'> 'NoneType' object has no attribute '__getitem__'

Version

web2py™ Version 2.17.2-stable+timestamp.2018.10.06.18.54.02
Python Python 2.7.13: /usr/bin/python (prefix: /opt/rh/python27/root/usr)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Traceback (most recent call last):
File "/data/web2py/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment
File "/data/web2py/web2py/gluon/globals.py", line 421, in <lambda>
self._caller = lambda f: f
()
File "/data/web2py/web2py/applications/Mec/controllers/default.py", line 84, in company_login
Co_Name = DbRows['company_name']
TypeError: 'NoneType' object has no attribute '__getitem__'

As one can see , the Row item exists, it just seems there is no way to get to it ...

Any other ideas ?


Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

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

Ben Duncan

unread,
Dec 6, 2018, 1:54:05 PM12/6/18
to Web2py
Here is something strange as well - working thru the web2py console, everything works:

[web2py@su-postgres-ben-3 web2py]$ python ./web2py.py -S Mec -M
web2py Web Framework
Created by Massimo Di Pierro, Copyright 2007-2018
Version 2.17.2-stable+timestamp.2018.10.06.18.54.02
Database drivers available: psycopg2, pymysql, imaplib, sqlite3, pg8000, pyodbc
WARNING:web2py:import IPython error; use default python shell
Python 2.7.13 (default, Feb  8 2017, 06:30:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> session.company_id = '5'
>>> Company_ID = session.company_id
>>> print Company_ID
5
>>> DbRows = db(db.company.company_number==Company_ID).select(db.company.company_name).first()
>>> type(DbRows['company_name'])
<type 'str'>
>>> DbRows['company_name']
'Number 5 Company'
>>> session.company_data = DbRows
>>> type(DbRows['company_name'])
<type 'str'>
>>> print session.company_data['company_name']
Number 5 Company
>>>type(DbRows)
<class 'pydal.objects.Row'>
>>>





Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

Jim S

unread,
Dec 6, 2018, 2:16:11 PM12/6/18
to web2py-users
Can you try passing DbRows to your view and see if that works?

return dict(form=form, DbRows=DbRows)

Or, throw a print statement in there before session.company_name = DbRows['company_name'] to see if DbRows['company_name'] has a value before sending to the view?

-Jim

Jim S

unread,
Dec 6, 2018, 2:17:35 PM12/6/18
to web2py-users
It looks like the second line of your view if reassigning something else to session.company.  I don't believe the HTML comment tags are not going to prevent web2py from executing that statement.

-Jim

Val K

unread,
Dec 6, 2018, 2:30:06 PM12/6/18
to web2py-users
Maybe I don't understand something, but I think that when the controller is requested for the first time there is no any request.vars and request.vars['Company'] too. Just in case: form.process() doesn't interrupt controller execution

Ben Duncan

unread,
Dec 6, 2018, 3:22:15 PM12/6/18
to Web2py
The request.vars get set from the SQL form in the controller....
Removed all the COMMENTS, and in the controller put a print and changed the code:

    DbRows = db(db.company.company_number==Company_ID).select(db.company.company_name).first()
    print DbRows['company_name']
    session.company_data = DbRows
    return dict(form=form,DbRows=DbRows)

And still getting a failure now on the print line ...

Ticket ID

10.13.69.144.2018-12-06.13-53-08.f35db77f-72f8-4749-82e9-90eda4c32141

<type 'exceptions.TypeError'> 'NoneType' object has no attribute '__getitem__'

Version

web2py™ Version 2.17.2-stable+timestamp.2018.10.06.18.54.02
Python Python 2.7.13: /usr/bin/python (prefix: /opt/rh/python27/root/usr)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Traceback (most recent call last):
File "/data/web2py/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment
File "/data/web2py/web2py/gluon/globals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "/data/web2py/web2py/applications/Mec/controllers/default.py", line 87, in company_login
    print DbRows['company_name']
TypeError: 'NoneType' object has no attribute '__getitem__'


It's very strange how :
 session.company_data = DbRows
Gets passed.

However, looking at the postgres logs:
 2018-12-06 14:18:31.482 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: SELECT "company"."company_name", "company"."company_number" FROM "company" WHERE ("company"."company_number" IS NOT NULL) ORDER BY "company"."company_name", "company"."company_number";
< 2018-12-06 14:18:31.488 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: SELECT "company"."company_name" FROM "company" WHERE ("company"."company_number" IS NULL);
< 2018-12-06 14:18:31.505 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: COMMIT
< 2018-12-06 14:18:35.282 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: BEGIN
< 2018-12-06 14:18:35.282 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: SELECT 1;
< 2018-12-06 14:18:35.475 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: SELECT "company"."company_name", "company"."company_number" FROM "company" WHERE ("company"."company_number" IS NOT NULL) ORDER BY "company"."company_name", "company"."company_number";
< 2018-12-06 14:18:35.485 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: SELECT "company"."company_name" FROM "company" WHERE ("company"."company_number" = 13);
< 2018-12-06 14:18:35.505 CST SessId: 5c0981bd.6e70  TrxId: 0>LOG:  statement: COMMIT

It seems to be making 2 passes thru the DB

Allows the  the session variable to be set
Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

On Thu, Dec 6, 2018 at 1:30 PM Val K <valq...@gmail.com> wrote:
Maybe I don't understand something, but I think that when the controller is requested for the first time there is no any request.vars and request.vars['Company'] too. Just in case: form.process() doesn't interrupt controller execution

Ben Duncan

unread,
Dec 6, 2018, 3:46:22 PM12/6/18
to Web2py
Ok, YOU GUYS are awesome .......
Your replies gave me the insight I needed !!!!!!!!!

Here is the explanation:

A: in the database, the passes were for the "lookup" in the SQLFORM.factory
   SO there was the confusion there ...

B: It seems there is a 2 passes thru the controller (which this group gave me the hint at it)
    The failure was at the first pass when nothing was set ...
    putting this in:

    if session.company :
       DbRows = db(db.company.company_number==Company_ID).select(db.company.company_name).first()
       session.company_data = DbRows
       session.company_name = DbRows.company_name

Allowed me to PASS unharmed and get what I needed.

THANKS Again, group ....


.
Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

Jim S

unread,
Dec 6, 2018, 3:59:52 PM12/6/18
to web2py-users
I'd love to see the final controller code if you have the time?

Also, I'm curious as to why you're putting all this in the session.  Why not just pass it in the dict being returned to the view?

-Jim

Ben Duncan

unread,
Dec 6, 2018, 4:42:01 PM12/6/18
to Web2py
Controller Code:

def company_login():


    form = SQLFORM.factory(
        Field('username', label = 'User Name', requires=IS_NOT_EMPTY()),
        Field('password', 'password', label = "Password",requires=IS_NOT_EMPTY()),
        Field('Company', label = 'Court ID',
                 requires=IS_IN_DB(db,db.company.company_number,'%(company_name)s')),
        submit_button=' Login ',
        )

    if form.process().accepted:
       response.flash = 'form accepted'
    elif form.errors:
       response.flash = T('form has errors')

    session.company = request.vars['Company']
 
    Company_ID = session.company

    if session.company :
       DbRows = db(db.company.company_number==Company_ID).select().first()

       session.company_data = DbRows
       session.company_name = DbRows.company_name
       session.company_ap_aging = DbRows.

    return dict(form=form)

The reason I need this setup is that this is the Company Record that controls all aspects of
the system (see ap_apaging_days). It also controls what sub-apps are on and off ...



Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

Jim Steil

unread,
Dec 6, 2018, 5:00:34 PM12/6/18
to web...@googlegroups.com
Thanks Ben

You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/o48pZhY7dYM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages