コード例 #1
0
def newFlow(conductID):
    conductObj = conduct._conduct().getAsClass(api.g["sessionData"],
                                               id=conductID)
    if len(conductObj) == 1:
        conductObj = conductObj[0]
    else:
        return {}, 404

    access, accessIDs, adminBypass = db.ACLAccess(api.g["sessionData"],
                                                  conductObj.acl, "write")
    if access:
        data = json.loads(api.request.data)
        # Get new UUID store within current conduct flow and return UUID
        newFlowID = str(uuid.uuid4())
        flow = {"flowID": newFlowID, "next": []}
        # Creating new object of model type
        _class = model._model().getAsClass(api.g["sessionData"],
                                           id=data["classID"])
        if _class:
            subtype = _class[0].name
            _class = _class[0].classObject()
            newFlowObjectID = _class().new(flow["flowID"]).inserted_id
            # Working out by bruteforce which type this is ( try and load it by parent class and check for error) - get on trigger if it does not exist will return None
            modelFlowObjectType = "action"
            if len(trigger._trigger().getAsClass(api.g["sessionData"],
                                                 id=newFlowObjectID)) > 0:
                modelFlowObjectType = "trigger"
            modelFlowObject = _class().getAsClass(api.g["sessionData"],
                                                  id=newFlowObjectID)
            if len(modelFlowObject) == 1:
                modelFlowObject = modelFlowObject[0]
            else:
                return {}, 404
            modelFlowObject.acl = {
                "ids": [{
                    "accessID": api.g["sessionData"]["primaryGroup"],
                    "read": True,
                    "write": True,
                    "delete": True
                }]
            }
            modelFlowObject.update(["acl"], sessionData=api.g["sessionData"])
            flow["type"] = modelFlowObjectType
            if subtype != "action" and subtype != "trigger":
                flow["subtype"] = subtype
            flow["{0}{1}".format(modelFlowObjectType,
                                 "ID")] = str(newFlowObjectID)
            # Adding UI position for cloned object
            webui._modelUI().new(conductID, conductObj.acl, flow["flowID"],
                                 data["x"], data["y"], modelFlowObject.name)
            # Appending new object to conduct
            conductObj.flow.append(flow)
            conductObj.update(["flow"], sessionData=api.g["sessionData"])
            return {}, 201
    else:
        return {}, 403

    return {}, 404
コード例 #2
0
    def install(self):
        # Register models
        model.registerModel("choice","_choice","_document","plugins.choice.models.choice",True)
        model.registerModel("choiceRequest","_requestChoice","_action","plugins.choice.models.action")
        model.registerModel("choiceTrigger","_choiceTrigger","_action","plugins.choice.models.action",True)

        c = conduct._conduct().new("choiceCore")
        c = conduct._conduct().getAsClass(id=c.inserted_id)[0]
        t = trigger._trigger().new("choiceCore")
        t = trigger._trigger().getAsClass(id=t.inserted_id)[0]
        a = action._choiceTrigger().new("choiceCore")
        a = action._choiceTrigger().getAsClass(id=a.inserted_id)[0]
       
        c.triggers = [t._id]
        flowTriggerID = str(uuid.uuid4())
        flowActionID = str(uuid.uuid4())
        c.flow = [
            {
                "flowID" : flowTriggerID,
                "type" : "trigger",
                "triggerID" : t._id,
                "next" : [
                    {"flowID": flowActionID, "logic": True }
                ]
            },
            {
                "flowID" : flowActionID,
                "type" : "action",
                "actionID" : a._id,
                "next" : []
            }
        ]
        webui._modelUI().new(c._id,{ "ids":[ { "accessID":"0","delete": True,"read": True,"write": True } ] },flowTriggerID,0,0,"")
        webui._modelUI().new(c._id,{ "ids":[ { "accessID":"0","delete": True,"read": True,"write": True } ] },flowActionID,100,0,"")
        c.acl = { "ids":[ { "accessID":"0","delete": True,"read": True,"write": True } ] }
        c.enabled = True
        c.update(["triggers","flow","enabled","acl"])
        t.acl = { "ids":[ { "accessID":"0","delete": True,"read": True,"write": True } ] }
        t.schedule = "60-90s"
        t.enabled = True
        t.update(["schedule","enabled","acl"])
        a.acl = { "ids":[ { "accessID":"0","delete": True,"read": True,"write": True } ] }
        a.enabled = True
        a.update(["enabled","acl"])
        return True
コード例 #3
0
def dropExistingObject(conductID):
    conductObj = conduct._conduct().getAsClass(api.g["sessionData"],
                                               id=conductID)
    if len(conductObj) == 1:
        conductObj = conductObj[0]
    else:
        return {}, 404
    access, accessIDs, adminBypass = db.ACLAccess(api.g["sessionData"],
                                                  conductObj.acl, "write")
    if access:
        data = json.loads(api.request.data)
        if data["action"] == "drop":
            newFlowID = str(uuid.uuid4())
            flow = {
                "flowID": newFlowID,
                "type": data["flowType"],
                "{0}{1}".format(data["flowType"], "ID"): data["_id"],
                "next": []
            }
            modelFlowObject = None
            if data["flowType"] == "trigger":
                modelFlowObject = trigger._trigger().getAsClass(
                    api.g["sessionData"], id=data["_id"])[0]
            elif data["flowType"] == "action":
                modelFlowObject = action._action().getAsClass(
                    api.g["sessionData"], id=data["_id"])[0]
            if modelFlowObject:
                name = modelFlowObject.name
            else:
                name = flow["flowID"]

            webui._modelUI().new(conductID, conductObj.acl, flow["flowID"],
                                 data["x"], data["y"], name)
            conductObj.flow.append(flow)
            conductObj.update(["flow"], sessionData=api.g["sessionData"])
            return {}, 201
    return {}, 404
コード例 #4
0
def updateFlow(conductID, flowID):
    conductObj = conduct._conduct().getAsClass(api.g["sessionData"],
                                               id=conductID)
    if len(conductObj) == 1:
        conductObj = conductObj[0]
    else:
        return {}, 404

    flow = [x for x in conductObj.flow if x["flowID"] == flowID]
    if len(flow) == 1:
        flow = flow[0]
        data = json.loads(api.request.data)
        if data["action"] == "update":
            access, accessIDs, adminBypass = db.ACLAccess(
                api.g["sessionData"], conductObj.acl, "write")
            if access:
                if "x" in data and "y" in data:
                    try:
                        x = int(data["x"])
                        y = int(data["y"])
                    except:
                        return {}, 403
                flowUI = webui._modelUI().getAsClass(api.g["sessionData"],
                                                     query={
                                                         "flowID":
                                                         flow["flowID"],
                                                         "conductID": conductID
                                                     })
                if len(flowUI) == 1:
                    flowUI = flowUI[0]
                    if "x" in data and "y" in data:
                        flowUI.x = x
                        flowUI.y = y
                        flowUI.update(["x", "y"],
                                      sessionData=api.g["sessionData"])
                    if "title" in data:
                        flowUI.title = data["title"]
                        flowUI.update(["title"],
                                      sessionData=api.g["sessionData"])
                    return {}, 200
                else:
                    webui._modelUI().new(conductID, conductObj.acl,
                                         flow["flowID"], x, y)
                    return {}, 201
        elif data["action"] == "copy":
            access, accessIDs, adminBypass = db.ACLAccess(
                api.g["sessionData"], conductObj.acl, "write")
            if access:
                flow = [
                    x for x in conductObj.flow
                    if x["flowID"] == data["operatorId"]
                ]
                if len(flow) == 1:
                    flow = flow[0]
                    newFlowID = str(uuid.uuid4())
                    newFlow = {
                        "flowID":
                        newFlowID,
                        "type":
                        flow["type"],
                        "{0}{1}".format(flow["type"], "ID"):
                        flow["{0}{1}".format(flow["type"], "ID")],
                        "next": []
                    }
                    flowUI = webui._modelUI().getAsClass(api.g["sessionData"],
                                                         query={
                                                             "flowID":
                                                             flow["flowID"],
                                                             "conductID":
                                                             conductID
                                                         })[0]
                    webui._modelUI().new(conductID, conductObj.acl,
                                         newFlow["flowID"], data["x"],
                                         data["y"], flowUI.title)
                    conductObj.flow.append(newFlow)
                    conductObj.update(["flow"],
                                      sessionData=api.g["sessionData"])
                    return {}, 201
        elif data["action"] == "clone":
            access, accessIDs, adminBypass = db.ACLAccess(
                api.g["sessionData"], conductObj.acl, "write")
            if access:
                flow = [
                    x for x in conductObj.flow
                    if x["flowID"] == data["operatorId"]
                ]
                if len(flow) == 1:
                    flow = flow[0]
                    data = json.loads(api.request.data)
                    modelFlowObject = None
                    # Check if the modelType and object are unchanged
                    if "type" in flow:
                        if flow["type"] == "trigger":
                            modelFlowObject = trigger._trigger().getAsClass(
                                api.g["sessionData"],
                                id=flow["{0}{1}".format(flow["type"], "ID")])
                            if len(modelFlowObject) == 1:
                                modelFlowObject = modelFlowObject[0]
                            modelFlowObjectType = "trigger"
                        if flow["type"] == "action":
                            modelFlowObject = action._action().getAsClass(
                                api.g["sessionData"],
                                id=flow["{0}{1}".format(flow["type"], "ID")])
                            if len(modelFlowObject) == 1:
                                modelFlowObject = modelFlowObject[0]
                            modelFlowObjectType = "action"

                        # Was it possible to load an existing object
                        if modelFlowObject:
                            # Create new flowItem
                            newFlowID = str(uuid.uuid4())
                            flow = {
                                "flowID": newFlowID,
                                "type": flow["type"],
                                "next": []
                            }
                            # New object required
                            _class = model._model().getAsClass(
                                api.g["sessionData"],
                                id=modelFlowObject.classID)
                            if _class:
                                _class = _class[0].classObject()
                                # Bug exists as name value is not requried by db class but is for core models - this could result in an error if new model is added that does not accept name within new function override
                                newFlowObjectID = _class().new(
                                    flow["flowID"]).inserted_id

                                # Working out by bruteforce which type this is ( try and load it by parent class and check for error) - get on trigger if it does not exist will return None
                                modelFlowObjectClone = _class().getAsClass(
                                    api.g["sessionData"], id=newFlowObjectID)
                                if len(modelFlowObjectClone) == 1:
                                    modelFlowObjectClone = modelFlowObjectClone[
                                        0]
                                else:
                                    return {}, 404

                                # Setting values in cloned object
                                members = [
                                    attr for attr in dir(modelFlowObject)
                                    if not callable(
                                        getattr(modelFlowObject, attr))
                                    and not "__" in attr and attr
                                ]
                                dontCopy = ["_id", "name"]
                                updateList = []
                                for member in members:
                                    if member not in dontCopy:
                                        setattr(
                                            modelFlowObjectClone, member,
                                            getattr(modelFlowObject, member))
                                        updateList.append(member)
                                modelFlowObjectClone.update(
                                    updateList,
                                    sessionData=api.g["sessionData"])

                                # Set conduct flow to correct type and objectID
                                flow["{0}{1}".format(
                                    flow["type"], "ID")] = str(newFlowObjectID)
                                conductObj.flow.append(flow)

                                # Adding UI position for cloned object
                                flowUI = webui._modelUI().getAsClass(
                                    api.g["sessionData"],
                                    query={
                                        "flowID": flowID,
                                        "conductID": conductID
                                    })[0]
                                webui._modelUI().new(
                                    conductID, conductObj.acl, flow["flowID"],
                                    data["x"], data["y"],
                                    "Copy - {0}".format(flowUI.title))
                                conductObj.update(
                                    ["flow"], sessionData=api.g["sessionData"])
                                return {"result": True}, 201
    return {}, 404
コード例 #5
0
def conductFlowchartPoll(conductID):
    conductObj = conduct._conduct().query(api.g["sessionData"],
                                          id=conductID)["results"]
    if len(conductObj) == 1:
        conductObj = conductObj[0]
    else:
        return {}, 404
    data = json.loads(api.request.data)

    flowchartOperators = data["operators"]
    flowchartLinks = data["links"]

    flowchartResponse = {
        "operators": {
            "delete": {},
            "create": {},
            "update": {}
        },
        "links": {
            "delete": {},
            "create": {},
            "update": {}
        }
    }

    # Getting all UI flow details for flows in this conduct
    flows = [x for x in conductObj["flow"]]
    flowTriggers = [
        db.ObjectId(x["triggerID"]) for x in flows if x["type"] == "trigger"
    ]
    flowActions = [
        db.ObjectId(x["actionID"]) for x in flows if x["type"] == "action"
    ]
    flowsList = [x["flowID"] for x in flows]
    linksList = []

    # For every refresh the entire flow object and UI is loaded from the database - this may need improvment for speed in future
    flowsUI = webui._modelUI().getAsClass(api.g["sessionData"],
                                          query={
                                              "flowID": {
                                                  "$in": flowsList
                                              },
                                              "conductID": conductID
                                          })
    actions = action._action().getAsClass(api.g["sessionData"],
                                          query={"_id": {
                                              "$in": flowActions
                                          }})
    triggers = trigger._trigger().getAsClass(
        api.g["sessionData"], query={"_id": {
            "$in": flowTriggers
        }})
    cache.globalCache.newCache("modelCache", sessionData=api.g["sessionData"])

    for flow in flows:
        if "type" in flow:
            flowType = flow["type"]
            if "subtype" in flow:
                flowSubtype = flow["subtype"]
            else:
                flowSubtype = ""
            if "{0}{1}".format(flowType, "ID") in flow:
                objectID = "{0}{1}".format(flowType, "ID")
                flowID = flow["flowID"]
                # Default to create
                flowchartResponseType = "create"
                if flowID in flowchartOperators:
                    # If it already exits then its an update
                    flowchartResponseType = "update"
                # Setting position if it has changed since last pollTime
                foundFlowUI = False
                foundObject = False
                name = flow["flowID"]
                node = {}
                for flowUI in flowsUI:
                    if flow["flowID"] == flowUI.flowID:
                        foundFlowUI = True
                        if flowchartResponseType == "create":
                            node["x"] = flowUI.x
                            node["y"] = flowUI.y
                            node["shape"] = "box"
                            node["widthConstraint"] = {
                                "minimum": 125,
                                "maximum": 125
                            }
                            node["heightConstraint"] = {
                                "minimum": 35,
                                "maximum": 35
                            }
                            node["borderWidth"] = 1.5
                            node["font"] = {"multi": True}
                        elif flowUI.x != flowchartOperators[flowID]["node"][
                                "x"] or flowUI.y != flowchartOperators[flowID][
                                    "node"]["y"]:
                            node["x"] = flowUI.x
                            node["y"] = flowUI.y
                        if flow["type"] == "trigger":
                            for t in triggers:
                                if flow["triggerID"] == t._id:
                                    name = t.name
                                    modeClass = cache.globalCache.get(
                                        "modelCache",
                                        t.classID,
                                        model.getClassObject,
                                        sessionData=api.g["sessionData"])[0]
                                    color = None
                                    if t.enabled:
                                        color = "#7cbeeb"
                                    duration = t.maxDuration
                                    if duration == 0:
                                        duration = 60
                                    if ((t.startCheck != 0)
                                            and (t.startCheck + duration >
                                                 time.time())):
                                        color = "green"
                                    if ((t.startCheck != 0)
                                            and (t.startCheck + duration <
                                                 time.time())):
                                        color = "red"
                                    if not t.enabled:
                                        color = "gray"

                                    label = "<b>{0}</b>\n{1}".format(
                                        t.name, modeClass.name)
                                    if flowchartResponseType == "create":
                                        node["label"] = label
                                        node["color"] = {"background": color}
                                    else:
                                        if color != flowchartOperators[flowID][
                                                "node"]["color"]:
                                            node["color"] = {
                                                "background": color
                                            }
                                        if label != flowchartOperators[flowID][
                                                "node"]["label"]:
                                            node["label"] = label
                                    foundObject = True
                                    break
                        elif flow["type"] == "action":
                            for a in actions:
                                if flow["actionID"] == a._id:
                                    name = a.name
                                    modeClass = cache.globalCache.get(
                                        "modelCache",
                                        a.classID,
                                        model.getClassObject,
                                        sessionData=api.g["sessionData"])[0]
                                    color = None
                                    if a.enabled:
                                        color = "#7cbeeb"
                                    if not a.enabled:
                                        color = "gray"

                                    label = "<b>{0}</b>\n{1}".format(
                                        a.name, modeClass.name)
                                    if flowchartResponseType == "create":
                                        node["label"] = label
                                        node["color"] = {"background": color}
                                    else:
                                        if color != flowchartOperators[flowID][
                                                "node"]["color"]:
                                            node["color"] = {
                                                "background": color
                                            }
                                        if label != flowchartOperators[flowID][
                                                "node"]["label"]:
                                            node["label"] = label
                                    foundObject = True
                                    break
                        if node:
                            if not foundObject:
                                node["label"] = "Unknown Object"
                                node["color"] = {"background": "black"}
                            flowchartResponse["operators"][
                                flowchartResponseType][flowID] = {
                                    "_id": flow[objectID],
                                    "flowID": flowID,
                                    "flowType": flowType,
                                    "flowSubtype": flowSubtype,
                                    "name": name,
                                    "node": node
                                }
                        break
                if not foundFlowUI:
                    node["x"] = 0
                    node["y"] = 0
                    node["shape"] = "box"
                    node["widthConstraint"] = {"minimum": 125, "maximum": 125}
                    node["heightConstraint"] = {"minimum": 35, "maximum": 35}
                    node["borderWidth"] = 1.5
                    node["label"] = "Unknown Object"
                    node["color"] = {"background": "black"}
                    flowchartResponse["operators"][flowchartResponseType][
                        flowID] = {
                            "_id": flow[objectID],
                            "flowID": flowID,
                            "flowType": flowType,
                            "flowSubtype": flowSubtype,
                            "node": node
                        }

                # Do any links need to be created
                for nextFlow in flow["next"]:
                    linkName = "{0}->{1}".format(flowID, nextFlow["flowID"])
                    linksList.append(linkName)
                    if linkName not in flowchartLinks:
                        flowchartResponse["links"]["create"][linkName] = {
                            "from": flowID,
                            "to": nextFlow["flowID"],
                            "logic": nextFlow["logic"]
                        }
                    #Updates (for logic for now) << needs to be readded but using same as node with color value set from server
                    #flowchartResponse["links"]["update"][linkName] = { "from" : flowID, "to" : nextFlow["flowID"], "logic" : nextFlow["logic"] }

    # Checking for deleted operators
    for flowchartOperator in flowchartOperators:
        if flowchartOperator not in flowsList:
            flowchartResponse["operators"]["delete"][flowchartOperator] = {
                "flowID": flowchartOperator
            }
    # Checking for deleted links
    for flowchartLink in flowchartLinks:
        if flowchartLink not in linksList:
            flowchartResponse["links"]["delete"][flowchartLink] = {
                "linkName": flowchartLink
            }

    return flowchartResponse, 200
コード例 #6
0
    def install(self):
        # Register models
        model.registerModel("occurrence", "_occurrence", "_action",
                            "plugins.occurrence.models.action")
        model.registerModel("occurrence clean", "_occurrenceClean", "_action",
                            "plugins.occurrence.models.action")
        model.registerModel("occurrenceUpdate", "_occurrenceUpdate", "_action",
                            "plugins.occurrence.models.action")

        # Finding conduct
        foundConducts = conduct._conduct().query(
            query={"name": "occurrenceCore"})["results"]
        if len(foundConducts) == 0:
            # Install
            c = conduct._conduct().new("occurrenceCore")
            c = conduct._conduct().get(c.inserted_id)
        elif len(foundConducts) == 1:
            # Reinstall
            c = conduct._conduct().get(foundConducts[0]["_id"])
        else:
            # Count invalid
            return False

        # Finding trigger
        foundTriggers = trigger._trigger().query(
            query={"name": "occurrenceCore"})["results"]
        if len(foundTriggers) == 0:
            # Install
            t = trigger._trigger().new("occurrenceCore")
            t = trigger._trigger().get(t.inserted_id)
        elif len(foundTriggers) == 1:
            # Reinstall
            t = trigger._trigger().get(foundTriggers[0]["_id"])
        else:
            # Count invalid
            return False

        # Finding action
        foundActions = action._occurrenceClean().query(
            query={"name": "occurrenceCore"})["results"]
        if len(foundActions) == 0:
            # Install
            a = action._occurrenceClean().new("occurrenceCore")
            a = action._occurrenceClean().get(a.inserted_id)
        elif len(foundActions) == 1:
            # Reinstall
            a = action._occurrenceClean().get(foundActions[0]["_id"])
        else:
            # Count invalid
            return False

        c.triggers = [t._id]
        flowTriggerID = str(uuid.uuid4())
        flowActionID = str(uuid.uuid4())
        c.flow = [{
            "flowID": flowTriggerID,
            "type": "trigger",
            "triggerID": t._id,
            "next": [{
                "flowID": flowActionID,
                "logic": True
            }]
        }, {
            "flowID": flowActionID,
            "type": "action",
            "actionID": a._id,
            "next": []
        }]
        webui._modelUI().new(
            c._id, {
                "ids": [{
                    "accessID": "0",
                    "delete": True,
                    "read": True,
                    "write": True
                }]
            }, flowTriggerID, 0, 0, "")
        webui._modelUI().new(
            c._id, {
                "ids": [{
                    "accessID": "0",
                    "delete": True,
                    "read": True,
                    "write": True
                }]
            }, flowActionID, 100, 0, "")

        c.acl = {
            "ids": [{
                "accessID": "0",
                "delete": True,
                "read": True,
                "write": True
            }]
        }
        c.enabled = True
        c.update(["triggers", "flow", "enabled", "acl"])

        t.acl = {
            "ids": [{
                "accessID": "0",
                "delete": True,
                "read": True,
                "write": True
            }]
        }
        t.schedule = "60-90s"
        t.enabled = True
        t.update(["schedule", "enabled", "acl"])

        a.acl = {
            "ids": [{
                "accessID": "0",
                "delete": True,
                "read": True,
                "write": True
            }]
        }
        a.enabled = True
        a.update(["enabled", "acl"])

        # Hide Created Models
        temp = model._model().getAsClass(query={"name": "occurrence clean"})
        if len(temp) == 1:
            temp = temp[0]
            temp.hidden = True
            temp.update(["hidden"])

        return True