11 de noviembre de 2010

setting.py configurable por host y sistema operativo

Para un desarrollo de una aplicación Django donde intervengan varios colaboradores y en diferentes entornos de trabajo podemos trucar un poco el fichero setting.py.

Veamos como, poniendo un pequeño ejemplo.
  • Tenemos dos colaboradores b3ni y alex en el mismo proyecto.
  • Ambos colaboradores pueden desarrollar en diferentes entornos: linux/windows
  • Tenemos otro caso cuando el proyecto se ejecute en preproducción o producción.
Para que los desarrolladores puedan programar cómodamente y el proyecto pueda ser funcional en cualquier entorno, utilizaremos:

socket.gethostname()

Para identificar el host / usuario.

os.name

Para identificar la plataforma.

  • nt: Windows
  • posix: Linux

Ahora solo necesitamos encajarlo todo.

# diferentes tipos de configuraciones
import socket
import os

name_host = socket.gethostname()
name_os = os.name

if name_host == 'b3ni' or 'b3ni-laptop':
    # desarrollo en sobremesa y portatil
    # ----------------------------------
    DEBUG = True
    DIR_BASE = {'nt': r"C:/Users/b3ni/ws/project", 
                'posix': '/home/b3ni/ws/project'}[name_os]

    DATABASES = {
        'default': {
            'ENGINE':   'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME':     DIR_BASE + r'/data.db', # Or path to database file if using sqlite3.
            'USER':     '', # Not used with sqlite3.
            'PASSWORD': '', # Not used with sqlite3.
            'HOST':     '', # Set to empty string for localhost. Not used with sqlite3.
            'PORT':     '', # Set to empty string for default. Not used with sqlite3.
        }
    }
    
    MEDIA_ROOT = DIR_BASE + r'/media'
    TEMPLATE_DIRS = (DIR_BASE + r"/templates/",)

elif name_host == 'alex':
    # desarrollo alex
    
    DEBUG = True
    DIR_BASE = {'nt': r"C:/Users/alex/workspace/dummy/project", 'posix': '/home/alex/workspace/dummy/project'}[name_os]

    DATABASES = {
        'default': {
            'ENGINE':   'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME':     DIR_BASE + r'/data.db', # Or path to database file if using sqlite3.
            'USER':     '', # Not used with sqlite3.
            'PASSWORD': '', # Not used with sqlite3.
            'HOST':     '', # Set to empty string for localhost. Not used with sqlite3.
            'PORT':     '', # Set to empty string for default. Not used with sqlite3.
        }
    }
    
    MEDIA_ROOT = DIR_BASE + r'/media'
    TEMPLATE_DIRS = (DIR_BASE + r"/templates/",)
    
else:
    # PRODUCCION
    # ----------
    DEBUG = False

    DATABASE_ENGINE =   'mysql'
    DATABASE_NAME =     'xxx'
    DATABASE_USER =     'xxx'             # Not used with sqlite3.
    DATABASE_PASSWORD = 'xxx'             # Not used with sqlite3.
    DATABASE_HOST =     ''             # Set to empty string for localhost. Not used with sqlite3.
    DATABASE_PORT =     ''             # Set to empty string for default. Not used with sqlite3.

    EMAIL_HOST = 'xxx'
    EMAIL_HOST_PASSWORD = 'xxx'
    EMAIL_HOST_USER = 'xx'

    MEDIA_ROOT = '/server/project/media'
    TEMPLATE_DIRS = ('/server/project/templates/',)
    
# RESTO DE CONFIGURACION DJANGO .....

No hay comentarios:

Publicar un comentario