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

7.13. newUserRecord(): Create the initial user record

The default state of a new user is to be on chapter 1, with a session cookie.

reader.cgi
# - - -   n e w U s e r R e c o r d

def newUserRecord():
    '''Create a new UserRecord instance.

      [ return a new UserRecord with a random userId,
        chapterNo=1, persist=0, and expiration=0 ]
    '''

To generate a random userId, we'll use the random.choice() function, which picks a random member of a sequence. We'll pick the characters from a concatenation of string.letters, which contains the 52 upper- and lowercase letters, and string.digits, the ten digits. The length of the user ID is defined in Section 7.6.22, “USER_ID_LENGTH.

reader.cgi
    #-- 1 --
    # [ userId  :=  USER_ID_LENGTH random letters or digits ]
    c  =  string.letters + string.digits
    L  =  [random.choice(c) for i in range(USER_ID_LENGTH)]
    userId  =  "".join ( L )

The expiration time will be adjusted by the caller, so we don't need to worry about it here. See Section 7.28, “class UserRecord.

reader.cgi
    #-- 2 --
    # [ userRecord  :=  a new UserRecord instance with
    #       userId=(userId), chapterNo=1, persist=0, and
    #       expiration=0 ]
    userRecord  =  UserRecord ( userId, 1, 0, 0 )

    #-- 3 --
    return userRecord