0

I've got a Django App where I need to follow different workflow for different inputs.

I receive an input parameter with the data payload over the POST endpoint. Based on the input, I need to fire different functions, process the data accordingly and finally save it in the data store.

One option is to write if-else, however writing if-else is difficult in maintaining as the code grows.

For eg:-

If input1, then function1(), process1(), save1()

elif input2, then function2(), process2(), save2()

I've looked into Intellect, django-viewflow and many other business-rule libraries, but not sure about the recommended way of doing it.

It'll be helpful if anyone can provide me a dummy example or an open-source project via which I can understand the implementation of the same.

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

1 Answers1

1

I think you are not building a huge application, right?

So to focus on the root requirement which is different processes for different inputs, you can create multiple objects extended from a Base object Processor, such as AppleProcessor or OrangeProcessor. All of them share the same interfaces.

And then in your logistical part, you can create a dictionary looks like:

processors = {"Apple": AppleProcessor, "Orange": OrangeProcessor}

Then it is easy to take it in use:

processor = processors.get(input)
processor.process(*some_needed_args, **some_needed_kwargs)
result = processor.get_result()

This is just a simple solution, if you have more restrictions, it may becomes much more complicated.

Sraw
  • 18,892
  • 11
  • 54
  • 87