I want to have multiple services on Google App Engine powered with Flask and Flask-SQLAlchemy and one MySQL database. The problem is that I have database version control (Flask-Migrate) and ORM definition (models.py) in one app.
The question is, how can I have right access to the database with the ORM from another service (another flask app)? Should I define the same models.py file for all this apps (share one copy)? I don't think this is the best solution
Thanks!
Related
This is my first Django app, and also my first time building an app, and I am seeking some DB and API guidance. I have an app that functions as an online directory and marketplace. What I aim to do is to provide the app to many different organizations such that the organizations have their own online directory's and marketplace's where they can manage it fully on their own, but that their data is still linked to my database so that I can then operate on the data for machine learning purposes. This is a pretty standard/routine practice, I realize, but I am trying to wrap my head around how best to make it work. For each organization, would there just be an instance of the app which would then be a separate app or interface that connects to the original? From my newbie understanding, that is essentially what an API is, correct?
Since it's an API, I wouldn't separate the DBs or the instances, neither make several Django apps, but I would have a dedicated clients model/table in the DB, which would store encoded API keys for each of your organizations.
Each client makes its request to your API by authenticating and using their API key, and the Django views answer only with the data linked to the client, as other DB tables have a foreign key to clients.
Sounds pretty standard and RESTful to me.
Some Django / Django REST framework modules like "Django REST Framework API Key" could probably do most of this work for you.
I have been deployed my Django project to the Google App Engine. I wanna add cronjobs to my Django project and there is a cool feature for it in Google App Engine.
If I understand it well, I must create GET functions for my cronjobs in my views.py. But how do I make them callable only by the App Engine and no one else? Or, maybe there is a better solution for cronjobs in Django?
You can validate your request by checking header X-Appengine-Cron at your endpoint. This header is only supplied when your endpoint is triggered by GAE cron jobs.
Reference link:
https://cloud.google.com/appengine/docs/standard/python3/scheduling-jobs-with-cron-yaml#validating_cron_requests
How can I connect multiple Google App Engine apps to my one Django app engine service so that I can write to another apps datastore? Is this even possible?
Directly accessing an app's datastore from another application is possible (you don't really need to write to the app itself for that!)
The fact that the other app is also a GAE app or not doesn't really matter, setting up the access control and accessing the respective datastore are the same.
I captured the details in How do I use Google datastore for my web app which is NOT hosted in google app engine?
If you don't want to give direct datastore access to the outside app then you could implement an inter-app communication protocol to achieve what you want:
the app owning the datastore would act as a server for the other apps and would perform itself the datastore accesses on their behalf
the other apps would be clients, sending requests to the server app to get it to perform the desired actions
With this approach you can implement any access control/restriction scheme you want on the server side, which is not really possible with the direct datastore access method.
Business case:
We have multiple databases that need to be accessed and we don't know which until a URL/Route is called. The database server and database name are part of the route.
example: http://<flask_server>/<db_server>/<db_name>/weeklyreport
Since standard Flask-SQLAlchemy uses APP settings to define the DB connection and APP settings cannot (should not) be changed at runtime... how could one accomplish this?
I've just sidestepped the issue by using straight SQLAlchemy and not Flask-SQLAlchemy as I could find no way around this. For my needs I won't miss the benefits of the Flask wrappers.
Is it possible in Python Flask to connect to 2 databases, one mysql and one mongo?
This is for an e-commerce app where the users are stored in mysql and the products in mongo.
If yes, can you give me an example?
Thanks.