通过pycamunda进行流程引擎api的使用

1.pycamunda 简介

Pycamunda是一个用Python语言编写的Camunda BPM平台的客户端库。它可以用于与Camunda BPM引擎进行交互,通过Camunda REST API执行各种操作,如启动流程实例、完成任务等。它提供了一组易于使用的工具,使开发人员可以轻松地与Camunda BPM集成。Pycamunda还提供了一系列例子和文档,以帮助开发人员更好地利用Camunda BPM引擎的功能。
站在使用pycamunda进行后续开发时,可能常用的网址记录如下:
pycamunda的官方文档:https://pycamunda.readthedocs.io/en/latest/src/usage.html
camunda的restful接口:https://docs.camunda.org/manual/7.17/reference/rest/
国内开源社区:https://www.oschina.net/informat/camunda
官方文档:https://docs.camunda.org/manual/7.17/
官方论坛:https://forum.camunda.io/
GitHub社区:https://github.com/camunda-community-hub
GitHub开源库:https://github.com/camunda

2. pymanuda 进行bpmn部署

from pycamunda import deployment,processdef,processinst,task,user

url = "http://192.168.8.183:8080/engine-rest"


def create_deploy():
    #filepath = "diagram_6.bpmn"
    filepath = "diagram_1.bpmn"
    with open(filepath, "rb") as ff:
        content = ff.read()
    create_file = deployment.Create(url=url, name=filepath.split(".")[0], )
    # file1 = deployment.Create(url=url,name=filepath.split(".")[0],tenant_id="12345690").files
    create_file.add_resource(content)
    deploy = create_file()
    print(deploy)
    pro_dic = deploy.deployed_process_definitions
    #items = pro_dic.items()
    #print(len(pro_dic),pro_dic,type(pro_dic))
    for key,value in pro_dic.items():
        print(value)
        pro_key = value.key
        pro_id = value.id_
        print(pro_key)
        #start_pro = processdef.StartInstance(url=url,key=pro_key,business_key="xxxxx")
        start_pro = processdef.StartInstance(url=url, id_=pro_id, business_key="xxxxx")
        process1 = start_pro()
        processinst.Activate(url=url,id_=process1.id_)

create_deploy()

在以上程序中主要的部署程序为以下几条:

create_file = deployment.Create(url=url, name=filepath.split(".")[0], )
    # file1 = deployment.Create(url=url,name=filepath.split(".")[0],tenant_id="12345690").files
    create_file.add_resource(content)
    deploy = create_file()

这里用到了pycamunda.deployment.Create
不过源码的files属性方法应该改为如下:

   @property
    def files(self):
        return {f'resource-{i}.bnmp': resource for i, resource in enumerate(self._files)}

因为通过files方法会上传文件,并且把文件命名为resource-i,如果没有.bpmn后缀则无法正常识别。
在完成部署后需要启动进程和实例,完成之后就可以正常执行了。

3. 获取用户任务的信息

可以通过pycamunda.task的Getlist获取到用户的任务列表,获取任务列表之后可以获取到任务的id_,

from pycamunda import deployment,processdef,processinst,task,user,auth
import requests.auth
from setup import URL_CAMUNDA_ENGINE
import json
import requests
from bs4 import BeautifulSoup
def get_task(assignee):
    getlist = task.GetList(url=URL_CAMUNDA_ENGINE,assignee=assignee)
    tasktur = getlist()
    tasklist = []
    for taskitem in tasktur:
        task_id = taskitem.id_
        task_dic = get_task_form(task_id,assignee)
        tasklist.append(task_dic)
    print({"task":tasklist})
    return json.dumps({"task":tasklist},ensure_ascii=False)


def get_task_form(task_id,assignee):
    camunda_api_url = url + '/task/{}/rendered-form'.format(str(task_id))
    label_value = 'not found'
    form_variable = {assignee:""}
    try:
        response = requests.get(camunda_api_url)
        #print(response.content)
        rendered_form = response.content.decode('utf-8')
        rendered_html = "<html>\n<body>"+ rendered_form + '</body>\n</html>'
        #print(rendered_html)
        soup = BeautifulSoup(rendered_html,'html.parser')
        label_value = soup.find('label',{'for':assignee}).text
        label_value = label_value.strip()
        #print(label_value,type(label_value))
    except Exception as e:
        print(e)
        return {"code":1,"message":"not found rendered-form"} #json.dumps({"code":1,"message":"not found rendered-form"})
    camunda_api_url = url + '/task/{}/form-variables'.format(str(task_id))
    try:
        response = requests.get(camunda_api_url)
        form_variable = response.json()
    except Exception as e:
        print(e)
        return {"code":1,"message":"not found variables"} #json.dumps({"code":1,"message":"not found variables"})
    # form_variable = {'xiaoming': {'type': 'Boolean', 'value': False, 'valueInfo': {}}}
    info_dic = {"label":label_value,assignee:form_variable.get(assignee)}
    return info_dic

不过任务的具体内容如下:

(Task(assignee='xiaoming', case_definition_id=None, case_execution_id=None, case_instance_id=None, delegation_state=None, description=None, execution_id='36109935-50a4-11ee-aa94-02420a05074f', form_key=None, id_='3610c048-50a4-11ee-aa94-02420a05074f', name='xiaoming', owner=None, parent_task_id=None, priority=50, process_definition_id='Process_1fc5t32:3:360e7654-50a4-11ee-aa94-02420a05074f', process_instance_id='36109935-50a4-11ee-aa94-02420a05074f', suspended=False, task_definition_key='Activity_088d8gd', created=datetime.datetime(2023, 9, 11, 6, 7, 51, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=61200))), due=None, follow_up=None),)

里面不会包含任务的具体内容,要获取的话可以通过url/task/{id}/form-variables,例如:
http://192.168.8.140:8080/engine-rest/task/5dae2b35-52ce-11ee-928b-000c29ba0dac/form-variables进行获取,
而http://192.168.8.140:8080/engine-rest/task/5dae2b35-52ce-11ee-928b-000c29ba0dac/rendered-form获取到的是html文件的一部分也就是form文件,类似这样:

<form name="generatedForm" role="form">
  <div class="form-group">
    <label for="xiaoming">
      请假
    </label>
    <input class="form-control" name="xiaoming" cam-variable-type="Boolean" cam-variable-name="xiaoming" type="checkbox" value="false" />
    <div ng-if="this.generatedForm.xiaoming.$invalid && this.generatedForm.xiaoming.$dirty" class="has-error">
      <div ng-show="this.generatedForm.xiaoming.$error.required" class="help-block">
        Required field
      </div>
      <div ng-show="this.generatedForm.xiaoming.$error.camVariableType" class="help-block">
        Only a boolean value is allowed
      </div>
    </div>
  </div>
</form>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
流程引擎是一种用于管理和执行复杂业务流程的工具。在Python中,有一些流程引擎库可以帮助我们实现这一目标。下面是两个常用的流程引擎库的介绍: 1. Django Flow Django Flow是一个基于Django框架的流程引擎库,它提供了一种简单而强大的方式来定义和执行流程使用Django Flow,你可以通过定义状态和转换来构建流程模型,并使用它来管理和执行复杂的业务流程。下面是一个简单的示例: ```python from django.db import models from django_flow.models import FlowModel, FlowState, FlowTransition class MyFlowModel(FlowModel): # 定义流程状态 state1 = FlowState() state2 = FlowState() state3 = FlowState() # 定义流程转换 transition1 = FlowTransition(source=state1, target=state2) transition2 = FlowTransition(source=state2, target=state3) # 创建流程实例 my_flow = MyFlowModel() my_flow.save() # 执行流程转换 my_flow.transition1() my_flow.transition2() ``` 2. Zeebe Zeebe是一个开源的分布式流程引擎,它使用BPMN(Business Process Model and Notation)标准来定义和执行流程。Zeebe提供了一个Python客户端库,可以与Zeebe服务器进行通信,并管理和执行流程。下面是一个简单的示例: ```python from zeebe import ZeebeClient # 创建Zeebe客户端 client = ZeebeClient() # 部署流程定义 client.deploy_workflow("my_workflow.bpmn") # 创建流程实例 instance = client.create_workflow_instance("my_workflow") # 执行流程任务 client.complete_task("task1", variables={"key": "value"}) client.complete_task("task2", variables={"key": "value"}) ``` 这些是两个常用的Python流程引擎库的简介。你可以根据自己的需求选择适合的库来管理和执行流程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值