annotate bns_website/apache/bns.wsgi @ 27:a5e8741452a3

Add path to Django in WSGI file. Use Django's simplesjon module as it will either import the Python system one if it exists, or use the one provided with Django as a fallback. This is needed for the production server, which is running Python 2.5.
author Brian Neal <bgneal@gmail.com>
date Tue, 01 Nov 2011 20:15:21 -0500
parents 6f68f6800843
children f00199f1524c
rev   line source
bgneal@19 1 import os
bgneal@19 2 import sys
bgneal@19 3
bgneal@19 4 OFFLINE = False
bgneal@19 5
bgneal@19 6 here = os.path.dirname(__file__)
bgneal@19 7 website = os.path.dirname(here)
bgneal@26 8 repo = os.path.dirname(website)
bgneal@26 9 project = os.path.dirname(repo)
bgneal@19 10 eggs = os.path.join(project, 'eggs')
bgneal@27 11 django = os.path.join(project, 'django')
bgneal@19 12
bgneal@27 13 sys.path.extend([repo, website, django])
bgneal@19 14
bgneal@19 15 os.environ['PYTHON_EGG_CACHE'] = eggs
bgneal@19 16
bgneal@19 17
bgneal@19 18 def offline_handler(environ, start_response):
bgneal@19 19 """
bgneal@19 20 This handler is run when the site is in maintenance mode.
bgneal@19 21 It either displays a brief outage message, or, if a template
bgneal@19 22 file exists, it reads the file and returns its contents.
bgneal@19 23
bgneal@19 24 """
bgneal@19 25 wsgi_dir = os.path.dirname(__file__)
bgneal@19 26 sys.path.append(wsgi_dir)
bgneal@19 27
bgneal@19 28 offline_file = os.path.abspath(
bgneal@19 29 os.path.join(wsgi_dir, '..', 'templates', 'offline.html'))
bgneal@19 30 if os.path.exists(offline_file):
bgneal@19 31 response_headers = [('Content-type','text/html')]
bgneal@19 32 response = open(offline_file).read()
bgneal@19 33 else:
bgneal@19 34 response_headers = [('Content-type','text/plain')]
bgneal@19 35 response = ('Brave New Surf website maintenance in progress; '
bgneal@19 36 'please check back soon.')
bgneal@19 37
bgneal@19 38 if environ['REQUEST_METHOD'] == 'GET':
bgneal@19 39 status = '503 Service Unavailable'
bgneal@19 40 else:
bgneal@19 41 status = '405 Method Not Allowed'
bgneal@19 42 start_response(status, response_headers)
bgneal@19 43 return [response]
bgneal@19 44
bgneal@19 45
bgneal@19 46 if not OFFLINE:
bgneal@19 47 os.environ['DJANGO_SETTINGS_MODULE'] = 'bns_website.settings.production'
bgneal@19 48 import django.core.handlers.wsgi
bgneal@19 49 application = django.core.handlers.wsgi.WSGIHandler()
bgneal@19 50 else:
bgneal@19 51 application = offline_handler