Commit 2ea3ad4c authored by BaiJiangJie's avatar BaiJiangJie

[Update] 重构 LDAP/AD 同步功能,添加缓存机制 2

parent 596e5a6d
......@@ -137,13 +137,45 @@ class LDAPUserListApi(generics.ListAPIView):
users = self.get_queryset_from_server()
return users
@staticmethod
def processing_queryset(queryset):
db_username_list = User.objects.all().values_list('username', flat=True)
for q in queryset:
q['id'] = q['username']
q['existing'] = q['username'] in db_username_list
return queryset
def sort_queryset(self, queryset):
order_by = self.request.query_params.get('order')
if not order_by:
order_by = 'existing'
if order_by.startswith('-'):
order_by = order_by.lstrip('-')
reverse = True
else:
reverse = False
queryset = sorted(queryset, key=lambda x: x[order_by], reverse=reverse)
return queryset
def filter_queryset(self, queryset):
if queryset is None:
return queryset
queryset = self.processing_queryset(queryset)
queryset = self.sort_queryset(queryset)
return queryset
def list(self, request, *args, **kwargs):
cache_police = self.request.query_params.get('cache_police', True)
# 不是用缓存
if cache_police not in LDAP_USE_CACHE_FLAGS:
return super().list(request, *args, **kwargs)
queryset = self.get_queryset()
try:
queryset = self.get_queryset()
except Exception as e:
data = {'error': str(e)}
return Response(data=data, status=400)
# 缓存有数据
if queryset is not None:
return super().list(request, *args, **kwargs)
......@@ -161,36 +193,11 @@ class LDAPUserListApi(generics.ListAPIView):
# 同步任务执行结束
if sync_util.task_is_over:
msg = sync_util.get_task_error_msg()
data = {'msg': 'Synchronization task report error: {}'.format(msg)}
data = {'error': 'Synchronization task report error: {}'.format(msg)}
return Response(data=data, status=400)
return super().list(request, *args, **kwargs)
@staticmethod
def processing_queryset(queryset):
db_username_list = User.objects.all().values_list('username', flat=True)
for q in queryset:
q['id'] = q['username']
q['existing'] = q['username'] in db_username_list
return queryset
def sort_queryset(self, queryset):
order_by = self.request.query_params.get('order')
if not order_by:
order_by = 'existing'
if order_by.startswith('-'):
order_by = order_by.lstrip('-')
reverse = True
else:
reverse = False
queryset = sorted(queryset, key=lambda x: x[order_by], reverse=reverse)
return queryset
def filter_queryset(self, queryset):
queryset = self.processing_queryset(queryset)
queryset = self.sort_queryset(queryset)
return queryset
class LDAPUserImportAPI(APIView):
permission_classes = (IsOrgAdmin,)
......@@ -205,11 +212,20 @@ class LDAPUserImportAPI(APIView):
return users
def post(self, request):
users = self.get_ldap_users()
try:
users = self.get_ldap_users()
except Exception as e:
return Response({'error': str(e)}, status=401)
if users is None:
return Response({'msg': 'Get ldap users is None'}, status=401)
errors = LDAPImportUtil().perform_import(users)
if errors:
return Response({'Error': errors}, status=401)
return Response({'msg': 'Imported {} users successfully'.format(len(users))})
return Response({'errors': errors}, status=401)
count = users if users is None else len(users)
return Response({'msg': 'Imported {} users successfully'.format(count)})
class LDAPCacheRefreshAPI(generics.RetrieveAPIView):
......
......@@ -37,6 +37,9 @@
<tbody>
</tbody>
</table>
<div id="fake_datatable_wrapper_loading" class="dataTables_wrapper" style="display: block;">
<div id="ldap_list_users_table_processing" class="dataTables_processing panel panel-default">{% trans 'Loading' %}...</div>
</div>
</div>
</div>
</div>
......@@ -48,7 +51,7 @@ var interval;
function initLdapUsersTable() {
if(ldap_users_table){
ldap_users_table.ajax.reload();
ldap_users_table.ajax.reload(null, false);
return ldap_users_table
}
var options = {
......@@ -77,6 +80,7 @@ function initLdapUsersTable() {
}
function testRequestLdapUser(){
$("#fake_datatable_wrapper_loading").css('display', 'block');
var the_url = "{% url 'api-settings:ldap-user-list' %}";
var error = function (data, status) {
if (status === 409){
......@@ -92,6 +96,7 @@ function testRequestLdapUser(){
console.log(data, status)
};
var success = function() {
$("#fake_datatable_wrapper_loading").css('display', 'none');
initLdapUsersTable();
clearInterval(interval);
interval = undefined
......@@ -145,6 +150,7 @@ $(document).ready(function(){
}
function success(message) {
toastr.success(message.msg);
ldap_users_table.selected = [];
timingTestRequestLdapUser();
}
requestApi({
......
......@@ -187,7 +187,8 @@ class LDAPCacheUtil(object):
def get_users(self):
users = cache.get(self.CACHE_KEY_USERS)
logger.info('Get ldap users from cache, count: {}'.format(len(users)))
count = users if users is None else len(users)
logger.info('Get ldap users from cache, count: {}'.format(count))
return users
def delete_users(self):
......@@ -195,6 +196,8 @@ class LDAPCacheUtil(object):
cache.delete(self.CACHE_KEY_USERS)
def filter_users(self, users):
if users is None:
return users
if self.search_users:
filter_users = [
user for user in users
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment