通过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>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值