<s> """<STR_LIT>""" <EOL> import httplib <EOL> from bottle import abort , get , route , request , local <EOL> from marshmallow import ValidationError <EOL> from marshmallow_jsonapi import Schema , fields <EOL> from codalab . lib import formatting <EOL> from codalab . lib . spec_util import NAME_REGEX <EOL> from codalab . server . authenticated_plugin import ( <EOL> AuthenticatedPlugin , <EOL> UserVerifiedPlugin , <EOL> ) <EOL> PUBLIC_USER_FIELDS = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> class DataSize ( fields . Field ) : <EOL> def _serialize ( self , value , attr , obj ) : <EOL> return formatting . size_str ( value ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> return formatting . parse_size ( value ) <EOL> class Duration ( fields . Field ) : <EOL> def _serialize ( self , value , attr , obj ) : <EOL> return formatting . duration_str ( value ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> return formatting . parse_duration ( value ) <EOL> class UserSchema ( Schema ) : <EOL> id = fields . String ( dump_only = True , attribute = '<STR_LIT>' ) <EOL> user_name = fields . String ( ) <EOL> email = fields . Email ( dump_only = True ) <EOL> first_name = fields . String ( allow_none = True ) <EOL> last_name = fields . String ( allow_none = True ) <EOL> affiliation = fields . String ( allow_none = True ) <EOL> url = fields . Url ( allow_none = True ) <EOL> time_quota = Duration ( dump_only = True ) <EOL> time_used = Duration ( dump_only = True ) <EOL> disk_quota = DataSize ( dump_only = True ) <EOL> disk_used = DataSize ( dump_only = True ) <EOL> last_login = fields . LocalDateTime ( "<STR_LIT>" , dump_only = True ) <EOL> date_joined = fields . LocalDateTime ( "<STR_LIT>" , dump_only = True ) <EOL> class Meta : <EOL> type_ = '<STR_LIT>' <EOL> @ get ( '<STR_LIT>' , apply = AuthenticatedPlugin ( ) , skip = UserVerifiedPlugin ) <EOL> def fetch_authenticated_user ( ) : <EOL> """<STR_LIT>""" <EOL> return UserSchema ( ) . dump ( request . user ) . data <EOL> @ route ( '<STR_LIT>' , method = '<STR_LIT>' , apply = AuthenticatedPlugin ( ) , skip = UserVerifiedPlugin ) <EOL> def update_authenticated_user ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user_info , errors = UserSchema ( strict = True ) . load ( request . json , partial = True ) <EOL> except ValidationError as err : <EOL> message = '<STR_LIT:U+0020>' . join ( [ e [ '<STR_LIT>' ] for e in err . messages [ '<STR_LIT>' ] ] ) <EOL> abort ( httplib . BAD_REQUEST , message ) <EOL> user_info [ '<STR_LIT>' ] = request . user . user_id <EOL> if ( user_info . get ( '<STR_LIT>' , request . user . user_name ) != request . user . user_name and <EOL> local . model . user_exists ( user_info [ '<STR_LIT>' ] , None ) ) : <EOL> abort ( httplib . BAD_REQUEST , "<STR_LIT>" % user_info [ '<STR_LIT>' ] ) <EOL> if not NAME_REGEX . match ( user_info . get ( '<STR_LIT>' , request . user . user_name ) ) : <EOL> abort ( httplib . BAD_REQUEST , "<STR_LIT>" ) <EOL> local . model . update_user_info ( user_info ) <EOL> return UserSchema ( ) . dump ( local . model . get_user ( request . user . user_id ) ) . data <EOL> @ get ( '<STR_LIT>' ) <EOL> def fetch_user ( id ) : <EOL> """<STR_LIT>""" <EOL> user = local . model . get_user ( id ) <EOL> if user is None : <EOL> abort ( httplib . NOT_FOUND , "<STR_LIT>" % id ) <EOL> return UserSchema ( only = PUBLIC_USER_FIELDS ) . dump ( user ) . data <EOL> @ get ( '<STR_LIT>' ) <EOL> def fetch_users ( ) : <EOL> """<STR_LIT>""" <EOL> usernames = set ( request . query . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ) <EOL> usernames |= set ( request . query . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ) <EOL> usernames . discard ( '<STR_LIT>' ) <EOL> users = local . model . get_users ( usernames = ( usernames or None ) ) <EOL> return UserSchema ( many = True , only = PUBLIC_USER_FIELDS ) . dump ( users ) . data </s>
