Skip to content

Commit

Permalink
Merge pull request #87 from nephila/fix/settings
Browse files Browse the repository at this point in the history
Better support for different styles of django version settings. Fix #85.
  • Loading branch information
yakky committed Apr 8, 2014
2 parents 730e906 + 171e274 commit 0195093
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion djangocms_installer/config/__init__.py
Expand Up @@ -18,7 +18,7 @@ def parse(args):
"""
parser = argparse.ArgumentParser(description='Bootstrap a django CMS project.')
parser.add_argument('--db', '-d', dest='db', action=DbAction,
default='sqlite://locahost/project.db',
default='sqlite://localhost/project.db',
help='Database configuration (in URL format)')
parser.add_argument('--i18n', '-i', dest='i18n', action='store',
choices=('yes', 'no'),
Expand Down
14 changes: 5 additions & 9 deletions djangocms_installer/config/data.py
Expand Up @@ -94,16 +94,12 @@
DEFAULT_PROJECT_HEADER = """# -*- coding: utf-8 -*-
import os
gettext = lambda s: s
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
"""

STATIC_FILES = """
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'static'),
)
"""
STATICFILES_DEFAULT = """STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)"""

BASE_DIR = """
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Expand Down
28 changes: 20 additions & 8 deletions djangocms_installer/django/__init__.py
Expand Up @@ -82,18 +82,29 @@ def patch_settings(config_data):
original = fd_original.read()

original = original.replace("# -*- coding: utf-8 -*-\n", "")
original = data.DEFAULT_PROJECT_HEADER + original

if original.find('BASE_DIR') == -1:
original += data.BASE_DIR
original = data.DEFAULT_PROJECT_HEADER + data.BASE_DIR + original
else:
original = data.DEFAULT_PROJECT_HEADER + original
if original.find('MEDIA_URL') > -1:
original = original.replace("MEDIA_URL = ''", "MEDIA_URL = '/media/'")
else:
original += "MEDIA_URL = '/media/'\n"
if original.find('MEDIA_ROOT') > -1:
original = original.replace("MEDIA_ROOT = ''", "MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')")
original = original.replace("MEDIA_ROOT = ''", "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')")
else:
original += "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')\n"
if original.find('STATIC_ROOT') > -1:
original = original.replace("STATIC_ROOT = ''", "STATIC_ROOT = os.path.join(BASE_DIR, 'static')")
else:
original += "STATIC_ROOT = os.path.join(BASE_DIR, 'static')"
if original.find('STATICFILES_DIRS') > -1:
original = original.replace(data.STATICFILES_DEFAULT, """STATICFILES_DEFAULT = (
os.path.join(BASE_DIR, '%s', 'static'),
)""" % config_data.project_name)
else:
original += "MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')\n"
original += data.STATIC_FILES
original += "STATICFILES_DIRS = os.path.join(BASE_DIR, 'static')"
original = original.replace("# -*- coding: utf-8 -*-\n", "")

# I18N
Expand All @@ -116,7 +127,8 @@ def patch_settings(config_data):
# DATABASES is a dictionary, so different regexp needed
item_re = re.compile(r"DATABASES = [^\}]+\}[^\}]+\}", re.DOTALL | re.MULTILINE)
original = item_re.sub('', original)
original += "SITE_ID = 1\n\n"
if original.find('SITE_ID') == -1:
original += "SITE_ID = 1\n\n"

original += _build_settings(config_data)

Expand Down Expand Up @@ -145,7 +157,7 @@ def _build_settings(config_data):
spacer, (",\n" + spacer).join(["'%s'" % var for var in vars.TEMPLATE_CONTEXT_PROCESSORS])))

text.append("TEMPLATE_DIRS = (\n%s%s\n)" % (
spacer, 'os.path.join(PROJECT_PATH, "templates"),'))
spacer, "os.path.join(BASE_DIR, '%s', 'templates')," % config_data.project_name))

apps = list(vars.INSTALLED_APPS)
if config_data.cms_version == 2.4:
Expand All @@ -167,7 +179,7 @@ def _build_settings(config_data):
if config_data.reversion:
apps.extend(vars.REVERSION_APPLICATIONS)
text.append("INSTALLED_APPS = (\n%s%s\n)" % (
spacer, (",\n" + spacer).join(["'%s'" % var for var in apps])))
spacer, (",\n" + spacer).join(["'%s'" % var for var in apps] + ["'%s'" % config_data.project_name])))

text.append("LANGUAGES = (\n%s%s\n%s%s\n)" % (
spacer, "## Customize this",
Expand Down
6 changes: 6 additions & 0 deletions tests/django.py
Expand Up @@ -5,6 +5,7 @@
import six
import sqlite3
from . import unittest
import re

from djangocms_installer import config, django, install
from . import BaseTestClass
Expand Down Expand Up @@ -66,6 +67,11 @@ def test_patch_16(self):
self.assertTrue('djangocms_teaser' in project.settings.INSTALLED_APPS)
self.assertTrue('djangocms_video' in project.settings.INSTALLED_APPS)

self.assertEqual(len(re.findall('BASE_DIR = ', settings)), 1)
self.assertEqual(len(re.findall('STATIC_ROOT', settings)), 1)
self.assertEqual(len(re.findall('MEDIA_ROOT =', settings)), 1)
self.assertEqual(len(re.findall('STATICFILES_DIRS', settings)), 1)

@unittest.skipIf(sys.version_info >= (3, 0),
reason="django CMS 2.4 does not support python3")
def test_patch_24_standard(self):
Expand Down

0 comments on commit 0195093

Please sign in to comment.