Allow configuring additional database settings so we can use postgres.
[htsworkflow.git] / htsworkflow / settings.py
1 """
2 Generate settings for the Django Application.
3
4 To make it easier to customize the application the settings can be 
5 defined in a configuration file read by ConfigParser.
6
7 The options understood by this module are (with their defaults):
8
9   [frontend]
10   email_host=localhost
11   email_port=25
12   database_engine=sqlite3
13   database_name=/path/to/db
14
15   [admins]
16   #name1=email1
17
18   [allowed_hosts]
19   #name1=ip
20   localhost=127.0.0.1
21   
22   [allowed_analysis_hosts]
23   #name1=ip
24   localhost=127.0.0.1
25
26 """
27 import ConfigParser
28 import os
29 import shlex
30 import htsworkflow
31
32 HTSWORKFLOW_ROOT = os.path.abspath(os.path.split(htsworkflow.__file__)[0])
33
34 # make epydoc happy
35 __docformat__ = "restructuredtext en"
36
37 def options_to_list(options, dest, section_name, option_name):
38   """
39   Load a options from section_name and store in a dictionary
40   """
41   if options.has_option(section_name, option_name):
42     opt = options.get(section_name, option_name)
43     dest.extend( shlex.split(opt) )
44       
45 def options_to_dict(dest, section_name):
46   """
47   Load a options from section_name and store in a dictionary
48   """
49   if options.has_section(section_name):
50     for name in options.options(section_name):
51       dest[name] = options.get(section_name, name)
52
53 # define your defaults here
54 options = ConfigParser.SafeConfigParser(
55            { 'email_host': 'localhost',
56              'email_port': '25', 
57              'database_engine': 'sqlite3',
58              'database_name': 
59                   os.path.join(HTSWORKFLOW_ROOT, '..', 'fctracker.db'),
60              'time_zone': 'America/Los_Angeles',
61              'default_pm': '5',
62              'link_flowcell_storage_device_url': "http://localhost:8000/inventory/lts/link/",
63              'printer1_host': '127.0.0.1',
64              'printer1_port': '9100',
65              'printer2_host': '127.0.0.1',
66              'printer2_port': '9100',
67            })
68
69 options.read([os.path.expanduser("~/.htsworkflow.ini"),
70               '/etc/htsworkflow.ini',])
71
72 # OptionParser will use the dictionary passed into the config parser as
73 # 'Default' values in any section. However it still needs an empty section
74 # to exist in order to retrieve anything.
75 if not options.has_section('frontend'):
76     options.add_section('frontend')
77 if not options.has_section('bcprinter'):
78     options.add_section('bcprinter')
79
80
81 # Django settings for elandifier project.
82
83 DEBUG = True
84 TEMPLATE_DEBUG = DEBUG
85
86 ADMINS = []
87 options_to_list(options, ADMINS, 'frontend', 'admins')
88
89 MANAGERS = []
90 options_to_list(options, MANAGERS, 'frontend', 'managers')
91
92 DEFAULT_PM=int(options.get('frontend', 'default_pm'))
93
94 AUTHENTICATION_BACKENDS = ( 
95   'htsworkflow.frontend.samples.auth_backend.HTSUserModelBackend', )
96 CUSTOM_USER_MODEL = 'samples.HTSUser' 
97
98 EMAIL_HOST = options.get('frontend', 'email_host')
99 EMAIL_PORT = int(options.get('frontend', 'email_port'))
100
101 if options.has_option('frontend', 'notification_sender'):
102     NOTIFICATION_SENDER = options.get('frontend', 'notification_sender')
103 else:
104     NOTIFICATION_SENDER = "noreply@example.com"
105 NOTIFICATION_BCC = []
106 options_to_list(options, NOTIFICATION_BCC, 'frontend', 'notification_bcc')
107
108 database_section = options.get('frontend', 'database', 'database')
109
110 if not options.has_section(database_section):
111     raise ConfigParser.NoSectionError(
112         "No database=<database_section_name> defined")
113     
114 # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
115 DATABASE_ENGINE = options.get(database_section, 'engine')
116 DATABASE_NAME = options.get(database_section, 'name')
117 if options.has_option(database_section, 'user'):
118     DATABASE_USER = options.get(database_section, 'user')
119 if options.has_option(database_section, 'host'):
120     DATABASE_HOST = options.get(database_section, 'host')
121 if options.has_option(database_section, 'port'):
122     DATABASE_PORT = options.get(database_section, 'port')
123
124 if options.has_option(database_section, 'password_file'):
125     password_file = options.get(database_section, 'password_file')
126     DATABASE_PASSWORD = open(password_file,'r').readline()
127 elif options.has_option(database_section, 'password'):
128     DATABASE_PASSWORD = options.get(database_section, 'password')
129
130 # Local time zone for this installation. Choices can be found here:
131 # http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
132 # although not all variations may be possible on all operating systems.
133 # If running in a Windows environment this must be set to the same as your
134 # system time zone.
135 TIME_ZONE = options.get('frontend', 'time_zone')
136
137 # Language code for this installation. All choices can be found here:
138 # http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
139 # http://blogs.law.harvard.edu/tech/stories/storyReader$15
140 LANGUAGE_CODE = 'en-us'
141
142 SITE_ID = 1
143
144 # If you set this to False, Django will make some optimizations so as not
145 # to load the internationalization machinery.
146 USE_I18N = True
147
148 # Absolute path to the directory that holds media.
149 # Example: "/home/media/media.lawrence.com/"
150 MEDIA_ROOT = os.path.join(HTSWORKFLOW_ROOT, 'frontend', 'static', '')
151
152 # URL that handles the media served from MEDIA_ROOT.
153 # Example: "http://media.lawrence.com"
154 MEDIA_URL = '/static/'
155
156 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
157 # trailing slash.
158 # Examples: "http://foo.com/media/", "/media/".
159 ADMIN_MEDIA_PREFIX = '/media/'
160
161 # Make this unique, and don't share it with anybody.
162 SECRET_KEY = '(ekv^=gf(j9f(x25@a7r+8)hqlz%&_1!tw^75l%^041#vi=@4n'
163
164 # some of our urls need an api key
165 DEFAULT_API_KEY = 'n7HsXGHIi0vp9j5u4TIRJyqAlXYc4wrH'
166
167 # List of callables that know how to import templates from various sources.
168 TEMPLATE_LOADERS = (
169     'django.template.loaders.filesystem.load_template_source',
170     'django.template.loaders.app_directories.load_template_source',
171 #     'django.template.loaders.eggs.load_template_source',
172 )
173
174 MIDDLEWARE_CLASSES = (
175     'django.middleware.common.CommonMiddleware',
176     'django.contrib.sessions.middleware.SessionMiddleware',
177     'django.contrib.auth.middleware.AuthenticationMiddleware',
178     'django.middleware.doc.XViewMiddleware',
179 )
180
181 ROOT_URLCONF = 'htsworkflow.frontend.urls'
182
183 TEMPLATE_DIRS = (
184     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
185     # Always use forward slashes, even on Windows.
186     # Don't forget to use absolute paths, not relative paths.
187     '/usr/share/python-support/python-django/django/contrib/admin/templates',
188     #'/usr/lib/pymodules/python2.6/django/contrib/admin/templates/',
189     os.path.join(HTSWORKFLOW_ROOT, 'frontend','templates'),
190 )
191
192 INSTALLED_APPS = (
193     'django.contrib.admin',
194     'django.contrib.auth',
195     'django.contrib.contenttypes',
196     'django.contrib.humanize',
197     'django.contrib.sessions',
198     'django.contrib.sites',
199     'htsworkflow.frontend.eland_config',
200     'htsworkflow.frontend.samples',
201     # modules from htsworkflow branch
202     'htsworkflow.frontend.experiments',
203     'htsworkflow.frontend.analysis', 
204     'htsworkflow.frontend.reports',
205     'htsworkflow.frontend.inventory',
206     'htsworkflow.frontend.bcmagic',
207     'django.contrib.databrowse',
208 )
209
210 # Project specific settings
211
212 ALLOWED_IPS={'127.0.0.1': '127.0.0.1'}
213 options_to_dict(ALLOWED_IPS, 'allowed_hosts')
214
215 ALLOWED_ANALYS_IPS = {'127.0.0.1': '127.0.0.1'}
216 options_to_dict(ALLOWED_ANALYS_IPS, 'allowed_analysis_hosts')
217 #UPLOADTO_HOME = os.path.abspath('../../uploads')
218 #UPLOADTO_CONFIG_FILE = os.path.join(UPLOADTO_HOME, 'eland_config')
219 #UPLOADTO_ELAND_RESULT_PACKS = os.path.join(UPLOADTO_HOME, 'eland_results')
220 #UPLOADTO_BED_PACKS = os.path.join(UPLOADTO_HOME, 'bed_packs')
221 # Where "results_dir" means directory with all the flowcells
222 if options.has_option('frontend', 'results_dir'):
223     RESULT_HOME_DIR=os.path.expanduser(options.get('frontend', 'results_dir'))
224 else:
225     RESULT_HOME_DIR='/tmp'
226
227 LINK_FLOWCELL_STORAGE_DEVICE_URL = options.get('frontend', 'link_flowcell_storage_device_url')
228 # PORT 9100 is default for Zebra tabletop/desktop printers
229 # PORT 6101 is default for Zebra mobile printers
230 BCPRINTER_PRINTER1_HOST = options.get('bcprinter', 'printer1_host')
231 BCPRINTER_PRINTER1_PORT = int(options.get('bcprinter', 'printer1_port'))
232 BCPRINTER_PRINTER2_HOST = options.get('bcprinter', 'printer2_host')
233 BCPRINTER_PRINTER2_PORT = int(options.get('bcprinter', 'printer2_port'))
234