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