Book -- draft initialization paragraphs for Chapter 13

110 views
Skip to first unread message

Dave S

unread,
Aug 31, 2016, 5:39:52 AM8/31/16
to web2py-d...@googlegroups.com
In response to the DB initialization discussion in the Users forum (see <URL:https://groups.google.com/d/msg/web2py/b-mW4VsajR4/ICT5ET26EgAJ>)
I've taken a quick swing at adding paragraphs to Chapter 13 (Deployment).  I can't do a proper pull request tonight, but I can let you peek at my scribblings so you can make comments.

I think these 2 sections should be added after Start the scheduler as a Linux service (upstart) and before Windows.

---- begin draft ----
#### Initial Queuing of a Repeating Task

Task definition is done in a model file.  But the initial
queuing of a repeating task is most easily done from a non-exposed
controller function.  By doing this in a controller
rather than in a model file, you remove unnecessary database accesses
that would occur on every request to your website.

A non-exposed function is any controller function that has an argument,
so writing something like the following in a controller (e.g. default.py)
gives you 

``
def reinit_sched(protect=None):
    # put any preparatory code here
    #
    # schedule 2 tasks:
    sched.queue_task(foo)
    sched.queue_task(bar)
    #
    # put any afters code here
``

and you can then easily call it with ``web2py.py -M -S appname/default/reinit_sched``

#### Populating a Database on Deployment

Your application may have a need for a pre-populated database table.
A simple example might be a table of color names, or the names of the
months (perhaps in a special format not used in Python datetime
routines).  More complicated examples might include an initial inventory
of parts, or the initial pages of a wiki.

The simple examples might be done via ``db.mycolors.insert()``;
the more complex examples might use a CSV file to fill in the values.
As in the task queuing section above, this should be done
using non-exposed controller functions.
Table definition for a database is done in model files, but model files
are read on every request, so one-time actions should not be in the
model files where they would lower the responsiveness of your site.

Again, a non-exposed function is any controller function that has an argument,
so writing something like the following in a controller (e.g. default.py)
gives you 

``
def populate_colors(protected=None)
    collist = ["lime","#00FF00", "olive","#808000",
                "fuchsia","#FF00FF","maroon","#800000"]:
    for i in range(0,len(collist),2):
        k,v = collist[i:i+2]
        db.mycolors.insert(name=k, code=v)

you can then easily call it with ``web2py.py -M -S appname/default/populate_colors``

For the complex example using a CSV file,
your function might look like:

def populate_parts(exposed=False):
  db.myinventory.import_from_csv_file( 
    open(os.path.join(request.folder,
                      os.path.join('private', db_parts.csv')
                     ),
         'r') 
    )

and this would be called with ``web2py.py -M -S appname/default/populate_parts``
--- end draft ---

Editors may use blue or red pencil, or large scissors.

Thanks to Niphlod, Yoel Benítez Fonseca, Richard, and Ben Lawrence 

/dps

Niphlod

unread,
Aug 31, 2016, 6:22:15 AM8/31/16
to web2py-developers
looks good BUT I'd urge to encourage examples to include db.commit() to avoid unnecessary "I did as told but nothing worked as supposed" ^_^

Dave S

unread,
Aug 31, 2016, 2:04:38 PM8/31/16
to web2py-developers


On Wednesday, August 31, 2016 at 3:22:15 AM UTC-7, Niphlod wrote:
looks good BUT I'd urge to encourage examples to include db.commit() to avoid unnecessary "I did as told but nothing worked as supposed" ^_^

ooops!


/dps
 

Dave S

unread,
Sep 2, 2016, 6:16:53 AM9/2/16
to web2py-developers
Updated draft:

#### Initial Queuing of a Repeating Task

Task definition is done in a model file.  But the initial
queuing of a repeating task is most easily done from a non-exposed
controller function.  By doing this in a controller
rather than in a model file, you remove unnecessary database accesses
that would occur on every request to your website.

[The function could also be in a module or a private file,
but in the controller, the web2py API and environment is readily
accessible.]
[The function could also be in a module or a private file,
but in the controller, the web2py API and environment is readily
accessible.]

Again, a non-exposed function is any controller function that has an argument,
so writing something like the following in a controller (e.g. default.py)
gives you 

``
def populate_colors(protected=None)
    collist = ["lime","#00FF00", "olive","#808000",
                "fuchsia","#FF00FF","maroon","#800000"]:
    for i in range(0,len(collist),2):
        k,v = collist[i:i+2]
        db.mycolors.insert(name=k, code=v)
    db.commit()

you can then easily call it with ``web2py.py -M -S appname/default/populate_colors``

For the complex example using a CSV file,
your function might look like:

def populate_colors(exposed=False):
  db.mycolors.import_from_csv_file( 
    open(os.path.join(request.folder,
                      os.path.join('private', db_colors.csv')
                     ),
         'r') 
    )
  db.commit()

and this would be called with ``web2py.py -M -S appname/default/populate_colors``

See also Chapter 14,


/dps

Dave S

unread,
Sep 7, 2016, 4:08:25 AM9/7/16
to web2py-developers


On Friday, September 2, 2016 at 3:16:53 AM UTC-7, Dave S wrote:
Updated draft:



This is PR #340 in web2py-book, but I think I got the markmin for the cross-reference wrong.

/dps
Reply all
Reply to author
Forward
0 new messages