Version 2 (modified by Antonio Cavedoni, 19 years ago) ( diff )

Added syntax highlighting to the code samples

Sick of running django-admin repeatedly to init your site and install each of your apps when you've made lots of changes to the model? miniflush.py will do all the heavy lifting for you:

import os
from os.path import isdir, isfile, join, dirname
import django.core.management, django.core

def find_site(): 
    """Find the site by looking at the environment."""
    try: 
        settings_module = os.environ['DJANGO_SETTINGS_MODULE']
    except KeyError: 
        raise AssertionError, "DJANGO_SETTINGS_MODULE not set."

    settingsl = settings_module.split('.')
    site = __import__(settingsl[0])
    settings = __import__(settings_module, {}, {}, settingsl[-1])
    return site, settings
    
def delete_db(settings): 
    """Delete the old database."""
    engine = settings.DATABASE_ENGINE
    if engine == 'sqlite3': 
        try: 
            os.unlink(settings.DATABASE_NAME)
        except OSError: 
            pass
    elif engine == 'mysql': 
        import _mysql
        s = _mysql.connect(host=settings.DATABASE_HOST, 
                           user=settings.DATABASE_USER, 
                           passwd=settings.DATABASE_PASSWORD)
        for cmd in ['drop database if exists %s', 
                    'create database %s']: 
            s.query(cmd % settings.DATABASE_NAME)
    else: 
        raise AssertionError, "Unknown database engine %s", engine

def init_and_install(site): 
    """Initialise the site and install any applications. Applications 
    are found by looking for ``sitename/apps`` in sitename.apps."""
    django.core.management.init()
    appdir = join(dirname(site.__file__), 'apps')
    for name in [name for name in os.listdir(appdir) 
                if isdir(join(appdir, name))
                and isfile(join(appdir, name, '__init__.py'))]: 
        try: 
            app = django.core.meta.get_app(name)
            django.core.management.install(app)
        except AttributeError, ex: 
            pass # Exercise for reader: be more specific

def main(): 
    """Flush it all, baby!"""
    try: 
        site, settings = find_site()
        delete_db(settings)
        init_and_install(site)
    except AssertionError, ex: 
        print ex.args[0]

if __name__ == '__main__': 
    main()
Note: See TracWiki for help on using the wiki.
Back to Top