Fantastic, the request_tenant method is just what I was looking for!
So this is what I've got working:
def requires_account(f):
"""
Redirect to the account not found page if there is no account
(decorator function)
"""
if not get_account():
redirect(URL('accounts', 'not_found'))
return f
def get_account():
"""
Get the account from the subdomain and store it in the session, if
not already stored
"""
if not session.account:
subdomain = request.env.http_host.split('.')[:-2].pop()
session.account =
db(db.account.subdomain==subdomain).select(
db.account.id,
db.account.title).first()
return session.account
defined in db.py (is that the best place to put them?) - then after
I've defined the account table, I've got this:
db._request_tenant = 'account_id'
db._common_fields=[Field('account_id',default=
session.account.id,
writable=False, readable=False)]
Which seems to work rather nicely.
Thank you very much for your reply!
On Aug 15, 10:53 pm, Anthony <
abasta...@gmail.com> wrote:
> You might want to consider using this:
https://groups.google.com/d/msg/web2py/NrvxeWQJvH0/wbafxppaf1QJ(note,
> 'request_precinct' has been changed to the more general 'request_tenant', as
> noted later in that thread). Otherwise, I suppose you could use the Auth
> groups functionality (
http://web2py.com/book/default/chapter/08#Authorization) -- create a group
> for each subdomain and assign/check permissions based on the current
> request's subdomain. Note, the full multi-tenancy solution (first
> link) might be better because it allows you to easily segment every single
> database table by subdomain so any queries return only results related to
> the particular subdomain.
>
> Also, rather than creating your own requires_account decorator, you could
> probably just use auth.requires (seehttp://
web2py.com/book/default/chapter/08#Combining-Requirements).