def getFormObjAjax(request): ''' load service_object's form_object return: json author: Arthur ''' username = request.user.username postdata = getPostdata(request) result = {} if username: require_field = ['api_path'] checker = DataChecker(postdata, require_field) if checker.get('status') == 'success': flow_uuid = FlowActiveGlobalObject.APIgetUUID( postdata.get('api_path')) thisQuery = list( FlowActive.objects.filter(flow_uuid=flow_uuid, undeploy_flag=False).values( 'formobject', 'attachment')) if len(thisQuery): result = thisQuery[0] language = get_language() app_id = FlowActiveGlobalObject.UUIDSearch( flow_uuid).flow_app_id formobject = Translator('formobject', 'active', language, app_id, None).Do(result['formobject']) result['formobject'] = json.dumps(formobject) return ResponseAjax(statusEnum.success, _('讀取成功。'), result).returnJSON() else: error('%s get form_object with no permission' % username, request) return ResponseAjax(statusEnum.no_permission, _('您沒有權限進行此操作。')).returnJSON()
def getFormListAjax(request): ''' load form_list return: json author: Arthur ''' #Server Side Rule Check username = request.user.username result = {} if username: #static variable #omdata_list = list(filter(lambda x: re.match("^Omdata_.{32}$", x.__name__), apps.get_models())) app = list( ActiveApplication.objects.filterformat('id', 'app_name', undeploy_flag=False)) flow = list( FlowActive.objects.filterformat('id', 'flow_uuid', 'api_path', 'flow_name', 'flow_app_id', undeploy_flag=False, parent_uuid=None)) result = {'app': app, 'flow': flow} #翻譯 lang = get_language() result['app'] = Translator('datatable_multi_app', 'active', lang, None, None).Do(result['app']) result['flow'] = Translator('datatable_multi_app', 'active', lang, None, None).Do(result['flow']) return ResponseAjax(statusEnum.success, _('讀取成功。'), result).returnJSON() else: error('%s load form_list with no permission' % username, request) return ResponseAjax(statusEnum.no_permission, _('您沒有權限進行此操作。')).returnJSON()
def listHistoryMissionAjax(request): ''' list my groups missions. input: request return: json author: Kolin Hsu ''' #get post data postdata = getPostdata(request) updatetime_str = postdata.get('updatetime', '') updatetime = updatetime_str.split(',') group_id = postdata.get('group_id', '') field_list = [ 'title__icontains', 'flow_name__icontains', 'status__icontains', 'create_user__nick_name__icontains' ] display_field = [ 'flow_name', 'flow_uuid', 'title', 'create_user_id__nick_name', 'data_id', 'data_no', 'updatetime', 'assign_group_id', 'assign_group_id__display_name', 'assignee_id__nick_name', 'attachment' ] if group_id: username_list = list( OmUser.objects.filter(groups__id=group_id, delete=False).values_list('username', flat=True)) query = Missions.objects.filter( Q(update_user_id__in=username_list) & Q(updatetime__range=updatetime) & Q(history=True) & Q(is_active=True) & Q(deploy_flag=True)).values(*display_field) else: query = Missions.objects.filter( Q(update_user_id=request.user.username) & Q(updatetime__range=updatetime) & Q(history=True) & Q(is_active=True) & Q(deploy_flag=True)).values(*display_field) result = DatatableBuilder(request, query, field_list) #載入語言包 language = get_language() result['data'] = Translator('datatable_multi_app', 'active', language, None, None).Do(result['data']) info('%s list MissionHistory success.' % request.user.username, request) return JsonResponse(result)
def getColumnListAjax(request): ''' get model column-list with permission input: request return: json author: Arthur ''' username = request.user.username postdata = getPostdata(request) if username: require_field = ['model'] checker = DataChecker(postdata, require_field) if checker.get('status') == 'success': permission = "Omdata_" + postdata.get('model', '') + "_View" this_model = list( filter( lambda x: x.__name__ == re.findall("^(.+)_View", permission)[0], apps.get_models())) result = {} #isPolicy = False from omflow.syscom.common import License version_type = License().getVersionType() if version_type != 'C': from ommonitor.models import MonitorFlow if len(this_model) == 0 and request.user.has_perm( "OmMonitor_Manage"): policy_model = list( filter( lambda x: x.__name__ == postdata.get('model', ''), apps.get_models())) if len(policy_model): this_table_id = re.findall("OmPolicy_(.+)", postdata.get('model', ''))[0] fa = list( MonitorFlow.objects.filter( table_id=this_table_id).values('flowobject')) if len(fa): fa = json.loads(fa[0]['flowobject']) result["collector_id"] = "收集器ID" for item in fa['items']: if item['type'] == "end": for output in item['config']['output']: field_name = re.findall( '\$\((.+)\)', output['name'])[0] result[field_name] = output['des'] if len(this_model) and request.user.has_perm( str(this_model[0]._meta.app_label) + "." + permission): if re.match('Omdata_.+_View', permission): flow_uuid = re.findall("Omdata_(.+)_View", permission)[ 0] #Omdata_5d3c2cbdcf4145c3a05affaf4a74345b_View fa = FlowActiveGlobalObject.UUIDSearch( flow_uuid).merge_formobject if len(fa): fa = json.loads(fa) for field in this_model[0]._meta.fields: if 'formitm' in field.verbose_name: if field.name.upper() in fa: if fa[field.name.upper( )]["type"] == 'h_group': #result[field.name] = fa[field.name.upper] result[ field.name] = fa[field.name.upper( )]["config"]["group_title"] else: result[ field.name] = fa[field.name.upper( )]["config"]["title"] else: result[field.name] = field.verbose_name lang = get_language() app_id = FlowActiveGlobalObject.UUIDSearch( flow_uuid).flow_app_id result = Translator('single_app', 'active', lang, app_id, None).Do(result) if result != {}: info('%s get Column-List success' % username, request) return ResponseAjax(statusEnum.success, _('讀取成功。'), result).returnJSON() else: info('%s get Column-List with no permission' % username, request) return ResponseAjax(statusEnum.no_permission, _('您沒有權限進行此操作。')).returnJSON() else: info('%s get Column-List error' % username, request) return ResponseAjax(statusEnum.not_found, checker.get('message'), checker).returnJSON() else: info('%s get Column-List with no permission' % username, request) return ResponseAjax(statusEnum.no_permission, _('您沒有權限進行此操作。')).returnJSON()
def getFormListAjax(request): ''' get user model-list with permission input: request return: json author: Arthur ''' username = request.user.username if username: #已上架流程 this_activeList = list( FlowActive.objects.filter(undeploy_flag=0, parent_uuid__isnull=True).values_list( 'flow_uuid', flat=True)) this_activeList = [o.hex for o in this_activeList] #有權限流程清單 per_list = [] if request.user.is_superuser: p = list( Permission.objects.filter(codename__contains="_View").values()) else: p = list( Permission.objects.filter(group__in=request.user.groups.all(), codename__contains="_View").values()) result = {} for i in p: this_model = list( filter( lambda x: x.__name__ == re.findall("^(Om.+)_View", i.get("codename"))[0], apps.get_models())) if len(this_model): this_name = this_model[0].__name__ this_flow_uuid = re.findall("^Omdata_(.+)", this_name) if len(this_flow_uuid): if this_flow_uuid[0] in this_activeList: per_list.append(this_flow_uuid[0]) #上線流程 flow_list = list( FlowActive.objects.filterformat('id', 'flow_uuid', 'flow_name', 'flow_app_id', undeploy_flag=False, parent_uuid=None)) app_list = list( ActiveApplication.objects.filterformat('id', 'app_name', undeploy_flag=False)) #merge flow = [x for x in flow_list if x['flow_uuid'] in per_list] flow_app = [x['flow_app_id'] for x in flow_list] app = [x for x in app_list if x['id'] in flow_app] result = {'app': app, 'flow': flow} #翻譯 lang = get_language() result['app'] = Translator('datatable_multi_app', 'active', lang, None, None).Do(result['app']) result['flow'] = Translator('datatable_multi_app', 'active', lang, None, None).Do(result['flow']) from omflow.syscom.common import License version_type = License().getVersionType() if version_type != 'C': from ommonitor.models import MonitorFlow #判斷有無資料收集權限 if request.user.has_perms("OmMonitor_Manage"): result['app'].append({'id': 'sch', 'app_name': _('收集器流程')}) result['app'].append({'id': 'api', 'app_name': _('收集器API')}) this_monitorList = list( MonitorFlow.objects.filterformat('id', 'table_id', 'flow_name', 'attr', history=0)) for this_monitor in this_monitorList: result['flow'].append({ 'id': this_monitor['id'], 'flow_uuid': 'OmPolicy_' + this_monitor['table_id'], 'flow_name': this_monitor['flow_name'], 'flow_app_id': this_monitor['attr'] }) info('%s get Form-List success' % username, request) return ResponseAjax(statusEnum.success, _('讀取成功'), result).returnJSON() else: info('%s get Form-List with no permission' % username, request) return ResponseAjax(statusEnum.no_permission, _('您沒有權限進行此操作。')).returnJSON()
def SubQueryHeaderAjax(request): ''' list omdata for subquery. input: request return: json author: Kolin Hsu ''' #function variable display_field_list = [ 'id', 'data_no', 'stop_uuid', 'stop_chart_text', 'error', 'error_message', 'closed', 'running' ] #get post data postdata = getPostdata(request) app_name = postdata.get('app_name', '') flow_name = postdata.get('flow_name', '') condition = postdata.get('condition', []) service_id = postdata.get('service_id', []) flowactive = FlowActiveGlobalObject.NameSearch(flow_name, None, app_name) flow_uuid = flowactive.flow_uuid.hex if True: #get field setting display_field_dict = json.loads(flowactive.display_field) for key in display_field_dict: if '-' in key: key = 'group' if key == 'create_user_id' or key == 'create_user': key = 'create_user_id__nick_name' elif key == 'update_user_id' or key == 'update_user': key = 'update_user_id__nick_name' display_field_list.append(key) search_field_list = display_field_list.copy() #暫時先使用display_field當作search_field field_list = list( map(lambda search_field: search_field + '__icontains', search_field_list)) #sp conditions search_conditions = [{ 'column': 'history', 'condition': '=', 'value': False }] exclude_conditions = [] for con in condition: if con['condition'] == '!=': con['condition'] = '=' exclude_conditions.append(con) elif con['condition'] == 'exclude': con['condition'] = 'contains' exclude_conditions.append(con) else: search_conditions.append(con) sc = searchConditionBuilder(search_conditions) ec = searchConditionBuilder(exclude_conditions) #get model omdata_model = getModel('omformmodel', 'Omdata_' + flow_uuid) if ec: query = omdata_model.objects.filter( reduce(operator.and_, sc)).exclude(reduce(operator.and_, ec)).values(*display_field_list) else: query = omdata_model.objects.filter(reduce( operator.and_, sc)).values(*display_field_list) result = DatatableBuilder(request, query, field_list) #轉換字與值 result['data'] = datatableValueToText(result['data'], flow_uuid) #載入語言包 language = get_language() result['data'] = Translator('datatable_single_app', 'active', language, flowactive.flow_app_id, None).Do(result['data']) info(request, '%s list OmData success.' % request.user.username) return JsonResponse(result) else: info('%s has no permission.' % request.user.username, request) return ResponseAjax(statusEnum.no_permission, _('您沒有權限進行此操作。')).returnJSON()
def loadLSideAjax(request): ''' load left side bar design json for request user input: request return: json author: Kolin Hsu ''' #function variable require_field = ['draw_$'] mission = [] sidebar = [] request.session.set_expiry(130) #server side rule check postdata = getPostdata(request) data_checker = DataChecker(postdata, require_field) if data_checker.get('status') == 'success': if settings.OMFLOW_TYPE == 'collector': result = {'sidebar': sidebar, 'mission': mission} else: #get mission group_id_list = list(request.user.groups.all().values_list( 'id', flat=True)) level_list = list( Missions.objects.filter((Q(assignee_id=request.user.id) | (Q( assign_group_id__in=group_id_list) & Q(assignee_id=None))) & Q(history=False) & Q(is_active=True) & Q(deploy_flag=True)).values_list( 'level', flat=True)) green = 0 red = 0 yellow = 0 for i in level_list: if i == 'green': green += 1 elif i == 'yellow': yellow += 1 elif i == 'red': red += 1 elif i == None or i == '': green += 1 mission.append(red) mission.append(yellow) mission.append(green) #get sidebar draw = int(postdata.get('draw_$', '')) sidebar_design = GlobalObject.__sidebarDesignObj__[ 'sidebar_design'] if draw == 1: reviseLside(request, sidebar, sidebar_design) else: #get session object lside_design = request.session.get('lside_design', '') session_updatetime = request.session.get( 'design_updatetime', '') #get global object design_updatetime = GlobalObject.__sidebarDesignObj__[ 'design_updatetime'] permission_updatetime = GlobalObject.__sidebarDesignObj__[ 'permission_updatetime'] #compare updatetime if session_updatetime < str( design_updatetime) or session_updatetime < str( permission_updatetime): #compare two json array equal or not if lside_design != sidebar_design: reviseLside(request, sidebar, sidebar_design) else: sidebar = '' else: sidebar = '' #載入語言包 language = get_language() if isinstance(sidebar, list): trans_sidebar_str = json.dumps(sidebar) trans_sidebar = json.loads(trans_sidebar_str) for s in trans_sidebar: app_id = None sidebar_id = s['id'] if 'app-' in sidebar_id: app_id = sidebar_id[4:] elif 'formflow-' in sidebar_id: app_id = s['p_id'][4:] if app_id: s['name'] = Translator('single_app', 'active', language, app_id, None).Do(s['name']) else: trans_sidebar = sidebar result = {'sidebar': trans_sidebar, 'mission': mission} info('%s load LSide success.' % request.user.username, request) return ResponseAjax(statusEnum.success, _('讀取成功。'), result).returnJSON() else: info( '%s missing some require variable or the variable type error.' % request.user.username, request) return ResponseAjax(statusEnum.not_found, data_checker.get('message'), data_checker).returnJSON()
def listHistoryMissionCurrentStateAjax(request): ''' list my groups missions. input: request return: json author: Kolin Hsu ''' #get post data postdata = getPostdata(request) ticket_createtime = postdata.get('ticket_createtime', '') ticket_createtime = ticket_createtime.split(',') group_id = postdata.get('group_id', '') field_list = [ 'title__icontains', 'flow_name__icontains', 'status__icontains', 'create_user__nick_name__icontains' ] display_field = [ 'flow_name', 'flow_uuid', 'level', 'status', 'title', 'ticket_createtime', 'data_id', 'data_no', 'assign_group_id', 'assign_group_id__display_name', 'assignee_id__nick_name', 'closed', 'stop_uuid', 'stop_chart_text' ] #取得我(群組)曾經處理過的任務 if group_id: username_list = list( OmUser.objects.filter(groups__id=group_id, delete=False).values_list('username', flat=True)) mission_list = list( Missions.objects.filter( Q(update_user_id__in=username_list) & Q(ticket_createtime__range=ticket_createtime) & Q(history=True) & Q(closed=False) & Q(is_active=True) & Q(deploy_flag=True)).values('flow_uuid', 'data_no')) else: mission_list = list( Missions.objects.filter( Q(update_user_id=request.user.username) & Q(ticket_createtime__range=ticket_createtime) & Q(history=True) & Q(closed=False) & Q(is_active=True) & Q(deploy_flag=True)).values('flow_uuid', 'data_no')) #將查詢結果分為兩個list 建立對照的dict--(以flow_uuid為KEY,該流程的單號組成list為VALUE) flow_uuid_list = [] data_no_list = [] mapping_dict = {} for i in mission_list: mapping_data_no_list = mapping_dict.get(i['flow_uuid'], []) mapping_data_no_list.append(i['data_no']) if len(mapping_data_no_list) == 1: mapping_dict[i['flow_uuid']] = mapping_data_no_list if i['flow_uuid'] not in flow_uuid_list: flow_uuid_list.append(i['flow_uuid']) if i['data_no'] not in data_no_list: data_no_list.append(i['data_no']) try: #將所有flow_uuid_list、data_no_list組合的max id查出來 max_mission_list = list( Missions.objects.filter(flow_uuid__in=flow_uuid_list, data_no__in=data_no_list).values( 'flow_uuid', 'data_no').annotate(max_id=Max('id'))) toomany = False except: data_no_list = data_no_list[:900] max_mission_list = list( Missions.objects.filter(flow_uuid__in=flow_uuid_list, data_no__in=data_no_list).values( 'flow_uuid', 'data_no').annotate(max_id=Max('id'))) toomany = True #透過對照dict找出真正該撈出來的max id max_id_list = [] for m in max_mission_list: mapping_no_list = mapping_dict.get(m['flow_uuid'], None) if m['data_no'] in mapping_no_list: max_id_list.append(m['max_id']) query = Missions.objects.filter(id__in=max_id_list).values(*display_field) result = DatatableBuilder(request, query, field_list) result['toomany'] = toomany #載入語言包 language = get_language() result['data'] = Translator('datatable_multi_app', 'active', language, None, None).Do(result['data']) info('%s list MissionCurrentState success.' % request.user.username, request) return JsonResponse(result)
def listMyMissionAjax(request): ''' list my missions. input: request return: json author: Kolin Hsu ''' #get post data postdata = getPostdata(request) omflow_restapi = postdata.get('omflow_restapi', '') if not omflow_restapi: ticket_createtime = postdata.get('ticket_createtime', '') ticket_createtime = ticket_createtime.split(',') field_list = [ 'title__icontains', 'flow_name__icontains', 'status__icontains', 'create_user_id__nick_name__icontains' ] display_field = [ 'flow_name', 'flow_uuid', 'level', 'status', 'title', 'create_user_id__nick_name', 'assign_group_id__display_name', 'assignee_id__nick_name', 'ticket_createtime', 'data_id', 'data_no', 'action', 'attachment' ] group_id_list = list(request.user.groups.all().values_list('id', flat=True)) query = Missions.objects.filter( (Q(assignee_id=request.user.id) | (Q(assign_group_id__in=group_id_list) & Q(assignee_id=None))) & Q(history=False) & Q(ticket_createtime__range=ticket_createtime) & Q(is_active=True) & Q(deploy_flag=True)).values(*display_field) result = DatatableBuilder(request, query, field_list) #載入語言包 language = get_language() data = result['data'] if isinstance(data, str): pass elif isinstance(data, list) or isinstance(data, dict): for key_or_line in data: if isinstance(data, list): line = key_or_line else: line = data[key_or_line] fa = FlowActiveGlobalObject.UUIDSearch(line['flow_uuid']) if fa: app_id = fa.flow_app_id for key in line: if key in ['flow_name', 'level', 'status']: line[key] = Translator('single_app', 'active', language, app_id, None).Do(line[key]) elif key == 'action': action = line[key] if action: action1 = action.split(',')[0] action2 = action.split(',')[1] trans_a1 = Translator('single_app', 'active', language, app_id, None).Do(action1) trans_a2 = Translator('single_app', 'active', language, app_id, None).Do(action2) line[key] = trans_a1 + ',' + trans_a2 # result['data'] = Translator('datatable_multi_app', 'active', language, None, None).Do(result['data']) info('%s list Mission success.' % request.user.username, request) return JsonResponse(result) else: #api使用 group_id_list = list(request.user.groups.all().values_list('id', flat=True)) query = Missions.objects.filter((Q(assignee_id=request.user.id) | ( Q(assign_group_id__in=group_id_list) & Q(assignee_id=None))) & Q(history=False) & Q(is_active=True) & Q(deploy_flag=True)) result = listQueryBuilder(None, postdata, query) if result['status']: info('%s list Mission success.' % request.user.username, request) return ResponseAjax(statusEnum.success, result['message'], result['result']).returnJSON() else: info('%s list Mission error.' % request.user.username, request) return ResponseAjax(statusEnum.not_found, result['message']).returnJSON()