Anonymous avatar Anonymous committed 51b02f5

Finishing up the new features

Comments (0)

Files changed (9)

 
 Manage different settings for different locations (systems), like
 local (your machine), development (shared development system), test,
-staging/acceptance and production.
+staging/acceptance and production, with "maximum DRY".
 
 Or, more generically: be able to refactor your settings as you like,
 by simply "inheriting" from more generic settings using 'from ...
 import'.
 
+Compatibility with Python < 2.6
+===============================
+In the example settings files, 'from .. import *' is used. You will
+need to change this to ``from <project>.settings import *`` for older
+versions of Python. The downside is that you will have to include
+the project name in your settings, which is a violation of the DRY
+principle that django-environments tries to live by.
+
 Compatibility with virtualenv
 =============================
 
 
  1. Copy scripts/initenv_example to scripts/initenv.
  2. Edit initenv and set ``PROJECT_ROOT``, ``DJANGO_PROJECT`` (always a
-    subdirectory of ``PROJECT_ROOT``) and ``PROJECT_SETTINGS``.
+    subdirectory of ``PROJECT_ROOT``) and ``DJANGO_SETTINGS``.
  3. Activate the environment using ``source scripts/initenv``.
  4. When using virtualenv with virtualenvwrapper, you can do either
     ``source <path-to-project>/scripts/initenv`` from ``bin/postactivate``,
     or simply use your initenv's contents inside postactivate. 
-    Alternatively, you may also symlink ``bin/postactivate`` to a custom
-    version of this file.
+    Alternatively, you may also symlink ``bin/postactivate`` to the
+    initenv script.
 
 If everything works okay, the following shell aliases are created:
 
 settings.env.staging is used. Create other .wsgi files for other environment
 settings.
 
+You can add specific site packages to 
+
 Automatic generation of local wsgi links and settings file
 ==========================================================
 
 This will create a symbolic link deploy/local.wsgi to staging.wsgi and
 will create a settings/env/local.py with default contents for a given
 environment. Now, you only need to update settings.env.local with those
-settings you want to keep local, like user ids and passwords. Keep in
-mind the script will overwrite exiting local.py settings files!
+settings you want to keep absolutely local, like those containing
+user ids and passwords. The local.py file will be pyc-compiled
+automatically. Keep in mind the script will overwrite exiting
+local.py settings files!
 
 Directories
 ===========

mysite/deploy/development.wsgi

+# Copy this file to <environment>.wsgi to automatically use settings.env.<environment>
+# Example Apache configuration:
+# WSGIScriptAlias / /Users/spanky/repos/django-environments/mysite/deploy/local.wsgi
+
+# These will be prepended to the front of sys.path, so great for
+# virtual environments:
+site_packages = (
+    #'/var/virtualenvs/mysite/lib/python2.6/site-packages',
+)
+
+import os, site, sys
+import site
+
+# Remember original sys.path
+prev_sys_path = list(sys.path) 
+
+# Add each new site-packages directory
+for directory in site_packages:
+  site.addsitedir(directory)
+
+# Reorder sys.path so new directories are at the front
+new_sys_path = [] 
+for item in list(sys.path): 
+    if item not in prev_sys_path: 
+        new_sys_path.append(item) 
+        sys.path.remove(item) 
+sys.path[:0] = new_sys_path 
+
+# Now we can import from django
+import django.core.handlers.wsgi
+
+# Find out where this WSGI script is
+wsgi_dir = os.path.dirname(__file__)
+# Calculate the path based on the location of the WSGI script
+django_project = os.path.dirname(wsgi_dir)
+project_root = os.path.dirname(django_project)
+
+# Add paths
+sys.path.insert(0, project_root)
+sys.path.insert(0, django_project)
+
+os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.env.' + \
+    os.path.basename(__file__).split('.')[0]
+application = django.core.handlers.wsgi.WSGIHandler()

mysite/deploy/example.wsgi

-# Copy this file to <environment>.wsgi to automatically use settings.env.<environment>
-# Example Apache configuration:
-# WSGIPythonHome /var/virtualenvs/development # Optional
-# WSGIScriptAlias / /Users/spanky/repos/django-environments/mysite/deploy/local.wsgi
-
-import os, site, sys
-import django.core.handlers.wsgi
-
-# Find out where this WSGI script is...
-wsgi_dir = os.path.dirname(__file__)
-# Calculate the path based on the location of the WSGI script
-django_project = os.path.dirname(wsgi_dir)
-project_root = os.path.dirname(django_project)
-
-# Add paths
-sys.path.insert(0, project_root)
-sys.path.insert(0, django_project)
-
-os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.env.' + os.path.basename(__file__).split('.')[0]
-application = django.core.handlers.wsgi.WSGIHandler()

mysite/settings/env/development.py

 from .. import *
-from os.path import basename
+from os import path
 
 
-try:
-    CACHE_MIDDLEWARE_KEY_PREFIX += '.' + basename(__file__).split('.')[0]
-except NameError:
-    from os import environ
-    CACHE_MIDDLEWARE_KEY_PREFIX = environ['DJANGO_PROJECT'] + '.' + \
-                                  basename(__file__).split('.')[0]
-
-
-DATABASE_ENGINE = 'postgresql'
+DATABASE_ENGINE = 'sqlite3'
 DATABASE_HOST = ''
 DATABASE_NAME = ''
 DATABASE_USER = ''

mysite/settings/env/production_generic.py

 from .. import *
-from os.path import basename
+from os import path
 
 
-try:
-    CACHE_MIDDLEWARE_KEY_PREFIX += '.' + basename(__file__).split('.')[0]
-except NameError:
-    from os import environ
-    CACHE_MIDDLEWARE_KEY_PREFIX = environ['DJANGO_PROJECT'] + '.' + \
-                                  basename(__file__).split('.')[0]
-CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
-CACHE_MIDDLEWARE_SECONDS = 86400
-CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
-
 DATABASE_ENGINE = 'postgresql'
 DATABASE_HOST = ''
 DATABASE_NAME = ''
 ROSETTA_WSGI_AUTO_RELOAD = False
 
 GOOGLE_ANALYTICS_CODE = 'UA-XXX'
+
+CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
+CACHE_MIDDLEWARE_SECONDS = 86400
+CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
+
+try:
+    CACHE_MIDDLEWARE_KEY_PREFIX += '.' + path.basename(__file__).split('.')[0]
+except NameError:
+    from os import environ
+
+    # Find out where this settings file is
+    split_path = __file__.split(path.sep)
+    # Calculate the path based on the location of this settings file
+    django_project = split_path[-4]
+    project_root = split_path[-5]
+
+    CACHE_MIDDLEWARE_KEY_PREFIX = project_root + '.' +  django_project + '.' + \
+                                  split_path[-1].split('.')[0]

mysite/settings/env/staging.py

 from .. import *
-from os.path import basename
+from os import path
 
 
-try:
-    CACHE_MIDDLEWARE_KEY_PREFIX += '.' + basename(__file__).split('.')[0]
-except NameError:
-    from os import environ
-    CACHE_MIDDLEWARE_KEY_PREFIX = environ['DJANGO_PROJECT'] + '.' + \
-                                  basename(__file__).split('.')[0]
-CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
-CACHE_MIDDLEWARE_SECONDS = 86400
-CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
-
 DATABASE_ENGINE = 'postgresql'
 DATABASE_HOST = ''
 DATABASE_NAME = ''
 ROSETTA_WSGI_AUTO_RELOAD = True
 
 GOOGLE_ANALYTICS_CODE = 'UA-XXX'
+
+CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
+CACHE_MIDDLEWARE_SECONDS = 60
+CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
+
+try:
+    CACHE_MIDDLEWARE_KEY_PREFIX += '.' + path.basename(__file__).split('.')[0]
+except NameError:
+    from os import environ
+
+    # Find out where this settings file is
+    split_path = __file__.split(path.sep)
+    # Calculate the path based on the location of this settings file
+    django_project = split_path[-4]
+    project_root = split_path[-5]
+
+    CACHE_MIDDLEWARE_KEY_PREFIX = project_root + '.' +  django_project + '.' + \
+                                  split_path[-1].split('.')[0]

mysite/settings/env/test.py

-from .. import *
-from os.path import basename
-
-
-try:
-    CACHE_MIDDLEWARE_KEY_PREFIX += '.' + basename(__file__).split('.')[0]
-except NameError:
-    from os import environ
-    CACHE_MIDDLEWARE_KEY_PREFIX = environ['DJANGO_PROJECT'] + '.' + \
-                                  basename(__file__).split('.')[0]
-#CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
-#CACHE_MIDDLEWARE_SECONDS = 0
-#CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
-
-DATABASE_ENGINE = 'mysql'
-DATABASE_HOST = ''
-DATABASE_NAME = ''
-DATABASE_USER = ''
-DATABASE_PASSWORD = ''
-
-DEBUG = False
-
-SERVE_MEDIA = False
-
-COMPRESS = True
-
-SEND_BROKEN_LINK_EMAILS = False
-
-ROSETTA_WSGI_AUTO_RELOAD = True
     #(r'^', include('main.urls')),
 
     #$(r'^admin/doc/', include('django.contrib.admindocs.urls')),
-    (r'^admin/', include(admin.site.urls)),
+    #(r'^admin/', include(admin.site.urls)),
 
     #$(r'^admin/filebrowser/', include('filebrowser.urls')),
     #$(r'^admin/rosetta/', include('rosetta.urls')),

scripts/initenv_example

 
 PROJECT_ROOT=/Users/grone/repos/django-environments
 DJANGO_PROJECT=mysite
-PROJECT_SETTINGS=settings.env.local
+DJANGO_SETTINGS=settings.env.development
 SHOW_DJANGO_ENVIRONMENT=yes
 
 source $PROJECT_ROOT/scripts/initenv_generic
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.