Beispiel #1
0
def _split_project():
    global args
    global profiles
    global projectname
    global settings

    profiles = ('develop', 'staging', 'production')
    filenames = ('__init__', 'common') + profiles

    _print_verbose(2, 'Creating directories ...')
    os.mkdir('%s.media' % projectname)
    os.mkdir('%s.static' % projectname)
    os.mkdir('%s.templates' % projectname)
    os.mkdir(os.path.join(projectname, 'settings'))

    _print_verbose(
        2, 'Converting settings to deployment profiles (%s) ...' %
        ', '.join(profiles))
    os.rename(os.path.join(projectname, 'settings.py'),
              os.path.join(projectname, 'settings', 'common.py'))

    settings = DjangoSettingsManager(projectname, *filenames)
    settings.append_lines(
        '__init__', '"""',
        'Modularized settings generated by django Organice setup. http://organice.io',
        'This solution follows the second recommendation from',
        'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/',
        '"""', 'from .develop import *  # noqa')
    for prof in profiles:
        settings.append_lines(
            prof,
            '# Django project settings for %s environment' % prof.capitalize(),
            '', 'from .common import *  # noqa')

    # out-of-the-box Django values relevant for deployment
    settings.delete_var('common', 'SITE_ID')
    settings.insert_lines('common', '_ = lambda s: s', '', 'SITE_ID = 1')
    settings.replace_line('common', 'import os',
                          'from os.path import abspath, dirname, join')
    settings.set_value('common', 'BASE_DIR',
                       'dirname(dirname(dirname(abspath(__file__))))')
    settings.set_value('common', 'MEDIA_ROOT',
                       "join(BASE_DIR, '%s.media')" % projectname)
    settings.set_value('common', 'STATIC_ROOT',
                       "join(BASE_DIR, '%s.static')" % projectname)
    settings.set_value('common', 'MEDIA_URL', "'/media/'")
    settings.set_value('common', 'USE_I18N', False)
    settings.move_var('common', profiles, 'DEBUG')
    settings.move_var('common', profiles, 'ALLOWED_HOSTS')
    settings.move_var('common', profiles, 'DATABASES')
    settings.move_var('common', profiles, 'MEDIA_ROOT')
    settings.move_var('common', profiles, 'STATIC_ROOT')
    settings.move_var('common', profiles, 'SECRET_KEY')
    for prof in ('staging', 'production'):
        settings.set_value(prof, 'DEBUG', False)
        settings.set_value_lines(
            prof, 'ALLOWED_HOSTS', '[', "    '%s.organice.io'," %
            (args.account if args.account else projectname),
            "    '%s'," % (args.domain if args.domain else 'www.example.com'),
            ']')
Beispiel #2
0
def test_create_settings():
    """Create a settings module with several submodules."""
    project = 'test_project_settings'
    modules = ('__init__', 'foo', 'bar', 'baz')

    settings = DjangoSettingsManager(project, *modules)
    for module in modules:
        thefile = settings.get_file(module)
        assert os.path.isfile(
            thefile.name), "File not created: %s" % thefile.name
def _split_project():
    global args
    global profiles
    global settings

    profiles = ('develop', 'staging', 'production')
    filenames = ('__init__', 'common') + profiles

    _print_verbose(2, 'Creating directories ...')
    os.mkdir('%s.media' % args.projectname)
    os.mkdir('%s.static' % args.projectname)
    os.mkdir('%s.templates' % args.projectname)
    os.mkdir(os.path.join(args.projectname, 'settings'))

    _print_verbose(
        2, 'Converting settings to deployment profiles (%s) ...' %
        ', '.join(profiles))
    os.rename(os.path.join(args.projectname, 'settings.py'),
              os.path.join(args.projectname, 'settings', 'common.py'))

    settings = DjangoSettingsManager(args.projectname, *filenames)
    settings.append_lines(
        '__init__', '"""',
        'Modularized settings generated by django Organice setup. http://organice.io',
        'This solution follows the second recommendation from',
        'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/',
        '"""', 'from .develop import *  # noqa')
    for prof in profiles:
        settings.append_lines(
            prof,
            '# Django project settings for %s environment' % prof.capitalize(),
            '', 'from .common import *  # noqa')

    # out-of-the-box Django values relevant for deployment
    settings.delete_var('common', 'SITE_ID')
    settings.insert_lines(
        'common', 'from django.utils.translation import ugettext_lazy as _',
        '', 'SITE_ID = 1')
    settings.replace_line('common', 'import os', '')
    settings.insert_lines('common',
                          'from os.path import abspath, dirname, join')
    settings.set_value('common', 'BASE_DIR',
                       'dirname(dirname(dirname(abspath(__file__))))')
    settings.set_value('common', 'MEDIA_ROOT',
                       "join(BASE_DIR, '%s.media')" % args.projectname)
    settings.set_value('common', 'STATIC_ROOT',
                       "join(BASE_DIR, '%s.static')" % args.projectname)
    settings.set_value('common', 'MEDIA_URL', "'/media/'")
    settings.set_value('common', 'USE_I18N', True)
    settings.set_value('common', 'USE_L10N', True)
    settings.move_var('common', profiles, 'DEBUG')
    settings.move_var('common', profiles, 'ALLOWED_HOSTS')
    settings.append_lines(
        'develop', "DEVELOP_APPS = (", "    'behave_django',", ")", "", "try:",
        "    from importlib import import_module",
        "    for pkg in DEVELOP_APPS:", "        import_module(pkg)",
        "    INSTALLED_APPS += DEVELOP_APPS", "except ImportError:",
        "    from warnings import warn",
        "    warn('Development packages missing. Please run `make develop`', Warning)"
    )
    settings.move_var('common', profiles, 'DATABASES')
    settings.move_var('common', profiles, 'MEDIA_ROOT')
    settings.move_var('common', profiles, 'STATIC_ROOT')
    settings.move_var('common', profiles, 'SECRET_KEY')

    account_domain = '%s.organice.io' % args.account
    for prof in ('staging', 'production'):
        settings.set_value(prof, 'DEBUG', False)
        settings.set_value_lines(
            prof, 'ALLOWED_HOSTS', '[',
            "    '%s'," % (args.domain if args.domain else account_domain),
            "    '%s'," %
            (account_domain if args.domain else 'www.%s' % account_domain),
            ']')