Vadym Fedorov
Vadym Fedorov

Reputation: 2415

Flask: View, model and business logic segration

Please help me how to solve following task in "pythonic" way:

There are several model classes, which are mapped to the DB with the help of SQLAlchemy.

There is a Flask view, which handles the "POST" request.

The business logic of this method contains complex logic which includes:

  1. Getting input parameters from input JSON
  2. Validation
  3. Creation of several different models and the saving to database.

Is it a good idea to leave this logic in the "View"? Or it would be much better to separate this this logic into different modules or classes, for instance by introducing business logic class?

Upvotes: 0

Views: 2357

Answers (1)

Andrew Cox
Andrew Cox

Reputation: 10988

If you need to unit test the code separate from the View then you should defiantly separate it into another module or class.

As there seems to be three parts to your business logic then I would say starting by splitting the view into three functions of a module seems a good place to start.

Upvotes: 1

Related Questions