How to load some default data into my model?

0 views
Skip to first unread message

Matt Wilson

unread,
Mar 21, 2008, 9:29:38 AM3/21/08
to TurboGears
I'm using the TG identity system and my app uses three different
groups to control user privileges. I have "employees", "supervisors",
and "administrators" as groups.

I have two questions:

1. Is there some way that I can make sure when I first set up my
application that these values are stored in my database?

2. I use Group.by_group_name("employees") all the time in lots of
different controllers. I would prefer to lookup the ID for the
employees group once when I fire up the application and then I want to
store it as a class-level constant.

I can not hard-code the ID in my program.

All comments welcome!

Matt

Lukasz Szybalski

unread,
Mar 21, 2008, 10:13:09 AM3/21/08
to turbo...@googlegroups.com

session variable? cookie?
http://lucasmanual.com/mywiki/TurboGears#head-82370b50f86921fa6bf46d01ebd7d880dd9f5ad2
If anything you could encrypt that variable to hide the id number.

Other then that you either have to look it up each time or pass it
along to each function.
Lucas

Matt Wilson

unread,
Mar 21, 2008, 10:50:41 AM3/21/08
to TurboGears
I want to do the by_group_name lookup once when I start my
application, before any browser hits my site.



On Mar 21, 10:13 am, "Lukasz Szybalski" <szybal...@gmail.com> wrote:
> On Fri, Mar 21, 2008 at 8:29 AM, Matt Wilson <mw44...@gmail.com> wrote:
>
> > I'm using the TG identity system and my app uses three different
> > groups to control user privileges. I have "employees", "supervisors",
> > and "administrators" as groups.
>
> > I have two questions:
>
> > 1. Is there some way that I can make sure when I first set up my
> > application that these values are stored in my database?
>
> > 2. I use Group.by_group_name("employees") all the time in lots of
> > different controllers. I would prefer to lookup the ID for the
> > employees group once when I fire up the application and then I want to
> > store it as a class-level constant.
>
> > I can not hard-code the ID in my program.
>
> session variable? cookie?http://lucasmanual.com/mywiki/TurboGears#head-82370b50f86921fa6bf46d0...

Diez B. Roggisch

unread,
Mar 21, 2008, 12:34:42 PM3/21/08
to turbo...@googlegroups.com
Matt Wilson schrieb:

There are a bazillion ways to this, by why don't you do this - which
solves both of your problems at once?

class Group(...):

... # your declarations here

@property
def employees(self):
try:
return self.by_name("employees")
except SQLOBjectNotFound:
return self.__class__(name="employees")


Do that for all your three groups, and you're done.


Diez

Christoph Zwerschke

unread,
Mar 21, 2008, 3:08:48 PM3/21/08
to turbo...@googlegroups.com
Matt Wilson schrieb:

> I have two questions:
>
> 1. Is there some way that I can make sure when I first set up my
> application that these values are stored in my database?
>
> 2. I use Group.by_group_name("employees") all the time in lots of
> different controllers. I would prefer to lookup the ID for the
> employees group once when I fire up the application and then I want to
> store it as a class-level constant.

Create a function initdb() and put it in model.py or in a separate
module initdb.py. In this function you check the database tables and
populate them with initial data if they are empty (groups, permissions,
admin user etc.). You can also prefetch some of the data (e.g. the
employees group) and make it available in a global object.

Call this function inside the start() function of the commands.py module
or in the __init__ method of your root controller.

Maybe we should provide some template code for this in our quickstart
templates?

-- Christoph

Matt Wilson

unread,
Mar 21, 2008, 6:00:12 PM3/21/08
to TurboGears
Christoph -- thanks for the explanation!
Reply all
Reply to author
Forward
0 new messages