mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-03 16:10:26 +00:00
update search user when not use global address book
This commit is contained in:
@@ -14,7 +14,6 @@ import seaserv
|
|||||||
from seahub.api2.authentication import TokenAuthentication
|
from seahub.api2.authentication import TokenAuthentication
|
||||||
from seahub.api2.throttling import UserRateThrottle
|
from seahub.api2.throttling import UserRateThrottle
|
||||||
from seahub.api2.utils import api_error
|
from seahub.api2.utils import api_error
|
||||||
|
|
||||||
from seahub.utils import is_valid_email, is_org_context
|
from seahub.utils import is_valid_email, is_org_context
|
||||||
from seahub.base.accounts import User
|
from seahub.base.accounts import User
|
||||||
from seahub.base.templatetags.seahub_tags import email2nickname
|
from seahub.base.templatetags.seahub_tags import email2nickname
|
||||||
@@ -65,7 +64,7 @@ class SearchUser(APIView):
|
|||||||
for user in all_org_users:
|
for user in all_org_users:
|
||||||
limited_emails.append(user.email)
|
limited_emails.append(user.email)
|
||||||
|
|
||||||
email_list += search_user_from_profile(q, limited_emails)
|
email_list += search_user_from_profile_with_limits(q, limited_emails)
|
||||||
|
|
||||||
elif ENABLE_GLOBAL_ADDRESSBOOK:
|
elif ENABLE_GLOBAL_ADDRESSBOOK:
|
||||||
# search from ccnet
|
# search from ccnet
|
||||||
@@ -76,7 +75,7 @@ class SearchUser(APIView):
|
|||||||
else:
|
else:
|
||||||
# in cloud mode, user will be added to Contact when share repo
|
# in cloud mode, user will be added to Contact when share repo
|
||||||
# search user from user's contacts
|
# search user from user's contacts
|
||||||
email_list += search_user_from_contact(request, q)
|
email_list += search_user_when_global_address_book_disabled(request, q)
|
||||||
else:
|
else:
|
||||||
# not CLOUD_MODE
|
# not CLOUD_MODE
|
||||||
# search from ccnet
|
# search from ccnet
|
||||||
@@ -88,13 +87,9 @@ class SearchUser(APIView):
|
|||||||
# if current user can NOT use global address book,
|
# if current user can NOT use global address book,
|
||||||
# he/she can also search `q` from Contact,
|
# he/she can also search `q` from Contact,
|
||||||
# search user from user's contacts
|
# search user from user's contacts
|
||||||
email_list += search_user_from_contact(request, q)
|
email_list += search_user_when_global_address_book_disabled(request, q)
|
||||||
|
|
||||||
## search finished
|
## search finished
|
||||||
# check if `q` is a valid email
|
|
||||||
if is_valid_email(q):
|
|
||||||
email_list.append(q)
|
|
||||||
|
|
||||||
# remove duplicate emails
|
# remove duplicate emails
|
||||||
email_list = {}.fromkeys(email_list).keys()
|
email_list = {}.fromkeys(email_list).keys()
|
||||||
|
|
||||||
@@ -181,16 +176,11 @@ def search_user_from_ccnet(q):
|
|||||||
|
|
||||||
return email_list
|
return email_list
|
||||||
|
|
||||||
def search_user_from_profile(q, limited_emails=[]):
|
def search_user_from_profile(q):
|
||||||
users = []
|
|
||||||
if limited_emails:
|
|
||||||
# 'nickname__icontains' for search by nickname
|
# 'nickname__icontains' for search by nickname
|
||||||
# 'contact_email__icontains' for search by contact email
|
# 'contact_email__icontains' for search by contact email
|
||||||
users = Profile.objects.filter(Q(user__in=limited_emails) &
|
users = Profile.objects.filter(Q(nickname__icontains=q) | \
|
||||||
(Q(nickname__icontains=q) | Q(contact_email__icontains=q))).values('user')
|
Q(contact_email__icontains=q)).values('user')
|
||||||
else:
|
|
||||||
users = Profile.objects.filter(
|
|
||||||
Q(nickname__icontains=q) | Q(contact_email__icontains=q)).values('user')
|
|
||||||
|
|
||||||
email_list = []
|
email_list = []
|
||||||
for user in users:
|
for user in users:
|
||||||
@@ -198,23 +188,44 @@ def search_user_from_profile(q, limited_emails=[]):
|
|||||||
|
|
||||||
return email_list
|
return email_list
|
||||||
|
|
||||||
def search_user_from_contact(request, q):
|
def search_user_from_profile_with_limits(q, limited_emails):
|
||||||
|
# search within limited_emails
|
||||||
|
users = Profile.objects.filter(Q(user__in=limited_emails) &
|
||||||
|
(Q(nickname__icontains=q) | Q(contact_email__icontains=q))).values('user')
|
||||||
|
|
||||||
# get user's contact list
|
|
||||||
username = request.user.username
|
|
||||||
contacts = Contact.objects.get_contacts_by_user(username)
|
|
||||||
|
|
||||||
# search user from contact list
|
|
||||||
email_list = []
|
email_list = []
|
||||||
|
for user in users:
|
||||||
|
email_list.append(user['user'])
|
||||||
|
|
||||||
|
return email_list
|
||||||
|
|
||||||
|
def search_user_when_global_address_book_disabled(request, q):
|
||||||
|
|
||||||
|
email_list = []
|
||||||
|
username = request.user.username
|
||||||
|
|
||||||
|
# search from contact
|
||||||
|
# get user's contact list
|
||||||
|
contacts = Contact.objects.get_contacts_by_user(username)
|
||||||
for contact in contacts:
|
for contact in contacts:
|
||||||
|
# search user from contact list
|
||||||
if q in contact.contact_email:
|
if q in contact.contact_email:
|
||||||
email_list.append(contact.contact_email)
|
email_list.append(contact.contact_email)
|
||||||
|
|
||||||
# search from profile, limit search range in user contacts
|
# search from profile, limit search range in user's contacts
|
||||||
limited_emails = []
|
limited_emails = []
|
||||||
for contact in contacts:
|
for contact in contacts:
|
||||||
limited_emails.append(contact.contact_email)
|
limited_emails.append(contact.contact_email)
|
||||||
|
|
||||||
email_list += search_user_from_profile(q, limited_emails)
|
email_list += search_user_from_profile_with_limits(q, limited_emails)
|
||||||
|
|
||||||
|
if is_valid_email(q):
|
||||||
|
# if `q` is a valid email
|
||||||
|
email_list.append(q)
|
||||||
|
|
||||||
|
# get user whose `contact_email` is `q`
|
||||||
|
users = Profile.objects.filter(contact_email=q).values('user')
|
||||||
|
for user in users:
|
||||||
|
email_list.append(user['user'])
|
||||||
|
|
||||||
return email_list
|
return email_list
|
||||||
|
@@ -9,6 +9,7 @@ from seahub.profile.models import Profile
|
|||||||
from seahub.profile.utils import refresh_cache
|
from seahub.profile.utils import refresh_cache
|
||||||
from seahub.api2.endpoints.search_user import SearchUser
|
from seahub.api2.endpoints.search_user import SearchUser
|
||||||
from seahub.test_utils import BaseTestCase
|
from seahub.test_utils import BaseTestCase
|
||||||
|
from tests.common.utils import randstring
|
||||||
|
|
||||||
class SearchUserTest(BaseTestCase):
|
class SearchUserTest(BaseTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -157,29 +158,33 @@ class SearchUserTest(BaseTestCase):
|
|||||||
assert json_resp['users'][0]['email'] == self.admin.username
|
assert json_resp['users'][0]['email'] == self.admin.username
|
||||||
|
|
||||||
@patch.object(SearchUser, '_can_use_global_address_book')
|
@patch.object(SearchUser, '_can_use_global_address_book')
|
||||||
def test_search_with_not_use_global_address_book(self, mock_can_use_global_address_book):
|
def test_search_when_not_use_global_address_book(self, mock_can_use_global_address_book):
|
||||||
|
|
||||||
mock_can_use_global_address_book.return_value = False
|
mock_can_use_global_address_book.return_value = False
|
||||||
|
|
||||||
resp = self.client.get(self.endpoint + '?q=%s' % self.admin.username)
|
contact_email = '%s@%s.com' % (randstring(6), randstring(6))
|
||||||
json_resp = json.loads(resp.content)
|
|
||||||
|
|
||||||
|
p = Profile.objects.add_or_update(self.admin.username, nickname='')
|
||||||
|
p.contact_email = contact_email
|
||||||
|
p.save()
|
||||||
|
|
||||||
|
# search with valid email
|
||||||
|
resp = self.client.get(self.endpoint + '?q=%s' % contact_email)
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
self.assertEqual(200, resp.status_code)
|
self.assertEqual(200, resp.status_code)
|
||||||
assert json_resp['users'][0]['email'] == self.admin.username
|
assert json_resp['users'][0]['email'] == self.admin.username
|
||||||
|
|
||||||
@patch.object(SearchUser, '_can_use_global_address_book')
|
# search with invalid email & has no contacts
|
||||||
def test_search_by_contact_with_not_use_global_address_book(self, mock_can_use_global_address_book):
|
resp = self.client.get(self.endpoint + '?q=%s' % contact_email[:6])
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
mock_can_use_global_address_book.return_value = False
|
self.assertEqual(200, resp.status_code)
|
||||||
|
assert len(json_resp['users']) == 0
|
||||||
|
|
||||||
|
# search with invalid email & has contact
|
||||||
|
nickname_of_admin = randstring(6)
|
||||||
Contact.objects.add_contact(self.user.username, self.admin.username)
|
Contact.objects.add_contact(self.user.username, self.admin.username)
|
||||||
|
Profile.objects.add_or_update(self.admin.username, nickname=nickname_of_admin)
|
||||||
nickname_of_admin = 'nickname of admin'
|
|
||||||
Profile.objects.add_or_update(self.admin.username,
|
|
||||||
nickname=nickname_of_admin)
|
|
||||||
|
|
||||||
resp = self.client.get(self.endpoint + '?q=%s' % nickname_of_admin)
|
resp = self.client.get(self.endpoint + '?q=%s' % nickname_of_admin)
|
||||||
json_resp = json.loads(resp.content)
|
json_resp = json.loads(resp.content)
|
||||||
|
|
||||||
self.assertEqual(200, resp.status_code)
|
self.assertEqual(200, resp.status_code)
|
||||||
assert json_resp['users'][0]['email'] == self.admin.username
|
assert json_resp['users'][0]['email'] == self.admin.username
|
||||||
|
Reference in New Issue
Block a user