Create New Model Instance with API Call in Django - python

I'm unsure how to approach this problem in general in my Django app:
I need to make a call to an API every n days. I can make this call and fetch the data required via Python, but where exactly should I put the code?
Do I put the code in a specific view and then map the view to a URL and have that URL called whenever I want to create new model instances based on the API call?
Or am I approaching this the wrong way?

The way I usually do this is with a combination of custom Django-admin commands, and then run them wit a scheduled Cron job
You can run your custom commands in the same way as you would run the default ones:
python manage.py <your_command_name> <your_command_arguments>

Sounds like you are trying to have a schedule-able job. Celery works well for this sort of situation.
You would create a task that runs every N days. In that task, you would put your code that calls the API and processing the response as necessary.
Reference:
Celery Periodic Tasks

Do I put the code in a specific view
a django view is a callable that must accept an HTTP request and return an HTTP response, so unless you need to be able to call your code thru HTTP there's no point in using a view at all, and even if you want to have a view exposing this code it doesn't mean the code doing the API call etc has to live in the view.
Remember that a "django app" is basically a Python package, so beside the django-specific stuff (views, models etc) you can put any module you want and have your views, custom commands etc call on these modules. So just write a module for your API client etc with a function doing the fetch / create model instance / whatever job, and then call this function from where it makes sense (view, custom command called by a cron job, celery task, whatever).

Related

Making django server tick?

I have a django server setup on Linux/Apache/Mysql with WSGI (WGSIDaemonProcess/WGSIDaemonGroup/WSGIScriptAlias).
I'd like to write a python function that has access to all the backend models and stuff that a normal view function has, and to have that function run periodically (perhaps once a second or faster) while the server is up.
I guess I could write a new view function and then write a local script to GET that url every second, but is there a way of doing this within the django app itself?

flask API calls scheduling with cron jobs

I have a function which calls several API's and updates the database upon being called. I want to schedule the function to run daily at specific time.
Already tried flask_apscheduler and APScheduler which gives this error:
This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.
Any leads on this will be helpful.
You should:
Post the code where you define your flask application.
Specify how you try to access the app.
How you're calling the APIs.
Whether those APIs are 3rd party or part of your blueprint.
However, this is probably a context issue. I have come across a similar one with SQLAlchemy before.
You will need to somehow get access to your app, either by using app_context or by importing current_app from Flask and accessing the config.
Assuming you imported the app where your function is used, try this:
with app.app_context():
# call your function here
Refer to this document for more information: Flask Documentation
Another approach you can try, is passing your app configurations through a config class object.
You can define the jobs you want to schedule and pass a reference to your function inside.
Check this example from flask-apscheduler repository on GitHub.

Using Django outside of view.py

I have a twisted based script running that is managing IO, monitoring serial inputs, writing logs etc. It uses Twisted to run events every minute and every hour as well as interrupt on serial traffic.
Can Django be used to provide an interface for this, for example taking live values and display them using
#python code generating value1 and value2
def displayValues(request):
context = {
'value1':value1,
'value2':value2
}
return render(request, 'interface.html', context)
The obvious issue is that this python file doesn't live in the Django file setup and so the URL call wouldn't know where to look or how to call the displayValues function.
An additional feature I might look to is to write the IO values in to a mysql database through Django as it is already setup.
I understand Django from a simple databases application point of view but this is not something I've come across online and I might be moving outside of the scope of Django.
I've seen this but it is more to do with using the Model outside of the standard setup. Using Django database layer outside of Django?
Is this possible?
Thanks!
Why do you need Django for such a simple use case?
For simple Http requests you can you the included Python tool:
https://docs.python.org/2/library/simplehttpserver.html

Python-eve: Create a custom webservice

how do I create a Custom webservice using Python-eve?
If it was in Flask, I would only put the route decorator before the function and inside the function I could do whatever I wanted (call another service, run a python function, etc).
With Eve, I define de resources (domains) and it processes put, get, etc by itself using the database.
Should I just decorate a function like I would do with Flask?
You could also register a Blueprint like you would do with any other Flask application. That's what Eve-Docs is doing. You might want to check it out.
Eve is Flask (in fact, it is a subclass of it) so, in general, whatever works with Flask works with Eve too.

Tools for initializing a database with GAE

I'm looking for a way to initialize a database with a set of preliminary data. I've been looking around, but haven't seem to find anything that exactly matches what I'd like to do. Any suggestions?
The Datastore Administration allow to copy data to another app. But if you need this tool for testing your app in local, as I suppose, you could write a script.
You can also do this programatically. Write a function that initializes your database and call it from inside a dedicated URL handler, e.g.
http://myapp.com/prepareDb
See here how to configure this URL so that only admins of the app have permission to access it.
An HTTP request coming from a user has 60 seconds to complete before it is aborted by app engine. If, for some reason, you need more than 60 seconds to prepare your database, do it from a cron job (which has 10 minutes to finish).

Categories