Thanks for the info. Have tried option 2 (which I also like the most) but 
problem I am having is as soon as I try to include a view i.e generic.html in 
the action the info is not being added to the output.

 

So 

from py4web.core import Fixture

class InfoInjector(Fixture):

      def transform(self, output):

           if isinstance(output, dict) and 'info' not in output:

                output['info'] = get_info(auth)                                 
                    
           return output

 

@action('index')

@action.uses(auth, InfoInjector())

def index():

        message=’Hello there’    

        return dict(message=message ) # info is automatically injected I see 
the injected info + message

 

Output:-

{

  "info": {

    "info": {

      "email": "eudoraj...@gmail.com",

      "first_name": "John",

      "id": 1,

      "last_name": "Bannister",

      "username": "jab"

    }

  },

  "message": "Hello there"

}

 

 

@action('index')

@action.uses(auth, InfoInjector())

@action.uses(‘generic.html’)

 

def index():

        messate=”Hello there”    

        return dict(message=message) # info is automatically injected 
InfoInjector() is not called !!!!! and no info injected

 

 

 

From: web2py@googlegroups.com [mailto:web2py@googlegroups.com] On Behalf Of 
Massimo Di Pierro
Sent: 01 October 2019 05:43
To: web2py-users
Subject: Re: [web2py] Re: py4web Auth

 

I understand what you are  asking. Something like the ability of web2py to 
define stuff in a models file and it would be available to the views. Problem 
with that design is the overhead. With py4web we are trying to be more 
efficient. 

 

There are many ways to do this:

 

1) create a function that returns the info you need and pass this function to 
the layout. For example:

 

def get_info(auth):

      return {... whatever you need ...}

 

@action('index')

@action.uses(auth)

def index():

        return dict(info=get_info(auth))

 

2) create a new fixture that injects data in the template

 

from py4web.core import Fixture

class InfoInjector(Fixture)

      def transform(self, output):

           if isinstance(output, dict) and 'info' not in output:

                output['info'] = get_info(auth)                                 
                    
           return output

 

@action('index')

@action.uses(auth, InfoInjector())

def index():

        return dict() # info is automatically injected

 

3) add logic directly to the template as in:

 

    [[from app.thisapp.common import get_info, auth]][[info = get_info(auth)]] 
... [[=info]]

 

I like solution 2 best

 

On Monday, 30 September 2019 09:50:07 UTC-7, John Bannister wrote:

Hi Massimo,

 

Not quite what I am after. Scenario which I have is that I have a number of 
different models each has a number of different atctions. I have a menu defined 
in layout.html which is included in each view. I would like to display the 
logged in user name in the menu (so something like Welcome John) if the user is 
logged in or LOGIN if they are not.

 

I understand that I can use auth.get_user in the action to access any auth user 
info I need but the question is do I need to pass back the user information to 
each and every view for layout.html to have access to the information or is 
there some way to set a global/application wide variable so that can be access 
by all views(templates). Ideally I would like to be able to access 
auth.get_user() from layout.html

 

BR

John

 

 

From: web2py@googlegroups.com [mailto:web2py@googlegroups.com] On Behalf Of 
Massimo Di Pierro
Sent: 30 September 2019 17:12
To: web2py-users
Subject: Re: [web2py] Re: py4web Auth

 

In any action decorated with @action.uses(auth.user) you can do

 

first_name = auth.get_user()['first_name']

 

Is this what you are asking?


On Monday, 30 September 2019 01:07:23 UTC-7, John Bannister wrote:

Hi Massimo,

 

Thanks very much. Will test and give you some feedback hopefully today.

 

Is there some sort of way to access user info from layout.html for example 
across multiple modules without having to pass the user from each eaction in 
each module? I want to be able to display the user name as part of the menu 
something like Welcome John (if the user is logged in) irrespective of module 
or action??

 

Best Regards

John

 

 

From: web2py@googlegroups.com [mailto:web2py@googlegroups.com] On Behalf Of 
Massimo Di Pierro
Sent: 30 September 2019 06:39
To: web2py-users
Subject: [web2py] Re: py4web Auth

 

I added a page to the documentation:

 

http://py4web.com/_documentation/static/index.html#chapter-11

 

I am sure it if full of spelling errors and I am making a fool of myself (my 
keyboard is acting up and my mother language is not english)

Also the examples could use an independent tester as I typed them without 
running them.

 

Anyway I thought this was better than nothing. :-)

 

Massimo

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/e525e23a-199b-4b9b-83f6-ced97e1efaff%40googlegroups.com
 
<https://groups.google.com/d/msgid/web2py/e525e23a-199b-4b9b-83f6-ced97e1efaff%40googlegroups.com?utm_medium=email&utm_source=footer>
 .

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/44a408dc-40cc-4bf7-b599-05b3f2b66289%40googlegroups.com
 
<https://groups.google.com/d/msgid/web2py/44a408dc-40cc-4bf7-b599-05b3f2b66289%40googlegroups.com?utm_medium=email&utm_source=footer>
 .

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/5ba7b3b0-4b32-42c3-a4f7-9ad46607b4c9%40googlegroups.com
 
<https://groups.google.com/d/msgid/web2py/5ba7b3b0-4b32-42c3-a4f7-9ad46607b4c9%40googlegroups.com?utm_medium=email&utm_source=footer>
 .

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/04ed01d57845%24c7861f30%2456925d90%24%40gmail.com.

Reply via email to