[web2py] Re: Auth Decorator / Model Question.
Can you make a more concrete example. Not Sure I understand what users_id refers to. On Monday, 3 September 2012 22:43:10 UTC-5, Kevin C wrote: The title isn't very descriptive, but I hope this paragraph clears things up. Basically we are creating a basic SaaS app. Users will log in at theirname.oursite.com to an administrative panel. Each administrative panel is tied to a user ID from the auth table. So basically here is what should happen: User visits theirname.oursite.com/admin Web2py retrieves users_id field from stores table (This field assigns ownership of each store to a user id) Web2py auth decorator should ensure that the logged in user ID is that of the store owner I understand how to write the decorator, but what is the best way to retrieve the users_id from the stores table and store it? Sessions? Temporary variable that is set on each page load? Please forgive my ignorance. I am completely new to web2py and want to develop using best practices. I appreciate any guidance you can offer. Thank you. --
[web2py] Re: Auth Decorator / Model Question.
The users_id comes from the stores table, and is a foreign key to the web2py users table. Really the source of it is irrelevant. What I need to know is what is the best practice for storing this type of information? In PHP, I would do something like the following in a global include file: ?php if(empty($_SESSION['store.mysite.com'])) { $_SESSION['store.mysite.com'] = get_store_info('store.mysite.com'); } ? This would make all of the store info (Including users_id) available to the entire script through the PHP session. I need to know how to do the equivalent in web2py. How do I execute a certain block on every page load (IE to check if the store info has been retrieved and stored yet) and then where should I store that info to make it accessible for the rest of the script? In my limited experience with web2py, I'm thinking I should store the block in a model file and store it to a web2py session, but I wanted to get a second opinion before continuing down that path. I hope I have explained it a little more clearly. Basically, how do I execute code on every page load and what is the best practice for storing short term data like that? On Monday, September 3, 2012 10:43:10 PM UTC-5, Kevin C wrote: The title isn't very descriptive, but I hope this paragraph clears things up. Basically we are creating a basic SaaS app. Users will log in at theirname.oursite.com to an administrative panel. Each administrative panel is tied to a user ID from the auth table. So basically here is what should happen: User visits theirname.oursite.com/admin Web2py retrieves users_id field from stores table (This field assigns ownership of each store to a user id) Web2py auth decorator should ensure that the logged in user ID is that of the store owner I understand how to write the decorator, but what is the best way to retrieve the users_id from the stores table and store it? Sessions? Temporary variable that is set on each page load? Please forgive my ignorance. I am completely new to web2py and want to develop using best practices. I appreciate any guidance you can offer. Thank you. --
[web2py] Re: Auth Decorator / Model Question.
In my limited experience with web2py, I'm thinking I should store the code in a model file and have that code save the store info to a web2py session, but I wanted to get a second opinion before continuing down that path. Yes, that's exactly right. Anthony --
Re: [web2py] Re: Auth Decorator / Model Question.
Great, thanks. I spent the first few days having web2py create our database tables from our models and now we're moving on to actual coding. I am LOVING this framework! Kevin Cackler Tech Daddies 501-205-1512 http://www.techdaddies.com On 9/4/2012 3:18 PM, Anthony wrote: In my limited experience with web2py, I'm thinking I should store the code in a model file and have that code save the store info to a web2py session, but I wanted to get a second opinion before continuing down that path. Yes, that's exactly right. Anthony -- --
[web2py] Re: Auth Decorator / Model Question.
you can certainly do STORE_DETAILS = db(db.stores.user_id == auth.user_id).select() in models. You'd have the variable STORE_DETAILS available in all controllers and every time a user loads a page the data will be refreshed. In order to reduce the db pressure, you can STORE_DETAILS = db(db.stores.user_id == auth.user_id).select(cache=(cache.ram, 60)) Doing so, the 2nd select will be fired only if more than 60 seconds passed from the 1st (i.e. a new page requested by the same user within 60 seconds will be fetched from the cache and not from the db) What you are doing in php works for web2py also: if you are positive that once a user is logged-in he would get the same stores forever (so it's not necessary to fetch the data every time you load the page), you can cache it with a high number or simply store the store details in session, i.e. if not session.store_details: #so it will be fetched one time only, if no store_details key is found on session store_details = db(db.stores.user_id == auth.user_id).select() Then you'd have to access this data as session.store_details That's all if I got it correctly: if I didn't understand please post more details. --
Re: [web2py] Re: Auth Decorator / Model Question.
Every store has a unique URL (So store.mysite.com) which will be the key we look up on. This is why the session trick will work. Because we are storing the store data for store.site.com in session.store.mysite.com so every store would have a unique session. I guess the real question is - Should we just cache this query result or should we store it in a session? Which way is preferred for Python / web2py development? Kevin Cackler Tech Daddies 501-205-1512 http://www.techdaddies.com On 9/4/2012 3:22 PM, Niphlod wrote: you can certainly do STORE_DETAILS = db(db.stores.user_id == auth.user_id).select() in models. You'd have the variable STORE_DETAILS available in all controllers and every time a user loads a page the data will be refreshed. In order to reduce the db pressure, you can STORE_DETAILS = db(db.stores.user_id == auth.user_id).select(cache=(cache.ram, 60)) Doing so, the 2nd select will be fired only if more than 60 seconds passed from the 1st (i.e. a new page requested by the same user within 60 seconds will be fetched from the cache and not from the db) What you are doing in php works for web2py also: if you are positive that once a user is logged-in he would get the same stores forever (so it's not necessary to fetch the data every time you load the page), you can cache it with a high number or simply store the store details in session, i.e. if not session.store_details: #so it will be fetched one time only, if no store_details key is found on session store_details = db(db.stores.user_id == auth.user_id).select() Then you'd have to access this data as session.store_details That's all if I got it correctly: if I didn't understand please post more details. -- --
Re: [web2py] Re: Auth Decorator / Model Question.
cache.ram or even better (memcached) is preferred! if you use session and your sessions are stored on filesystem, if you have too much data it will be hard to load on each request. DATA = cache.ram(request.http_host, lambda: db(..).select(cacheable=True), 86400) # keeps for 24 hours. Now, on every place where data is changed you can call cache.ram.clear(regex=None) to reset that cache. --
Re: [web2py] Re: Auth Decorator / Model Question.
Note, sessions are specific to individual users (clients). If you put something in session.store.mysite on a given request, it will only be available to that specific user, not to anyone who goes to the store.mysite.com URL. If you need data accessible to all users store-wide, you should put it in the cache. Anthony On Tuesday, September 4, 2012 4:26:02 PM UTC-4, Kevin C wrote: Every store has a unique URL (So store.mysite.com) which will be the key we look up on. This is why the session trick will work. Because we are storing the store data for store.site.com in session.store.mysite.com so every store would have a unique session. I guess the real question is - Should we just cache this query result or should we store it in a session? Which way is preferred for Python / web2py development? Kevin Cackler Tech Daddies 501-205-1512 http://www.techdaddies.com On 9/4/2012 3:22 PM, Niphlod wrote: you can certainly do STORE_DETAILS = db(db.stores.user_id == auth.user_id).select() in models. You'd have the variable STORE_DETAILS available in all controllers and every time a user loads a page the data will be refreshed. In order to reduce the db pressure, you can STORE_DETAILS = db(db.stores.user_id == auth.user_id).select(cache=(cache.ram, 60)) Doing so, the 2nd select will be fired only if more than 60 seconds passed from the 1st (i.e. a new page requested by the same user within 60 seconds will be fetched from the cache and not from the db) What you are doing in php works for web2py also: if you are positive that once a user is logged-in he would get the same stores forever (so it's not necessary to fetch the data every time you load the page), you can cache it with a high number or simply store the store details in session, i.e. if not session.store_details: #so it will be fetched one time only, if no store_details key is found on session store_details = db(db.stores.user_id == auth.user_id).select() Then you'd have to access this data as session.store_details That's all if I got it correctly: if I didn't understand please post more details. -- --
[web2py] Re: Auth Decorator / Model Question.
session[''store.mysite.com'] = session.get('store.mysite.com ',get_store_info('store.mysite.com')) On Tuesday, 4 September 2012 15:03:10 UTC-5, Kevin C wrote: The users_id comes from the stores table, and is a foreign key to the web2py users table. Really the source of it is irrelevant. What I need to know is what is the best practice for storing this type of information? In PHP, I would do something like the following in a global include file: ?php if(empty($_SESSION['store.mysite.com'])) { $_SESSION['store.mysite.com'] = get_store_info('store.mysite.com'); } ? This would make all of the store info (Including users_id) available to the entire script through the PHP session. I need to know how to do the equivalent in web2py. How do I execute a certain block on every page load (IE to check if the store info has been retrieved and stored yet) and then where should I store that info to make it accessible for the rest of the script? In my limited experience with web2py, I'm thinking I should store the code in a model file and have that code save the store info to a web2py session, but I wanted to get a second opinion before continuing down that path. I hope I have explained it a little more clearly. Basically, how do I execute code on every page load and what is the best practice for storing short term data like that? On Monday, September 3, 2012 10:43:10 PM UTC-5, Kevin C wrote: The title isn't very descriptive, but I hope this paragraph clears things up. Basically we are creating a basic SaaS app. Users will log in at theirname.oursite.com to an administrative panel. Each administrative panel is tied to a user ID from the auth table. So basically here is what should happen: User visits theirname.oursite.com/admin Web2py retrieves users_id field from stores table (This field assigns ownership of each store to a user id) Web2py auth decorator should ensure that the logged in user ID is that of the store owner I understand how to write the decorator, but what is the best way to retrieve the users_id from the stores table and store it? Sessions? Temporary variable that is set on each page load? Please forgive my ignorance. I am completely new to web2py and want to develop using best practices. I appreciate any guidance you can offer. Thank you. --