tags:

views:

109

answers:

2

I use pylons in my job, but I'm new to django. I'm making an rss filtering application, and so I'd like to have two backend processes that run on a schedule: one to crawl rss feeds for each user, and another to determine relevance of individual posts relative to users' past preferences. In pylons, I'd just write paster commands to update the db with that data. Is there an equivalent in django? EG is there a way to run the equivalent of python manage.py shell in a non-interactive mode?

+1  A: 

Yes, this is actually how I run my cron backup scripts. You just need to load your virtualenv if you're using virtual environments and your project settings.

I hope you can follow this, but after the line # manage.py shell you can write your code just as if you were in manage.py shell

You can import your virtualenv like so:

import site

site.addsitedir(VIRTUALENV_PATH + '/lib/python2.6/site-packages')

You can then add the django project to the path

import sys

sys.path.append(DJANGO_ROOT)
sys.path.append(PROJECT_PATH)

Next you load the django settings and chdir to the django project

import os

from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)

os.chdir(PROJECT_PATH)

After this point your environment will be set just like if you started with manage.py shell

You can then run anything just as if you were in the interactive shell.

from application.models import MyModel

for element in MyModel:
    element.delete()

Here is my backup file in full. I've abstracted the process out into functions. This would be named daily_backup and be put into the cron.daily folder to be run daily. You can see how to set up the environment and modify the functionality as needed.

#!/usr/bin/env python
import sys
import os
import site
import logging

from datetime import datetime

PROJECT_NAME = 'myproject'
DJANGO_ROOT = '/var/www/django'
PROJECT_PATH = DJANGO_ROOT + '/' + PROJECT_NAME
VIRTUALENV_PATH = '/var/www/envs/'+ PROJECT_NAME
BACKUP_DIR = '/var/www/backups/%s/daily' % (PROJECT_NAME)
TODAY = datetime.now().strftime('%Y%m%d-%H%M%S')
FILE_NAME = PROJECT_NAME + '_' + TODAY

site.addsitedir(VIRTUALENV_PATH + '/lib/python2.6/site-packages')
sys.path.append(DJANGO_ROOT)
sys.path.append(PROJECT_PATH)

from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)

os.chdir(PROJECT_PATH)

# manage.py shell
from django.conf import settings

logging.basicConfig(level=logging.WARN)

def _setup():
    if not os.path.exists(BACKUP_DIR):
        logging.debug('Creating backup directory ' + BACKUP_DIR)
        os.mkdir(BACKUP_DIR)
        os.mkdir(BACKUP_DIR + '/databases')
    else:
        logging.debug('Using backup directory ' + BACKUP_DIR)

def _remove_old():
    logging.debug('Cleaning out old backups')
    # keep past 7 days
    command = "find %s* -name '%s*' -mtime +7 -exec rm {} \\;" % (BACKUP_DIR, PROJECT_NAME)
    os.system(command)

def _create_backup():
    logging.debug('Backup database')

    if settings.DATABASE_ENGINE == 'mysql':
        command = 'mysqldump -u %s --password=%s %s > %s/databases/%s.sql' % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, BACKUP_DIR, FILE_NAME)
    else:
        command = '%s/bin/python %s/manage.py dumpdata --indent=4 > %s/databases/%s.json' % (VIRTUALENV_PATH, PROJECT_PATH, BACKUP_DIR, FILE_NAME)

    os.system(command)

    logging.debug('Backup project')
    command = 'tar -czf %s/%s.tgz -C %s %s/' % (BACKUP_DIR, FILE_NAME, DJANGO_ROOT, PROJECT_NAME)
    os.system(command)


if __name__ == '__main__':
    _setup()
    _remove_old()
    _create_backup()
digitaldreamer
Awesome, thanks! Putting manage.py shell at the beginning of the script is obvious in retrospect (the best hacks always are).
intractelicious
+2  A: 

I think that's what Custom Management Commands are there for.

macxxx