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

7.8. deleteCase(): Delete the cookie

This function handles the case where the user wants their cookie removed from our system.

reader.cgi
# - - -   d e l e t e C a s e

def deleteCase ( inputs, db ):
    '''Delete the user's cookie and database entry.

      [ (inputs is an Inputs instance) and
        (db is a UserDatabase instance) ->
          db  :=  db with the entry for key inputs.userId deleted
          sys.stdout  +:=  (header to delete the cookie) +
              (HTML header) + (blank line) +
              (deletion-successful page) ]
    '''

Deleting an entry from a gdbm file works the same as if it were a normal dictionary: we use the del statement. However, there is no guarantee this entry exists in the database, and in that case the del statement will raise a KeyError exception, which we can happily ignore.

reader.cgi
    #-- 1 --
    # [ if db has an entry with key=inputs.userId ->
    #     db  :=  db with that entry removed
    #   else -> I ]
    try:
        del db[inputs.userId]
    except KeyError:
        pass

Deleting the cookie from the user's browser is done by writing a header line that sets a cookie with a “max-age” attribute of zero. In this case, the cookie's name is ignored. See Section 7.6.17, “MAX_AGE.

reader.cgi
    #-- 2 --
    # [ antiCookie  :=  a Cookie.SimpleCookie instance with
    #       name 'ignore' and max-age=0 ]
    antiCookie  =  Cookie.SimpleCookie()
    antiCookie['ignore']  =  'ignore'
    antiMorsel  =  antiCookie['ignore']
    antiMorsel[MAX_AGE]  =  '0'

    #-- 3 --
    # [ sys.stdout  +:=  header that sets antiCookie ]
    print antiCookie.output()

For the HTML generation of the deletion message page, see Section 7.9, “deletionPage(): Generate the deletion-successful page”.

reader.cgi
    #-- 4 --
    # [ sys.stdout  +:=  (HTML header) + (blank line) +
    #                    (deletion-successful page ]
    deletionPage()