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

7.20. Inputs.__init__(): Constructor

Here is the beginning of the class, and the constructor.

reader.cgi
# - - - - -   c l a s s   I n p u t s

class Inputs:
    '''Container for all the script's inputs.
    '''

# - - -   I n p u t s . _ _ i n i t _ _

    def __init__ ( self ):
        '''Constructor for Inputs.
        '''

The existing cookie value comes from the environmental variable whose name is given in Section 7.6.1, “HTTP_COOKIE. If that variable has a value, we use the Cookie.SimpleCookie() constructor to convert it to a SimpleCookie instance, and then we extract the user ID from that.

reader.cgi
        #-- 1 --
        # [ if os.environ has a key HTTP_COOKIE ->
        #     self.userId  :=  the value for key MORSEL_NAME
        #         of a cookie made from os.environ[HTTP_COOKIE]
        #   else ->
        #     self.userId  :=  '' ]
        try:
            rawCookie  =  os.environ[HTTP_COOKIE]
            bakedCookie  =  Cookie.SimpleCookie ( rawCookie )
            self.userId  =  bakedCookie[MORSEL_NAME].value
        except KeyError:
            self.userId  =  ''

Now convert the various form elements into the equivalent attributes.

reader.cgi
        #-- 2 --
        # [ form  :=  a cgi.FieldStorage instance containing
        #       any name-value pairs passed to this script ]
        form  =  cgi.FieldStorage()

For control names, see Section 7.6.4, “PREV_CONTROL, Section 7.6.2, “NEXT_CONTROL, Section 7.6.11, “FORGET_CONTROL, and Section 7.6.6, “REQUEST_GROUP.

reader.cgi
        #-- 3 --
        # [ if form has a PREV_CONTROL value ->
        #     self.prev  :=  True
        #   else -> I ]
        if form.has_key ( PREV_CONTROL ):
            self.prev  =  True
        else:
            self.prev  =  False

        #-- 4 --
        # [ simile ]
        if form.has_key ( NEXT_CONTROL ):
            self.next  =  True
        else:
            self.next  =  False

        #-- 5 --
        if form.has_key ( FORGET_CONTROL ):
            self.forget  =  True
        else:
            self.forget  =  False

Next we get the value of the REQUEST_GROUP radiobutton group. If neither radiobuttons was set, we won't get a value, but this can happen only if the form is not designed correctly (with neither of the radiobuttons carrying a checked='checked' attribute). In that case, implement the default value here. For the control values of the radiobuttons, see Section 7.6.7, “REQUEST_SESSION and Section 7.6.8, “REQUEST_PERSIST.

reader.cgi
        #-- 5 --
        # [ if form has a REQUEST_GROUP value ->
        #     requestValue  :=  that value
        #   else ->
        #     requestValue  :=  REQUEST_SESSION ]
        try:
            requestValue  =  form[REQUEST_GROUP].value
        except KeyError:
            requestValue  =  REQUEST_SESSION

        if requestValue == REQUEST_PERSIST:
            self.persist  =  True
        else:
            self.persist  =  False