Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid events inside models

I'm using Pyramid 1.4. I would like to generate some custom Pyramid events from inside my model classes. Events are generated like so:

request.registry.notify(MyCustomEventType("Here it comes"))

As you can see, I need access to the application registry. I'm aware of get_current_registry() function. But I'm also concerned about this comment from the Pyramid website:

"This function should be used extremely sparingly, usually only in unit testing code"

Questions:

  • Is generating Pyramid events from the data layer (SQLAlchemy models) generally a bad idea?
  • If not, how to access application registry in a more civilised way? (Extending Base model perhaps?)
  • If yes, is there some alternative I could use. I'm aware of SQLAlchemy events, but I couldn't find an ability to generate custom events.

Rationale:

Basically, I divided my app into features and I try to keep them decoupled. For that, I sometimes need IoC: I was planning on using events as a mean for that. For instance, whenever a user answers a questionare, an event is emitted. Then, such an event can be subscribed to in another parts of the application. I like to keep the application logic in the models and not in the views. Hence, the described issue.

like image 475
julx Avatar asked Nov 12 '22 12:11

julx


1 Answers

What is the use case for firing the pyramid events from your models? Generally that would be a bad idea.

As for connecting up the app/models. Most of that is done in the initialize call in your main() that passes the settings to the model to give it connection settings and the like. I'd make sure any logic done to tie things together be limited to that single function call on application start up.

In the end though I'd imagine what you want is better done in another way.

like image 120
Rick Avatar answered Nov 15 '22 06:11

Rick