0

How can I structure my flask project such that my models can be aware of if TESTING is enabled (and thereby use a testing database), without having them deal with or have any knowledge of app context.

I'm developing this as an open source project so the source might shed some light on this: https://github.com/nficano/jotonce.com/blob/master/jotonce/messages/models.py#L33

NFicano
  • 1,065
  • 1
  • 11
  • 26

1 Answers1

0

I think you're running into this problem because you don't provide a way for users to specify configuration at run-time. Instead, managers.py grabs whatever settings that are specified in your settings.py file without consulting what settings the end-user might have specified.

Since you do have factory.py, you could potentially import current_app from Flask (assuming that your db functions are called within an app context) and use the settings value there. If that's an option for you, Flask has some good suggestions for configuration handling.

If you're running this outside of your application context, I don't think how factory.py is currently structured is going to work for you. You'll need to handle your own configuration manually.

You can take a look at https://github.com/Robpol86/Flask-Large-Application-Example/blob/master/pypi_portal/application.py for an example of a large flask project that uses the app factory with different configuration values well.

Good luck, and happy holidays!

Mike Lawson
  • 735
  • 6
  • 12
  • I based my project structure on this: https://github.com/mattupstate/overholt/tree/master/overholt would you happen to have an example of a well structured project that uses app factory pattern? That said, I do use a factory pattern for creating the app here: https://github.com/nficano/jotonce.com/blob/master/jotonce/factory.py I just don't see how I can use this pattern to get my models have knowledge of whether `TESTING` is enabled. – NFicano Dec 25 '15 at 17:31
  • I'd probably recommend Flask, to be honest. [app.py](https://github.com/mitsuhiko/flask/blob/master/flask/app.py) is extremely well documented and provides a good starting place for you. – Mike Lawson Dec 25 '15 at 17:36
  • Oh! I didn't see factory.py. That's great. In that case, I'm going to edit my response. One minute (or two). – Mike Lawson Dec 25 '15 at 17:38