def get(self, request, format=None): q = request.GET.get('q', None) if not q: return api_error(status.HTTP_400_BAD_REQUEST, 'q invalid.') email_list = [] username = request.user.username if CLOUD_MODE: if is_org_context(request): result_users, err = search_user_in_org(request, q) if err: return err email_list += result_users else: email_list += search_user_with_exact_match(request, q) else: if self._can_use_global_address_book(request): # check user permission according to user's role(default, guest, etc.) # if current user can use global address book if is_org_context(request): result_users, err = search_user_in_org(request, q) if err: return err email_list += result_users else: # search from ccnet email_list += search_user_from_ccnet(q) # search from profile, NOT limit search range email_list += search_user_from_profile(q) else: # if current user can NOT use global address book, # he/she can also search `q` from Contact, # search user from user's contacts email_list += search_user_with_exact_match(request, q) ## search finished, now filter out some users # remove duplicate emails email_list = list({}.fromkeys(email_list).keys()) email_result = [] # remove nonexistent or inactive user email_list_json = json.dumps(email_list) user_obj_list = ccnet_api.get_emailusers_in_list('DB', email_list_json) + \ ccnet_api.get_emailusers_in_list('LDAP', email_list_json) for user_obj in user_obj_list: if user_obj.is_active: email_result.append(user_obj.email) if django_settings.ENABLE_ADDRESSBOOK_OPT_IN: # get users who has setted to show in address book listed_users = Profile.objects.filter( list_in_address_book=True).values('user') listed_user_list = [u['user'] for u in listed_users] email_result = list(set(email_result) & set(listed_user_list)) # check if include myself in user result try: include_self = int(request.GET.get('include_self', 1)) except ValueError: include_self = 1 if include_self == 0 and username in email_result: # reomve myself email_result.remove(username) if CUSTOM_SEARCH_USER: email_result = custom_search_user(request, email_result) # format user result try: size = int(request.GET.get('avatar_size', 32)) except ValueError: size = 32 formated_result = format_searched_user_result(request, email_result[:10], size) return Response({"users": formated_result})
def get(self, request, format=None): q = request.GET.get('q', None) if not q: return api_error(status.HTTP_400_BAD_REQUEST, 'q invalid.') email_list = [] username = request.user.username if self._can_use_global_address_book(request): # check user permission according to user's role(default, guest, etc.) # if current user can use global address book if CLOUD_MODE: if is_org_context(request): # get all org users url_prefix = request.user.org.url_prefix try: all_org_users = ccnet_api.get_org_users_by_url_prefix( url_prefix, -1, -1) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) limited_emails = [] for org_user in all_org_users: # prepare limited emails for search from profile limited_emails.append(org_user.email) # search user from org users if q in org_user.email: email_list.append(org_user.email) # search from profile, limit search range in all org users email_list += search_user_from_profile_with_limits( q, limited_emails) elif ENABLE_GLOBAL_ADDRESSBOOK: # search from ccnet email_list += search_user_from_ccnet(q) # search from profile, NOT limit search range email_list += search_user_from_profile(q) else: # in cloud mode, user will be added to Contact when share repo # search user from user's contacts email_list += search_user_when_global_address_book_disabled( request, q) else: # not CLOUD_MODE # search from ccnet email_list += search_user_from_ccnet(q) # search from profile, NOT limit search range email_list += search_user_from_profile(q) else: # if current user can NOT use global address book, # he/she can also search `q` from Contact, # search user from user's contacts email_list += search_user_when_global_address_book_disabled( request, q) ## search finished # remove duplicate emails email_list = {}.fromkeys(email_list).keys() email_result = [] # remove nonexistent or inactive user for email in email_list: try: user = User.objects.get(email=email) if user.is_active: email_result.append(email) except User.DoesNotExist: continue # check if include myself in user result try: include_self = int(request.GET.get('include_self', 1)) except ValueError: include_self = 1 if include_self == 0 and username in email_result: # reomve myself email_result.remove(username) if CUSTOM_SEARCH_USER: email_result = custom_search_user(request, email_result) # format user result try: size = int(request.GET.get('avatar_size', 32)) except ValueError: size = 32 formated_result = format_searched_user_result(request, email_result[:10], size) return HttpResponse(json.dumps({"users": formated_result}), status=200, content_type='application/json; charset=utf-8')
def get(self, request, format=None): q = request.GET.get('q', None) if not q: return api_error(status.HTTP_400_BAD_REQUEST, 'q invalid.') email_list = [] username = request.user.username if self._can_use_global_address_book(request): # check user permission according to user's role(default, guest, etc.) # if current user can use global address book if CLOUD_MODE: if is_org_context(request): # get all org users url_prefix = request.user.org.url_prefix try: all_org_users = ccnet_api.get_org_users_by_url_prefix(url_prefix, -1, -1) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) limited_emails = [] for org_user in all_org_users: # prepare limited emails for search from profile limited_emails.append(org_user.email) # search user from org users if q in org_user.email: email_list.append(org_user.email) # search from profile, limit search range in all org users email_list += search_user_from_profile_with_limits(q, limited_emails) elif ENABLE_GLOBAL_ADDRESSBOOK: # search from ccnet email_list += search_user_from_ccnet(q) # search from profile, NOT limit search range email_list += search_user_from_profile(q) else: # in cloud mode, user will be added to Contact when share repo # search user from user's contacts email_list += search_user_when_global_address_book_disabled(request, q) else: # not CLOUD_MODE # search from ccnet email_list += search_user_from_ccnet(q) # search from profile, NOT limit search range email_list += search_user_from_profile(q) else: # if current user can NOT use global address book, # he/she can also search `q` from Contact, # search user from user's contacts email_list += search_user_when_global_address_book_disabled(request, q) ## search finished, now filter out some users # remove duplicate emails email_list = {}.fromkeys(email_list).keys() email_result = [] # remove nonexistent or inactive user email_list_json = json.dumps(email_list) user_obj_list = ccnet_api.get_emailusers_in_list('DB', email_list_json) + \ ccnet_api.get_emailusers_in_list('LDAP', email_list_json) for user_obj in user_obj_list: if user_obj.is_active: email_result.append(user_obj.email) if django_settings.ENABLE_ADDRESSBOOK_OPT_IN: # get users who has setted to show in address book listed_users = Profile.objects.filter(list_in_address_book=True).values('user') listed_user_list = [ u['user'] for u in listed_users ] email_result = list(set(email_result) & set(listed_user_list)) # check if include myself in user result try: include_self = int(request.GET.get('include_self', 1)) except ValueError: include_self = 1 if include_self == 0 and username in email_result: # reomve myself email_result.remove(username) if CUSTOM_SEARCH_USER: email_result = custom_search_user(request, email_result) # format user result try: size = int(request.GET.get('avatar_size', 32)) except ValueError: size = 32 formated_result = format_searched_user_result( request, email_result[:10], size) return Response({"users": formated_result})