Executing shell commands from a Django app/project - django

I want to be able to execute some shell commands like mkvirtualenv and createdb and such from a Django app, these processes will likely go into a Django celery task later. I would think using system() would be insecure to execute shell commands from Django/Apache, so my question is what is the best way to execute shell commands from a django app?

To start a shell process in the background you can use the subprocess module.

Related

How to have a Django app running on Heroku doing a scheduled job using Heroku Scheduler

I am developing a Django app running on Heroku.
In order to update my database with some data coming from a certain API service, I need to periodically run a certain script (let's say myscript).
How can I use Heroku Scheduler to do it?
As already explained here, quick and simple way to answer this question is asking yourself how would you do to run that script periodically, as if you were the scheduler yourself.
Now, the best way to run a script in your Django app at any moment, it is to create a custom management command and to run it from your command prompt when you need it, like this:
python manage.py some_custom_command
Then, if you were the scheduler, you would run that command from your command prompt at every time written in the schedule.
So, a good idea would be to make Heroku Scheduler behave the same. Thus, the aim here is to have Heroku Scheduler run python manage.py some_custom_command at scheduled times.
Here is how you can do it:
In your_app directory, create a folder management and then inside it create another folder commands and finally, inside it, create a file some_custom_command.py
So, just to be clear
your_app/management/commands/some_custom_command.py
Then, inside some_custom_command.py insert:
from django.core.management.base import BaseCommand
from your_app.path_to_myscript_file import myscript
class Command(BaseCommand):
def handle(self, *args, **options):
# Put here some script to get the data from api service and store it into your models.
myscript()
Then go on Heroku > your_app > resources
In add-ons section select Heroku Scheduler, click on it so that its window opens, then click on add job, select the time you want, insert the command python manage.py some_custom_command and save.

How to hook a debugger to a python code running via django crontab?

I have a Django based web application, some functionality of the application is scheduled to be run as a part of cron jobs using django-crontab. I want to hook a debugger so that I can inspect some odd behaviours of my code. I normally use visual studio code. Is it possible to hook a debugger, since cron jobs basically run independently apart from server?
You can put a breaking point debugger in the code using pdb or ipdb. Like this:
def some_function():
# some code
import pdb;pdb.set_trace() # or use ipdb
# rest of the code
Then in shell, run python manage.py crontab show to show cronjobs with ids, then run python manage.py crontab run <id>. It will hit the debugger, then you will hit the breaking point. Thus you can use debugger here.

How to run a script shell in django

I am developing a web app using django. I've a script shell that runs python codes for image processing. I want to execute this script shell using my web app so that users can apply image processing just by using a button in the web interface.
(for the moment i'm working localy , the django web app and the script shell are in the same folder).
I don't know how to do it. can you help me ?
thank you in advance.
Python standard library has the subprocess module which can be used just for that. For instance, given ./script.sh is your shell script a call to subprocess.call will do the work:
>>> import subprocess
>>> # From your working directory:
>>> subprocess.call(["./script.sh"])

Cron Job that access django Models

What i have done?
I have written a small application in django with few models with sqlite3 as backend. Now i want to write a python code that clears the database elements based on certain condition.
Question:
How can i achieve the above requirement?
I think the cleanest way to do this is to write your own django-admin command.
You can then run the command using manage.py:
python manage.py your_command
Having a command that can be run in shell, you can easily put in into your crontab. Optionally django-admin commands can receive command line arguments, if needed.

Execute command inside django

I've been working with the PaaS service AppFog and I was able to get my Django up and running, but the problem is that my application uses static files and this are not working because it needs to execute the collectstatic command in shell.
I've been reading about it on the internet but I wasn't able to find a proper solution. Should I make a shell file and execute it? How?
I appreciate your time.
You can execute collectstatic with the following command:
python manage.py collectstatic
This is called an administrative management command. You can find out more about these and implementing them yourself by reading https://docs.djangoproject.com/en/1.4/howto/custom-management-commands/.