<s> from django . db import models <EOL> from django . contrib . auth . models import User <EOL> from treeio . core . conf import settings <EOL> KEY_SIZE = <NUM_LIT> <EOL> SECRET_SIZE = <NUM_LIT:32> <EOL> CONSUMER_DB = getattr ( settings , '<STR_LIT>' , '<STR_LIT:default>' ) <EOL> class KeyManager ( models . Manager ) : <EOL> '''<STR_LIT>''' <EOL> def generate_random_codes ( self ) : <EOL> key = User . objects . make_random_password ( length = KEY_SIZE ) <EOL> secret = User . objects . make_random_password ( length = SECRET_SIZE ) <EOL> while self . filter ( key__exact = key , secret__exact = secret ) . count ( ) : <EOL> secret = User . objects . make_random_password ( length = SECRET_SIZE ) <EOL> return key , secret <EOL> class ConsumerManager ( KeyManager ) : <EOL> def create_consumer ( self , name , description = None , user = None , using = CONSUMER_DB ) : <EOL> """<STR_LIT>""" <EOL> consumer , created = self . using ( using ) . get_or_create ( name = name ) <EOL> if user : <EOL> consumer . user = user <EOL> if description : <EOL> consumer . description = description <EOL> if created : <EOL> consumer . key , consumer . secret = self . generate_random_codes ( ) <EOL> consumer . save ( ) <EOL> return consumer <EOL> _default_consumer = None <EOL> class ResourceManager ( models . Manager ) : <EOL> _default_resource = None <EOL> def get_default_resource ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not self . _default_resource : <EOL> self . _default_resource = self . get ( name = name ) <EOL> return self . _default_resource <EOL> class TokenManager ( KeyManager ) : <EOL> def create_token ( self , consumer_id , token_type , timestamp , user = None , using = None ) : <EOL> """<STR_LIT>""" <EOL> if using : <EOL> manager = self . using ( using ) <EOL> else : <EOL> manager = self <EOL> token , created = manager . get_or_create ( consumer_id = consumer_id , <EOL> token_type = token_type , <EOL> timestamp = timestamp , <EOL> user = user ) <EOL> if created : <EOL> token . key , token . secret = self . generate_random_codes ( ) <EOL> token . save ( ) <EOL> return token </s>
