Changeset - cf5b073d44b0
[Not reviewed]
0 2 0
Brett Smith (brett) - 7 years ago 2016-12-06 02:43:51
brett@sfconservancy.org
urls: Route /error/NNN/ to static.views.handler.

Since our Apache configuration uses `ErrorDocument NNN /error/NNN/`,
this is necessary to return the correct status code in those cases.
2 files changed with 2 insertions and 1 deletions:
0 comments (0 inline, 0 general)
www/conservancy/static/views.py
Show inline comments
 
import mimetypes
 
import os.path
 
from django.http import HttpResponse
 
from django.template import RequestContext, loader
 
from conservancy.apps.fundgoal.models import FundraisingGoal as FundraisingGoal
 

	
 
STATIC_ROOT = os.path.abspath(os.path.dirname(__file__))
 
FILESYSTEM_ENCODING = 'utf-8'
 

	
 
def handler(request, errorcode):
 
    path = os.path.join('error', str(errorcode), 'index.html')
 
    fullpath = os.path.join(STATIC_ROOT, path)
 
    if not os.path.exists(fullpath):
 
        return HttpResponse("Internal error: " + path)
 
    template = loader.get_template(path)
 
    context = RequestContext(request)
 
    return HttpResponse(template.render(context), status=errorcode)
 
    return HttpResponse(template.render(context), status=int(errorcode))
 

	
 
def handler401(request):
 
    return handler(request, 401)
 

	
 
def handler403(request):
 
    return handler(request, 403)
 

	
 
def handler404(request):
 
    return handler(request, 404)
 

	
 
def handler500(request):
 
    return handler(request, 500)
 

	
 
def fundgoal_lookup(fundraiser_sought):
 
    try:
 
        return FundraisingGoal.objects.get(fundraiser_code_name=fundraiser_sought)
 
    except FundraisingGoal.DoesNotExist:
 
     # we have no object!  do something
 
        return None
 

	
 
def index(request, *args, **kwargs):
 
    path = request.path.lstrip(u'/')
 
    if path.endswith(u'/'):
 
        path += u'index.html'
www/conservancy/urls.py
Show inline comments
...
 
@@ -29,48 +29,49 @@ from conservancy.feeds import BlogFeed, PressReleaseFeed, OmnibusFeed
 
# import conservancy.static.overview.views
 

	
 
# handler404 = 'modpythoncustom.view404'
 
# handler401 = 'conservancy.static.views.handler401'
 
# handler403 = 'conservancy.static.views.handler403'
 
handler404 = 'conservancy.static.views.handler404'
 
# handler500 = 'conservancy.static.views.handler500'
 

	
 
admin.autodiscover()
 

	
 
urlpatterns = patterns('',
 
    (r'^$', 'conservancy.frontpage.view'),
 
    (r'^sponsors$', 'conservancy.frontpage.view'),
 
    (r'^sponsors/$', 'conservancy.sponsors.view'),
 
    (r'^sponsors/index.html$', 'conservancy.sponsors.view'),
 
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
    (r'^admin/', admin.site.urls),
 
    (r'^feeds/blog/?$', BlogFeed()),
 
    (r'^feeds/news/?$', PressReleaseFeed()),
 
    (r'^feeds/omnibus/?$', OmnibusFeed()),
 
    (r'^feeds/?$', 'conservancy.feeds.view'),
 
    (r'^news(/|$)', include('conservancy.apps.news.urls')),
 
    (r'^blog(/|$)', include('conservancy.apps.blog.urls')),
 
    # formerly static templated things... (dirs with templates)
 
    (r'^error/(40[134]|500)(?:/index\.html|/|)$', 'conservancy.static.views.handler'),
 
    (r'^error', 'conservancy.static.views.index'),
 
    (r'^about', 'conservancy.static.views.index'),
 
    (r'^donate', 'conservancy.static.views.index'),
 
    (r'^copyleft-compliance', 'conservancy.static.views.index',
 
                           {'fundraiser_sought' : 'vmware-match-0'}),
 
    (r'^projects', 'conservancy.static.views.index'),
 
    (r'^npoacct', 'conservancy.static.views.index',
 
                  {'fundraiser_sought' : 'npoacct'}),
 
    (r'^contractpatch', include('conservancy.apps.contractpatch.urls')),
 
    (r'^overview', 'conservancy.static.views.index'),
 
    (r'^privacy-policy', 'conservancy.static.views.index'),
 
    (r'^supporter', include('conservancy.apps.supporter.urls')),
 
    (r'^fundraiser_data', 'conservancy.apps.fundgoal.views.view'),
 
)
 

	
 
# urlpatterns += url(regex  = r'^%s(?P<path>.*)$' % conservancy.settings.STATIC_URL[1:],
 
# urlpatterns += url(regex  = r'^/overview',
 
#                    view   = 'django.views.static.serve',
 
#                    kwargs = {'document_root': conservancy.settings.STATIC_ROOT,
 
#                              'show_indexes' : True})
 
# urlpatterns += (r'^(?P<path>.*)$', 'django.views.static.serve', 
 
# urlpatterns += (r'^overview/$', 'django.views.static.serve', 
 
#                 {'document_root': conservancy.settings.STATIC_ROOT,
 
#                  'show_indexes' : True})
0 comments (0 inline, 0 general)