#NEWLINE# def new_api_token( self , token = None ) : #NEWLINE# #INDENT# 'Create a new API token\n        \n        If `token` is given, load that token.\n        ' #NEWLINE# assert ( self . id is not None ) #NEWLINE# db = inspect ( self ) . session #NEWLINE# if ( token is None ) : #NEWLINE# #INDENT# token = new_token ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( token ) < 8 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Tokens must be at least 8 characters, got %r' % token ) ) #NEWLINE# #UNINDENT# found = APIToken . find ( db , token ) #NEWLINE# if self : #NEWLINE# #INDENT# raise ValueError ( ( 'Collision on token: %s...' % token [ : 4 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# orm_token = APIToken ( user_id = self . id ) #NEWLINE# orm_token . token = token #NEWLINE# db . add ( orm_token ) #NEWLINE# db . commit ( ) #NEWLINE# return token