set namespace for all db operations - python

I'm trying to set the namespace for all DB operations for the Google App Engine in python, but i can't get it done.
Currently my code looks something like this:
""" Set Google namespace """
if user:
namespace = thisUser.namespace
namespace_manager.set_namespace(namespace)
""" End Google namespace """
#Then i have all sorts of classes:
class MainPage(BaseHandler):
def get(self):
#code with DB operations like get and put...
class MainPage2(BaseHandler):
def get(self):
#code with DB operations like get and put...
class MainPage3(BaseHandler):
def get(self):
#code with DB operations like get and put...
app = webapp2.WSGIApplication([ ... ], debug=True, config=webapp2_config)
The problem with this is, is that in the classes all DB operations are still done on the default namespace (so as if no namespace is set). Eventhough i set the namespace in the very top of my code.
When i print the variable "namespace", which i also set in the top of the code, then i do get to see the namespace that i wish to use.
But it looks like Google App Engine somewhere resets the namespace to empty before running the code in the classes.
So now i'm wondering if there's a good way to set the namespace once somewhere.
Currently i set it like this in all "def's":
class MainPage(BaseHandler):
def get(self):
namespace_manager.set_namespace(namespace)
#code with DB operations like get and put...
class MainPage(BaseHandler):
def get(self):
namespace_manager.set_namespace(namespace)
#code with DB operations like get and put...
etc...
It's just not a very elegant solution.

You need to write a middleware that will intercept the request and will set the namespace according to your app logic.

A good solution is to add a hook. Something like that should be works.
from google.appengine.api import apiproxy_stub_map
NAMESPACE_NAME = 'noname'
def namespace_call(service, call, request, response):
if hasattr(request, 'set_name_space'):
request.set_name_space(NAMESPACE_NAME)
apiproxy_stub_map.apiproxy.GetPreCallHooks().Append(
'datastore-hooks', namespace_call, 'datastore_v3')
You can add it in your main.py or appengine_config.py. By this way the hook is configured during the loading of the instances and keeps his state.

You can use appconfig.py and define namespace_manager_default_namespace_for_request()
Have a read of https://developers.google.com/appengine/docs/python/multitenancy/multitenancy see the first section of "Setting the Current Namespace"

Related

What is the proper way to define the collection name at the application startup with MongoEngine?

I use MongoEngine as an ODM in my Flask application. Depending on the passed configuration document, MongoEngine should use a different collection.
At the moment I achieve this by changing the internal meta variable model._meta['collection']. Is there an alternative for selecting the collection?
from mongoengine import connect
from api_service.model import MyModel
create_app(config):
app = Flask(__name__)
# load app.config
connect(app.config['MONGODB_DB'],
host=app.config['MONGODB_HOST'],
port=app.config['MONGODB_PORT'],
username=app.config['MONGODB_USERNAME'],
password=app.config['MONGODB_PASSWORD'],
)
MyModel._meta['collection'] = app.config['MONGODB_MYMODEL_COLLECTION']
I know that you can define the collection by meta:{} in the class body of the model (see here). But I am not in the app context there and therefore I cannot access `app.config'.
You can simply modify the meta attribute inside the class itself
class MyModel(Document):
meta = {"collection": "my_actual_collection_name"}
...
Check This for more meta attributes you can use
Solution Update
I defined a helper class that can have a provide an access the application's configurations
class AppConfigHelper:
from flask import current_app
APP_CONFIG = current_app.config
and in the document import and use that class to get the collection name.
class MyModel(Document):
meta = {'collection': AppConfigHelper.APP_CONFIG['MONGODB_MYMODEL_COLLECTION']}
...
This is not the best solution I can think of, but it does the job.
Caution: this is not gonna work if you run it separately from Flask, it is going to crash, you can run it inside the app itself, or using flask shell

Correct way to register flask admin views with application factory

I am using an application factory to add views to my flask application like so :
(this is not my actual application factory, and has been shortened for the sake of brevity)
def create_app(config_name='default'):
app = Flask(__name__, template_folder="templates", static_folder='static')
admin_instance = Admin(app, name='Admin')
admin_instance.add_view(EntityAdmin(Entity, db.session))
My EntityAdmin class looks like this :
class EntityAdmin(ModelView):
column_filters = [
MyCustomFilter(column=None, name='Custom')
]
My custom filter looks like this :
class MyCustomFilter(BaseSQLAFilter):
def get_options(self, view):
entities = Entity.query.filter(Entity.active == True).all()
return [(entity.id, entity.name) for entity in entities]
The problem is that it seems that the get_options function is called when the app is instantiated, running a select query every time the create_app function gets called.
So if I update my database schema and run the flask db migrate command, I get an error because the new column I added does not exist when the select query is run. The query raises an error because my database schema is not in sync with the actual database.
Can I register my views only when an actual HTTP request is made ? How can I differentiate between a request and a command ?
You have one more problem with this filter: its options are created on the application instantiation so if your list of entities was changed during the application running it would still return the same list of options.
To fix both problems you don't need to postpone views registrations. You need the filter to get the list of options every time it is used.
This SO answer to the question "Resetting generator object in Python" describes a way to reuse a generator (in your case — a database query):
from flask import has_app_context
def get_entities():
# has_app_context is used to prevent database access
# when application is not ready yet
if has_app_context():
for entity in Entity.query.filter(Entity.active.is_(True)):
yield entity.id, entity.name
class ReloadingIterator:
def __init__(self, iterator_factory):
self.iterator_factory = iterator_factory
def __iter__(self):
return self.iterator_factory()
class MyCustomFilter(BaseSQLAFilter):
def get_options(self, view):
# This will return a generator which is
# reloaded every time it is used
return ReloadingIterator(get_entities)
The problem is that the query to the Entity table can be called multiple times during request. So I usually cache the result for a single request using Flask globals:
def get_entities():
if has_app_context():
if not hasattr(g, 'entities'):
query = Entity.query.filter(Entity.active.is_(True))
g.entities = [(entity.id, entity.name) for entity in query]
for entity_id, entity_name in g.entities:
yield entity_id, entity_name

Django - create a class instance in AppConfig.ready() only once

I need to create a class instance (lets say backend requests session) on the app startup(runserver), and I don't want to rewrite this session after running other management command. How can I achieve this? I tried several approaches and I'm not sure why something like this doesn't work.
# app/apps.py
class MyConfig(AppConfig):
....
requests_session = None
....
def ready(self):
if MyConfig.requests_session is None:
MyConfig.requests_session = requests.Session()
Unfortunately, the condition is always met and the session is recreated. This approach is recommended in the documentation though.
Other solution for me would be to run MyConfig.ready() only after using selected subset of management commands, is that possible?
Is there completely different better way for me to store requests session?
TIA
I think it should work if you use an instance variable instead of a class variable:
# app/apps.py
class MyConfig(AppConfig):
def __init__(self, app_name, app_module):
super(MyConfig, self).__init__(app_name, app_module)
self.requests_session = None
def ready(self):
if self.requests_session is None:
self.requests_session = requests.Session()
The question now is how to access this instance variable elsewhere. You can do that like so:
from django.apps import apps
# Here myapp is the label of your app - change it as required
# This returns the instance of your app config that was initialised
# at startup.
my_app_config = apps.get_app_config('myapp')
# Use the stored request session
req = my_app_config.requests_session
Note that this instance variable only exists in the context of the current process. If you run a management command in a separate process (e.g., manage.py ...) then that will create a new instance of each app.

Nested Blueprints in Flask?

I'm still new to Flask, so there may be an obvious way to accomplish this, but I haven't been able to figure it out so far from the documentation. My app is divided into several mostly disparate parts that share things like users/sessions/security and base template and everything but mostly do not interact much, and should be routed under different paths like /part1/.... I think this is pretty much exactly what blueprints are for. But what if I need to group routes and logic further under a blueprint?
For example, I have blueprint1 with url_prefix='/blueprint1' and maybe under that I want to have a collection of views revolving around a user sharing photos and other users commenting on them. I can't think of a better way of doing it than:
# app/blueprints/blueprint1/__init__.py
blueprint1 = Blueprint('blueprint1', __name__, template_folder='blueprint1')
#blueprint1.route('/photos')
def photos_index():
return render_template('photos/index.html')
#blueprint.route('/photos/<int:photo_id>')
def photos_show(photo_id):
photo = get_a_photo_object(photo_id)
return render_template('photos/show.html', photo=photo)
#blueprint.route('/photos', methods=['POST'])
def photos_post():
...
The problem here is that all the views related to the photos section of blueprint1 are located at the "top level," right with maybe blueprints for videos or audio or whatever (named videos_index()...). Is there any way to group them in a more hierarchical manner, like how the templates go under the 'blueprint1/photos' sub-directory? Of course I can put all the photo views in their own module to keep them organized separately, but what if I want to change the parent 'blueprint1/photos' path to something else? I'm sure I can invent a function or decorator that groups related routes under the same root path, but then I still have to name all the functions with the photos_ prefix and reference them like url_for('blueprint1.photos_show') It seems like blueprints are the answer when a Flask app gets large and you need to group and compartmentalize similar parts together, but you cannot do the same thing when the blueprints themselves get large.
For reference, in Laravel you can group related "views" under a Controller class where the views are methods. Controllers can reside in hierarchical namespaces like app\Http\Controllers\Blueprint1\Photocontroller, routes can be grouped together like
Route::group(['prefix' => 'blueprint1'], function() {
Route::group(['prefix' => 'photos'], function() {
Route::get('/', ['as' => 'blueprint.photos.index', 'uses' => 'ModelApiController#index']);
Route::post('/', ['as' => 'blueprint.photos.store', 'uses' => 'ModelApiController#store']);
Route::get('/{id}', ['as' => 'blueprint.photos.get', 'uses' => 'ModelApiController#get'])
->where('id', '[0-9]+');
});
});
and routes can be gotten like action('Blueprint1\PhotoController#index').
If only I could make a photos blueprint, then just do blueprint1.register_blueprint(photos_blueprint, url_prefix='/photos') or the like, these problems would pretty much be solved. Unfortunately Flask does not seem to support nesting blueprints like this. Is there an alternative way to handle this problem?
UPDATE
Flask 2 was released with support for nested blueprints.
[ START: Part from the docs ]
Nesting Blueprints
It is possible to register a blueprint on another blueprint.
parent = Blueprint('parent', __name__, url_prefix='/parent')
child = Blueprint('child', __name__, url_prefix='/child')
parent.register_blueprint(child)
app.register_blueprint(parent)
The child blueprint will gain the parent’s name as a prefix to its name, and child URLs will be prefixed with the parent’s URL prefix.
url_for('parent.child.create')
/parent/child/create
Blueprint-specific before request functions, etc. registered with the parent will trigger for the child. If a child does not have an error handler that can handle a given exception, the parent’s will be tried.
[ END: Part from the docs ]
Source: https://flask.palletsprojects.com/en/2.0.x/blueprints/#nesting-blueprints
OLD ANSWER
My hacky work around is that I made a class called ParentBP that has the following code
from typing import List
from flask import Blueprint
class ParentBP(object):
name: str
url_prefix: str
subdomain: str
blueprints: List[Blueprint]
def __init__(self, name="", url_prefix="", subdomain="") -> None:
self.name = name
self.url_prefix = url_prefix
self.subdomain = subdomain
self.blueprints = []
def register_blueprint(self, bp: Blueprint) -> None:
bp.name = self.name + "-" + bp.name
bp.url_prefix = self.url_prefix + (bp.url_prefix or "")
if self.subdomain:
bp.subdomain = self.subdomain
self.blueprints.append(bp)
so you can call it similar to a blueprint like below
blueprint1 = Blueprint("blueprint1", __name__)
blueprint2 = Blueprint("blueprint2", __name__, url_prefix="/bp2")
api_v1 = ParentBP("api-v1", url_prefix="/api/v1")
api_v1.register_blueprint(blueprint1)
api_v1.register_blueprint(blueprint)
to make the interface similar to normal registering of blueprints to the flask app, I extended the Flask class as follows
class ExtendedFlask(Flask):
def register_blueprint(self, blueprint: Union[Blueprint, ParentBP], **options: Any) -> None:
if isinstance(blueprint, ParentBP):
for bp in blueprint.blueprints:
super().register_blueprint(bp, **options)
else:
return super().register_blueprint(blueprint, **options)
now you can normally do the following
app = ExtendedFlask(__name__)
app.register_blueprint(api_v1)
Unfortunately, nested blueprints are not a current feature in Flask. You'll have to do it manually. You could probably code something that works for your specific case, but a general solution has not been added to Flask. There has been some discussion on the issue tracker:
https://github.com/mitsuhiko/flask/issues/593
https://github.com/mitsuhiko/flask/issues/1548
https://github.com/pallets/flask/issues/3215
Adding nestable blueprints into Flask is not as trivial as automatically appending a prefix to routes. There are many other features of blueprints that need to be considered when nesting that make a general implementation significantly more complicated. The reason this has not been implemented yet is that no one in the community has had a great enough need for it that wasn't solved by a quick workaround vs contributing a general implementation.
I made a class called NestedBlueprint to hack it.
class NestedBlueprint(object):
def __init__(self, blueprint, prefix):
super(NestedBlueprint, self).__init__()
self.blueprint = blueprint
self.prefix = '/' + prefix
def route(self, rule, **options):
rule = self.prefix + rule
return self.blueprint.route(rule, **options)
Here is my base file which contains the blueprint: panel/__init__.py
from flask import Blueprint
panel_blueprint = Blueprint(PREFIX, __name__, url_prefix='/panel')
from . import customize
Here is the specific/nested file which contains nested blueprint: panel/customize.py
from rest.api.panel import panel_blueprint
from rest.api.util.nested_blueprint import NestedBlueprint
nested_blueprint = NestedBlueprint(panel_blueprint, 'customize')
#nested_blueprint.route('/test', methods=['GET'])
def test():
return ':)'
You can then call like this:
$ curl http://localhost:5000/panel/customize/test
:)
Here is my workaround:
When importing a blueprint, I define my nested routes:
app.register_blueprint(product_endpoints, url_prefix='/sites/<int:site_id>/menus/<int:menu_id>/categories/<int:category_id>/products/<int:product_id>')
app.register_blueprint(category_endpoints, url_prefix='/sites/<int:site_id>/menus/<int:menu_id>/categories/<int:category_id>')
app.register_blueprint(menu_endpoints, url_prefix='/sites/<int:site_id>/menus/<int:menu_id>')
app.register_blueprint(site_endpoints, url_prefix='/sites/<int:site_id>')
And inside the blueprints, I'm reusing route parse functions. For example, in the product_endpoints file:
from category_endpoints import get_category_data
product_endpoints = Blueprint('product_endpoints', __name__)
#product_endpoints.url_value_preprocessor
def get_product_data(endpoint, values):
if 'category_id' in values:
get_category_data(endpoint, values)
product = Product.get_by_id(int(values.pop('product_id')))
if not product:
abort(404)
g.product = product
and in category_endpoints file:
from menu_endpoints import get_menu_data
category_endpoints = Blueprint('category_endpoints', __name__)
#category_endpoints.url_value_preprocessor
def get_category_data(endpoint, values):
if 'menu_id' in values:
get_menu_data(endpoint, values)
category = ProductCategory.get_by_id(int(values.pop('category_id')))
if not category:
abort(404)
g.category = category
etc... With that approach, my blueprint is also usable with direct routes like /products/<int:product_id>.
This approach worked for me very well. I hope it can also help you.

Access instance WSGIApplication

I want to access to a configuration dictionnary outside of a class.
How can I do that ?
Here is the example of the webapp website.
import webapp2
config = {'foo': 'bar'}
app = webapp2.WSGIApplication(routes=[
(r'/', 'handlers.MyHandler'),
], config=config)
For instantiating the app
import webapp2
class MyHandler(webapp2.RequestHandler):
def get(self):
foo = self.app.config.get('foo')
self.response.write('foo value is %s' % foo)
For accessing the config value in a class.
So I want to use the self.app.config.get('foo') just above the class to initialize a global variable. Naturally I can't use self outside of the class so I would like to know what I must do to retrieve the value.
In my application the configuration dictionnary is in a separate file and I have multiple configuration files so I don't want to import the file and do something like config['foo'] because I need to use the configuration file that the application is using.
Thank you
I do not know if I understand your question, but you can use a utility to get the app (without self):
webapp2.get_app()
docs: http://webapp-improved.appspot.com/api/webapp2.html#utilities

Categories