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