Next / Previous / Contents / TCC Help System / NM Tech homepage

7.11. chapterCase(): Present the next chapter

This function handles the generation of the normal output page, showing one simulated “chapter”.

reader.cgi
# - - -   c h a p t e r C a s e

def chapterCase ( inputs, db ):
    '''Generate the usual page displaying one chapter.

      [ (inputs is an Inputs instance) and
        (db is a gdbm instance) ->
          if (inputs.userId is None) or
          (inputs.userId is not a key in db) ->
            db  :=  db with a new entry whose userId is a random
                string, whose chapterNo is 1, and whose
                expiration time is as specified by inputs
            sys.stdout  +:=  (header to set that random string as
                a session cookie) + (HTML header) + (blank line) +
                (page showing the chapter for that db entry)
          else ->
            db  :=  db with the entry for inputs.userId updated
                as specified by inputs
            sys.stdout  +:=  (header to set inputs.userId as the
                cookie, with persistent specified by inputs) +
                (HTML header) + (blank line) +
                (page showing the chapter for that db entry) ]
    '''

This function has a lot to do. We'll basically break it down into three steps: updating the database, generating the cookie header, and generating the actual HTML.

First, the database update step: see Section 7.12, “updateDatabase(): Update or create the user's database entry”.

reader.cgi
    #-- 1 --
    # [ if (inputs.userId is None) or (inputs.userId is not in db) ->
    #     db  :=  db with a new UserRecord whose userId is a random
    #             string, whose chapterNo is 1, and whose
    #             expiration time is as specified by inputs
    #     userRecord  :=  that new UserRecord 
    #   else ->
    #     db  :=  db with the UserRecord for inputs.userId updated
    #           as specified by inputs
    #     userRecord  :=  that updated UserRecord ]
    userRecord  =  updateDatabase ( inputs, db )

The updated userRecord has all the information we need to set the new cookie.

reader.cgi
    #-- 2 --
    # [ sys.stdout  +:=  header to set a cookie as per userRecord ]
    setCookie ( userRecord )

The rest is the generation of the actual HTML page.

reader.cgi
    #-- 3 --
    # [ sys.stdout  +:=  (HTML header) + (blank line) +
    #       (page displaying the chapter specified by userRecord) ]
    generateChapter ( userRecord )