File: settings.py

package info (click to toggle)
django-testproject 0.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 132 kB
  • sloc: python: 126; makefile: 9
file content (131 lines) | stat: -rw-r--r-- 4,361 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of django-testproject.
#
# django-testproject is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-testproject is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-testproject.  If not, see <http://www.gnu.org/licenses/>.

"""
Settings generator for test projects
"""

import inspect
import os


DJANGO_TESTPROJECT_DIR = os.path.abspath(os.path.dirname(__file__))


def _get_default_settings(project_dir):
    """
    Produce default settings
    """
    DEBUG = True
    TEMPLATE_DEBUG = DEBUG
    ADMINS = (
        #( 'Your name', 'email@example.org'),
    )
    MANAGERS = ADMINS
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(project_dir, 'test.db')
        }
    }
    TIME_ZONE = 'UTC'
    LANGUAGE_CODE = 'en-us'
    SITE_ID = 1
    USE_I18N = True
    USE_L10N = True
    MEDIA_ROOT = ''
    MEDIA_URL = ''
    STATIC_ROOT = ''
    STATIC_URL = '/static/'
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )
    SECRET_KEY = '00000000000000000000000000000000000000000000000000'
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )
    TEMPLATE_DIRS = (
        os.path.join(project_dir, "templates"),
        os.path.join(DJANGO_TESTPROJECT_DIR, "templates")
    )
    ROOT_URLCONF = ''
    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
    )

    TEMPLATE_CONTEXT_PROCESSORS = (
        "django.contrib.auth.context_processors.auth",
        "django.core.context_processors.debug",
        "django.core.context_processors.i18n",
        "django.core.context_processors.media",
        "django.contrib.messages.context_processors.messages")
    return locals()


def gen_settings(**kwargs):
    """
    Generate settings for test project

    The settings will work for django 1.1.x and 1.2.x

    You may provide any additional settings with keyword arguments, they
    will be merged with generated settings.
    """
    # Find project_dir by inspecting caller
    frame = inspect.currentframe()
    outer_frames = inspect.getouterframes(frame)
    caller = outer_frames[1][0]
    project_dir = os.path.dirname(
        os.path.abspath(
            inspect.getsourcefile(caller)))

    # Default settings
    settings = _get_default_settings(project_dir)

    # Merge with user provided defaults
    for key, value in kwargs.items():
        if key not in settings:
            new_value = value
        elif isinstance(settings[key], (list, tuple)) and isinstance(value, (list, tuple)):
            # Merge lists
            new_value = list(settings[key]) + list(value)
        elif isinstance(settings[key], (int, float, bool, str)) and isinstance(value, (int, float, bool, str)):
            # Overwrite simple types
            new_value = value
        else:
            raise ValueError("Don't know how to merge custom setting %r that already exists in generated settings" % key)
        settings[key] = new_value

    # Crude django_coverage integration
    try:
        import django_coverage
        settings['INSTALLED_APPS'].insert(0, 'django_coverage')
        settings['COVERAGE_REPORT_HTML_OUTPUT_DIR'] = os.getenv("COVERAGE_REPORT_HTML_OUTPUT_DIR")
        settings['COVERAGE_MODULE_EXCLUDES'] = []
    except ImportError:
        pass

    # Return settings back to the caller
    return settings