How can I get auth.user_id inside my decorator?

34 views
Skip to first unread message

Константин Комков

unread,
Jun 13, 2020, 2:34:04 AM6/13/20
to web...@googlegroups.com
I use decorator for reduce count of sql queries for logged in users like that:
def is_info(fun):
    def decorated():
        if session.is_info is None:
            entrant = db(db.a_persons.WEB_ID == auth.user_id).select(
                db.a_persons.FILE_CODE0, db.a_persons.F, db.a_persons.I, db.a_persons.O, db.a_persons.id,
                db.a_persons.BIRTHDAY, db.a_persons.GENDER, db.a_persons.BIRTHPLACE, db.a_persons.GRADUATION_LEVEL,
                db.a_persons.COUNTRY, db.a_persons.REGION, db.a_persons.ADR_INDEX, db.a_persons.ADR_DISTRICT,
                db.a_persons.ADR_LOC_TYPE, db.a_persons.ADR_TOWN, db.a_persons.ADR_STREET_TYPE, db.a_persons.ADR_STREET,
                db.a_persons.ADR_HOUSE, db.a_persons.ADR_BUILDING, db.a_persons.ADR_FLAT, db.a_persons.PHONE,
                db.a_persons.NEED_HOSTEL, db.a_persons.PREP_COURSE, db.a_persons.ARMY, db.a_persons.EXTRA,
                db.a_persons.WORK_EXP, db.a_persons.PASS_ID, db.a_persons.GRADUATION_YEAR, db.a_persons.EDU_REGION,
                db.a_persons.LANGUAGE).first()
            if entrant is None:
                redirect(URL('user', 'index', args=['logout']))        
            session.is_info = True
            session.myvar = auth.user_id
            session.is_info = True
            session.file_code = entrant.FILE_CODE0
            session.middle_name = entrant.O
            session.entrant_id = entrant.id
            session.birthday = entrant.BIRTHDAY
            session.sex = entrant.GENDER
            session.birthplace = entrant.BIRTHPLACE
            session.country = entrant.COUNTRY
            session.region = entrant.REGION
            session.adr_index = entrant.ADR_INDEX
            session.adr_district = entrant.ADR_DISTRICT
            session.adr_loc_type = entrant.ADR_LOC_TYPE
            session.adr_town = entrant.ADR_TOWN
            session.adr_street_type = entrant.ADR_STREET_TYPE
            session.adr_street = entrant.ADR_STREET
            session.adr_house = entrant.ADR_HOUSE
            session.adr_building = entrant.ADR_BUILDING
            session.adr_flat = entrant.ADR_FLAT
            session.phone = entrant.PHONE
            session.need_hostel = entrant.NEED_HOSTEL
            session.prep_course = entrant.PREP_COURSE
            session.army = entrant.ARMY
            session.extra = entrant.EXTRA
            session.work_exp = entrant.WORK_EXP
            session.pass_id = entrant.PASS_ID
            session.graduation_level = entrant.GRADUATION_LEVEL
            session.graduation_year = entrant.GRADUATION_YEAR
            session.edu_region = entrant.EDU_REGION
            session.language = entrant.LANGUAGE
            return fun()
        else:
            return fun()
    return decorated
As I understund after that decorator inside my function I check session.myvar and have None. I can't use auth.user_id inside decorator?
tables.py
db.define_table('a_persons', Field('FILE_CODE0', length=15), Field('WEB_ID', 'reference auth_user')
                # ...
)
Why I don't have error in query, becouse db.a_persons.WEB_ID == None? I have the first user information.
Web2py - 2.18.5-stable+timestamp.2019.04.08.04.22.03

Jim S

unread,
Jun 13, 2020, 8:40:21 AM6/13/20
to web2py-users
Have you tried auth.user.id?

-Jim

Константин Комков

unread,
Jun 13, 2020, 11:01:04 AM6/13/20
to web...@googlegroups.com
Yes, I thought about it, but if auth.user_id is None for me in auth.user.id I got error: <class 'AttributeError'> 'NoneType' object has no attribute 'id'.

P.S. I wrote function and use it inside my function now, but variant with decorator might be interesting.

Jim Steil

unread,
Jun 13, 2020, 2:45:32 PM6/13/20
to web...@googlegroups.com
Then either you're not loved in our not running in a controller function I'd guess

On Sat, Jun 13, 2020, 10:01 AM Константин Комков <firean...@gmail.com> wrote:
Yes, I thought about it, but if auth.user_id is None for me in auth.user.id I got error: <class 'AttributeError'> 'NoneType' object has no attribute 'id'.

суббота, 13 июня 2020 г., 15:40:21 UTC+3 пользователь Jim S написал:

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/0PblfPYOs_o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/1d90d353-ba6a-42f3-bf60-b12282c3a10fo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages