Source

django-swingcms / swingcms / cms / management / commands / initialize.py

Fabio Michelini 7dc8c13 

























































#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from modules.utilities import easy_import

from django.core.management.base import BaseCommand, CommandError

import settings



class Command(BaseCommand):

    help = "Execute initialize functions in initialization module for app."
    requires_model_validation = False

    def handle(self, *args, **options):

        apps = ()

        if not args:
            raise CommandError(_("Specify what initialize please."))

        elif len(args) == 1 and args[0] == 'all':
            apps = settings.INSTALLED_APPS

        else:
            for a in args:
                if not a in settings.INSTALLED_APPS:
                    raise CommandError(_("App %s not installed.") % a)
                else:
                    apps += (a,)

        for a in apps:
            if os.path.isdir(os.path.join(settings.ROOT, a)):
                try:
                    initialization = easy_import('%s.%s' % (a, 'initialization'), '*')
                except ImportError:
                    if settings.VERBOSE:
                        print "FAIL: app %s have not initialization module" % a
                    continue
                except Exception, e:
                    if settings.VERBOSE:
                        print "FAIL: %s in import app %s" % (str(e), a)
                    continue

                # execute initialize function
                for k in initialization['__all__']:
                    v = initialization[k]
                    if hasattr(v, '__call__'):
                        try:
                            if settings.VERBOSE:
                                print "\nEXECUTE %s in %s.%s" % (k, a, 'initialization')
                            v()
                        except Exception, e:
                            if settings.VERBOSE:
                                print "FAIL: %s in %s.%s" % (unicode(e), a, 'initialization')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.