Example #1
0
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()
Example #2
0
def saveServiceAjax(request):
    '''
    save manager's service setting
    input: request
    return: json
    author: Arthur
    '''
    #Server Side Rule Check
    username = request.user.username
    postdata = getPostdata(request)

    if username:
        require_field = ['content']
        checker = DataChecker(postdata, require_field)

        if checker.get('status') == 'success':

            #是否存入翻譯
            lan_package = postdata.get('lan_package', None)
            if lan_package:
                FlowActiveGlobalObject.setSysLanDict("service", None,
                                                     lan_package)

            #static variable
            box_object = json.loads(postdata.get('content', ''))
            OmServiceDesign.objects.create(
                content=json.dumps(box_object),
                language_package=json.dumps(
                    FlowActiveGlobalObject.getSysLanDict('service')))

            OmService.objects.all().delete()

            ServiceList = []
            for key in box_object["list"]:
                thisSer = box_object["list"][key]
                if thisSer["type"] == "service":
                    flow_uuid = FlowActiveGlobalObject.APIgetUUID(
                        thisSer['api_path'])
                    ser = OmService(service_id=thisSer['id'],
                                    flow_uuid=flow_uuid,
                                    role=thisSer['setting']['setRole'],
                                    default_value=json.dumps(
                                        thisSer['default_value']))
                    ServiceList.append(ser)

            OmService.objects.bulk_create(ServiceList)

            info('%s update Service success' % username, request)
            return ResponseAjax(statusEnum.success, _('儲存成功。')).returnJSON()
        else:
            info('%s update Service error' % username, request)
            return ResponseAjax(statusEnum.not_found, checker.get('message'),
                                checker).returnJSON()
    else:
        info('%s update Service with no permission' % username, request)
        return ResponseAjax(statusEnum.no_permission,
                            _('您沒有權限進行此操作。')).returnJSON()
Example #3
0
def requestAjax(request):
    '''
    send user's service request
    return: json
    author: Arthur
    '''
    username = request.user.username
    postdata = request.POST.copy()
    files = request.FILES.getlist('files', '')
    result = {}
    #print(postdata)
    if username:
        require_field = ['api_path', 'service_id', 'formdata']
        checker = DataChecker(postdata, require_field)
        if checker.get('status') == 'success':
            #判斷權限
            postdata['flow_uuid'] = FlowActiveGlobalObject.APIgetUUID(
                postdata.get('api_path'))
            thisQuery = list(
                OmService.objects.filter(
                    service_id=postdata.get('service_id')))
            if len(thisQuery):
                service_obj = thisQuery[0]
                #print(service_obj.default_value)
                service_obj = json.loads(service_obj.default_value)

                postdata["formdata"] = json.loads(postdata["formdata"])
                for default_col in service_obj:
                    postdata["formdata"].append(default_col)

                postdata["formdata"] = json.dumps(postdata["formdata"])
            else:
                info('%s update Service error' % username, request)
                return ResponseAjax(statusEnum.error, _('查無此服務。')).returnJSON()

            result = createOmData(postdata, request.user.username, files)
            if result['status']:
                return ResponseAjax(statusEnum.success,
                                    result['message']).returnJSON()
            else:
                return ResponseAjax(statusEnum.not_found,
                                    result['message']).returnJSON()
        else:
            return ResponseAjax(statusEnum.not_found,
                                _('缺少必填欄位。')).returnJSON()
    else:
        error('%s request_service with no permission' % username, request)
        return ResponseAjax(statusEnum.no_permission,
                            _('您沒有權限進行此操作。')).returnJSON()
Example #4
0
def loadServiceAjax(request):
    '''
    load manager's service setting
    return: json
    author: Arthur
    '''

    #Server Side Rule Check
    username = request.user.username
    postdata = getPostdata(request)
    result = {}
    if username:
        #static variable
        #         result['structure'] = OmServiceDesign.objects.all()
        #         if request.user.has_perm('omservice.OmServiceDesign_Manager'):
        #             result['objects'] = OmService.objects.all()
        #         else:
        #             #等待修改
        #             user_role_list = list(request.user.groups.all().values_list('id', flat=True))
        #             for service_obj in OmService.objects.all():
        #                 service_role_list = json.loads(service_obj.get('role','[]'))
        #                 if len(set(user_role_list) & set(service_role_list)):
        #                     result['objects'].append(service_obj)
        #print(OmServiceDesign.objects.all().count())
        if OmServiceDesign.objects.all().count() > 0:
            max = OmServiceDesign.objects.all().aggregate(
                Max('id')).get('id__max')
            result = list(
                OmServiceDesign.objects.filter(id=max).values('content'))

            box_object = json.loads(result[0]["content"])

            #檢查權限
            if request.user.has_perm('omservice.OmServiceDesign_Manage'):
                pass
            else:
                Service_List = list(OmService.objects.all())
                Group_List = list(request.user.groups.all().values_list(
                    'id', flat=True))
                if len(Group_List):
                    Group_List.append(-1)
                else:
                    Group_List = [-1]
                for thisSer in Service_List:
                    if len(set(json.loads(thisSer.role))
                           & set(Group_List)) == 0:
                        del box_object["list"][str(thisSer.service_id)]

            #檢查是否過期
            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]
            for idx in box_object["list"]:
                #print(box_object["list"][idx])
                if box_object["list"][idx]["type"] == "service":
                    if 'api_path' in box_object["list"][idx]:
                        flow_uuid = FlowActiveGlobalObject.APIgetUUID(
                            box_object["list"][idx]["api_path"])
                    else:
                        flow_uuid = box_object["list"][idx]["flow_uuid"]
                        box_object["list"][idx][
                            "api_path"] = FlowActiveGlobalObject.UUIDgetAPI(
                                flow_uuid)

                    if flow_uuid in this_activeList:
                        box_object["list"][idx]["available"] = 1
                    else:
                        box_object["list"][idx]["available"] = 0

            #翻譯語言
            if postdata.get('type') != "source":
                language_type = get_language()
                #print(FlowActiveGlobalObject.getSysLanDict('service'))
                lan_package = FlowActiveGlobalObject.getSysLanDict(
                    'service').get(language_type, None)

                if lan_package:
                    for idx in box_object["list"]:
                        this_service = box_object["list"][idx]
                        if this_service["setting"]["setTitle"]:
                            this_service["setting"]["setTitle"] = lan_package[
                                this_service["setting"]
                                ["setTitle"]] if checkJsonKey(
                                    lan_package,
                                    this_service["setting"]["setTitle"]
                                ) else this_service["setting"]["setTitle"]

                        if this_service["type"] == "service":
                            if this_service["setting"]["setTip"]:
                                this_service["setting"]["setTip"] = lan_package[
                                    this_service["setting"]
                                    ["setTip"]] if checkJsonKey(
                                        lan_package,
                                        this_service["setting"]["setTip"]
                                    ) else this_service["setting"]["setTip"]
                            for sup_idx in this_service["service"]["list"]:
                                this_step = this_service["service"]["list"][
                                    sup_idx]
                                if this_step["name"]:
                                    this_step["name"] = lan_package[
                                        this_step["name"]] if checkJsonKey(
                                            lan_package, this_step["name"]
                                        ) else this_step["name"]
                                if this_step["tip"]:
                                    this_step["tip"] = lan_package[
                                        this_step["tip"]] if checkJsonKey(
                                            lan_package, this_step["tip"]
                                        ) else this_step["tip"]

            #print(result[0]["content"])
            result[0]["content"] = json.dumps(box_object)

        return ResponseAjax(statusEnum.success, _('讀取成功。'),
                            result).returnJSON()
    else:
        error('%s load Service with no permission' % username, request)
        return ResponseAjax(statusEnum.no_permission,
                            _('您沒有權限進行此操作。')).returnJSON()