<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django . db . models import Q <EOL> from django . utils import six <EOL> from djblets . util . decorators import augment_method_from <EOL> from djblets . webapi . decorators import ( webapi_request_fields , <EOL> webapi_response_errors ) <EOL> from djblets . webapi . errors import ( DOES_NOT_EXIST , NOT_LOGGED_IN , <EOL> PERMISSION_DENIED ) <EOL> from djblets . webapi . resources . user import UserResource as DjbletsUserResource <EOL> from reviewboard . accounts . backends import get_enabled_auth_backends <EOL> from reviewboard . accounts . errors import UserQueryError <EOL> from reviewboard . avatars import avatar_services <EOL> from reviewboard . site . urlresolvers import local_site_reverse <EOL> from reviewboard . webapi . base import WebAPIResource <EOL> from reviewboard . webapi . decorators import webapi_check_local_site <EOL> from reviewboard . webapi . errors import USER_QUERY_ERROR <EOL> from reviewboard . webapi . resources import resources <EOL> class UserResource ( WebAPIResource , DjbletsUserResource ) : <EOL> """<STR_LIT>""" <EOL> item_child_resources = [ <EOL> resources . api_token , <EOL> resources . archived_review_request , <EOL> resources . muted_review_request , <EOL> resources . watched , <EOL> ] <EOL> fields = dict ( { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } , ** DjbletsUserResource . fields ) <EOL> hidden_fields = ( '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_queryset ( self , request , local_site_name = None , * args , ** kwargs ) : <EOL> search_q = request . GET . get ( '<STR_LIT:q>' , None ) <EOL> for backend in get_enabled_auth_backends ( ) : <EOL> try : <EOL> backend . query_users ( search_q , request ) <EOL> except Exception as e : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> backend , e , exc_info = <NUM_LIT:1> ) <EOL> local_site = self . _get_local_site ( local_site_name ) <EOL> is_list = kwargs . get ( '<STR_LIT>' , False ) <EOL> if local_site and ( is_list or not local_site . public ) : <EOL> query = local_site . users . filter ( is_active = True ) <EOL> else : <EOL> query = self . model . objects . filter ( is_active = True ) <EOL> if search_q : <EOL> q = None <EOL> for backend in get_enabled_auth_backends ( ) : <EOL> try : <EOL> q = backend . search_users ( search_q , request ) <EOL> except Exception as e : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> backend , e , exc_info = <NUM_LIT:1> ) <EOL> if q : <EOL> break <EOL> if not q : <EOL> q = Q ( username__istartswith = search_q ) <EOL> if request . GET . get ( '<STR_LIT>' , None ) : <EOL> q = q | ( Q ( first_name__istartswith = search_q ) | <EOL> Q ( last_name__istartswith = search_q ) ) <EOL> query = query . filter ( q ) <EOL> return query . extra ( select = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> } ) <EOL> def serialize_object ( self , obj , request = None , * args , ** kwargs ) : <EOL> data = super ( UserResource , self ) . serialize_object ( <EOL> obj , request = request , * args , ** kwargs ) <EOL> if request : <EOL> if not obj . is_profile_visible ( request . user ) : <EOL> for field in self . hidden_fields : <EOL> del data [ field ] <EOL> return data <EOL> def serialize_url_field ( self , user , ** kwargs ) : <EOL> return local_site_reverse ( '<STR_LIT:user>' , kwargs [ '<STR_LIT>' ] , <EOL> kwargs = { '<STR_LIT:username>' : user . username } ) <EOL> def serialize_avatar_urls_field ( self , user , request = None , ** kwargs ) : <EOL> service = avatar_services . default_service <EOL> if service : <EOL> return service . get_avatar_urls ( request , user , <NUM_LIT> ) <EOL> return None <EOL> def has_access_permissions ( self , * args , ** kwargs ) : <EOL> return True <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( NOT_LOGGED_IN , PERMISSION_DENIED , DOES_NOT_EXIST , <EOL> USER_QUERY_ERROR ) <EOL> @ webapi_request_fields ( <EOL> optional = { <EOL> '<STR_LIT:q>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> allow_unknown = True <EOL> ) <EOL> def get_list ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return super ( UserResource , self ) . get_list ( * args , ** kwargs ) <EOL> except UserQueryError as e : <EOL> return USER_QUERY_ERROR . with_message ( e . msg ) <EOL> @ webapi_check_local_site <EOL> @ augment_method_from ( WebAPIResource ) <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> user_resource = UserResource ( ) </s>
