agdsn / sipa

The Supreme Information Providing Application
MIT License
13 stars 10 forks source link

Replace `current_app.extensions`-usages and proxies by access wrappers #454

Open lukasjuhrich opened 1 year ago

lukasjuhrich commented 1 year ago

Rationale

Instead of using LocalProxys ourselves, we should just store the relevant objects in g and access that. This would make the code easier to read and more in line with flask tutorials.

To make this well-typed, we could add a „type shim“ for g, which knows about the stored attributes, and import that.

Usages

https://github.com/agdsn/sipa/blob/004bb6a7581229ccc5ea3b375330dfc3e4766b66/sipa/backends/extension.py#L340-L341 https://github.com/agdsn/sipa/blob/004bb6a7581229ccc5ea3b375330dfc3e4766b66/sipa/blueprints/register.py#L22 https://github.com/agdsn/sipa/blob/004bb6a7581229ccc5ea3b375330dfc3e4766b66/sipa/model/sample/user.py#L38 https://github.com/agdsn/sipa/blob/004bb6a7581229ccc5ea3b375330dfc3e4766b66/sipa/model/pycroft/user.py#L25 https://github.com/agdsn/sipa/blob/004bb6a7581229ccc5ea3b375330dfc3e4766b66/sipa/model/pycroft/userdb.py#L47

lukasjuhrich commented 10 months ago

This was based on a misunderstanding of the scope of the g proxy: It is lifetime-bound to the application context, however usually there is one application context per request.

Using app.extensions is fine, however for type-safe access one might go the flask_babel way and provide well-typed access methods like get_backends(app) / get_api(app).