制作Django Rest Framework(DRF)工作流程的方法

时间:2018-09-20 08:04:17

标签: python django django-rest-framework workflow

我正在构建一个业务应用程序,它将支持多种贷款产品。

例如:房屋贷款,汽车贷款,个人贷款,电子商务贷款。

涉及的主要步骤是:

  1. 入职(产生潜在客户)
  2. 用户信息(和验证)
  3. 贷款信息(信誉)
  4. 支出​​

业务流程的一个示例是:

  1. 客户进入机舱,注册他的手机号码,并通过OTP进行验证

  2. 填写他的个人信息(验证该信息)

  3. 提供贷款金额

  4. 检查贷款信誉

  5. 分配资金(在XYZ验证后)

  6. 提供银行帐户详细信息

  7. 验证银行帐户(仅在您具有abc信息之后)

  8. 做eKYC

  9. 支付

现在,我正在使用Django REST Framework来构建Web API。但是,有一个问题。

在我们的另一个产品中,流程可能有所不同。 Step 4Step 6可以互换,但是Step 7需要在同一位置完成。基本上,我应该可以灵活地重新安排活动(节点)。

到目前为止,编写的API(尽管是模块化的)仅针对一种产品。如何使用DRF作为工作流方法?或使用DRF之上可以控制流程的任何库。

1 个答案:

答案 0 :(得分:1)

我们有一个类似的用例,并使用了一个流库,该库将根据条件驱动流捕获整个工作流。

您可以查看Viewflow:https://github.com/viewflow/viewflow

基本上,这就像设置流并利用条件来定向和重定向到其他机制。他们的简单快速入门页告诉您如何实现:http://docs.viewflow.io/viewflow_quickstart.html

我只是在您的情况下尝试了一些示例流程:

class CustomerProcessFlow(Flow):
    process_class = CustomerProcess

    start = (
        flow.Start(
            views.CustomerOnBoardView # Let's say this is your customer onboard view
            fields=["customer_name", "customer_address", "customer_phone"]
        ).Permission(
            auto_create=True
        ).Next(this.validate_customer)
    )

    validate_customer = (
        flow.View(
            views.ValidateCustomerDataView # Validation for customer data,
            fields=["approved"]
        ).Permission(
            auto_create=True
        ).Next(this.loan_amount)
    )

    loan_amount = (
        flow.View(
            views.LoanView # Provide Loan
            fields=["loan_amount"]
        ).Permission(
            auto_create=True
        ).Next(this.check_customer_association)
    )

    check_customer_association = (
        flow.If(lambda customer_association: ! customer.type.normal)
        .Then(this.step_4_6)
        .Else(this.step_6_4)
    )

    step_4_6 = (
        flow.Handler ( this.check_load_credibility_data )
        .Next( this.provide_bank_details_data )
    )

    step_6_4 = (
        flow.Handler( this.provide_bank_details_data )
        .Next(this.check_load_credibility)
    )

    this.check_load_credibility = (
        flow.Handler( this.check_load_credibility_data )
        .Next( this.end )
    )

    this.provide_bank_details_data = (
        flow.Handler( this.provide_bank_details_data )
        .Next(this.end)
    )

    end = flow.End()

    def check_load_credibility_data(self, customer):
        # Load credibility

    def provide_bank_details_data(self, customer):
        # Bank Details

可以看到一个示例here