Fix error views to use base REST framework template instead of admin, since admin is disabled.

This commit is contained in:
Chris Church 2013-11-19 22:58:46 -05:00
parent ebd6973f6b
commit 52c0a93293
10 changed files with 73 additions and 83 deletions

View File

@ -4,20 +4,39 @@
# Django
from django.shortcuts import render
from django.template import RequestContext
from django.utils.safestring import mark_safe
def handle_error(request, status=404):
context = {}
def handle_error(request, status=404, **kwargs):
# FIXME: Should attempt to check HTTP Accept request header and return
# plain JSON response instead of HTML (maybe only for /api/*).
context = kwargs
if request.path.startswith('/admin/'):
template_name = 'admin/%d.html' % status
template_name = 'admin/error.html'
else:
template_name = '%d.html' % status
# Return enough context to popuplate the base API template.
description = u'<pre class="err">%s</pre>' % context.get('content', '')
context['description'] = mark_safe(description)
context['content'] = ''
template_name = 'error.html'
return render(request, template_name, context, status=status)
def handle_403(request):
return handle_error(request, 403)
kwargs = {
'name': 'Forbidden',
'content': 'You don\'t have permission to access the requested page.',
}
return handle_error(request, 403, **kwargs)
def handle_404(request):
return handle_error(request, 404)
kwargs = {
'name': 'Not Found',
'content': 'The requested page could not be found.',
}
return handle_error(request, 404, **kwargs)
def handle_500(request):
return handle_error(request, 500)
kwargs = {
'name': 'Server Error',
'content': 'A server error has occurred.',
}
return handle_error(request, 500, **kwargs)

View File

@ -1,7 +0,0 @@
{% extends "admin/403.html" %}
{% block branding_title %}AWX{% endblock %}
{% block breadcrumbs %}
{% endblock %}

View File

@ -1,6 +0,0 @@
{% extends "admin/404.html" %}
{% block branding_title %}AWX{% endblock %}
{% block breadcrumbs %}
{% endblock %}

View File

@ -1,6 +0,0 @@
{% extends "admin/500.html" %}
{% block branding_title %}AWX{% endblock %}
{% block breadcrumbs %}
{% endblock %}

View File

@ -1,20 +0,0 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block title %}{% trans "Forbidden" %}{% endblock %}
{% block nav-global %}{% endblock %}
{% block content_title %}{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans "Home" %}</a> &rsaquo;
{% trans "Forbidden" %}
</div>
{% endblock %}
{% block content %}
<h1 style="margin-bottom: 0.7em;">{% trans 'Forbidden' %}</h1>
<p>{% trans "You don't have permission to access the requested page." %}</p>
{% endblock %}

View File

@ -1,18 +0,0 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block title %}{% trans "Not Found" %}{% endblock %}
{% block nav-global %}{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans "Home" %}</a> &rsaquo;
{% trans "Not Found" %}
</div>
{% endblock %}
{% block content %}
<h1 style="margin-bottom: 0.7em;">{% trans 'Not Found' %}</h1>
<p>{% trans "We're sorry, but the requested page could not be found." %}</p>
{% endblock %}

View File

@ -1,7 +1,7 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block title %}{% trans "Server Error" %}{% endblock %}
{% block title %}{{ name }}{% endblock %}
{% block nav-global %}{% endblock %}
@ -10,11 +10,11 @@
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans "Home" %}</a> &rsaquo;
{% trans "Server Error" %}
{{ name }}
</div>
{% endblock %}
{% block content %}
<h1 style="margin-bottom: 0.7em;">{% trans 'Server Error' %}</h1>
<p>{% trans "A server error has occurred." %}</p>
<h1 style="margin-bottom: 0.7em;">{{ name }}</h1>
<p>{{ content }}</p>
{% endblock %}

28
awx/templates/error.html Normal file
View File

@ -0,0 +1,28 @@
{% extends "rest_framework/api.html" %}
{% load i18n %}
{% block style %}
{{ block.super }}
<style type="text/css">
ul.breadcrumb {
visibility: hidden;
margin-top: 10px !important;
}
pre.err {
margin-bottom: 2em;
}
div.request-info, div.response-info {
display: none;
}
</style>
{% endblock %}
{% block branding_title %}{% endblock %}
{% block breadcrumbs %}
<ul class="breadcrumb">
<li>
<a href="{{ request.get_full_path }}" class="active">{{ name }}</a>
</li>
</ul>
{% endblock %}

View File

@ -1,7 +1,7 @@
{% extends 'rest_framework/base.html' %}
{% load i18n %}
{% block title %}{% trans 'AWX REST API' %}{% endblock %}
{% block title %}{{ name }} &middot; {% trans 'AWX REST API' %}{% endblock %}
{% block style %}
{{ block.super }}
@ -101,6 +101,7 @@ html body .description {
font-size: 0.8em;
text-align: center;
padding-bottom: 1em;
line-height: 1.6em;
}
#footer a,
#footer a:hover {
@ -125,7 +126,7 @@ html body #push {
{% endblock %}
{% block branding %}
<a class="brand" href="/api/"><img class="logo" src="{{ STATIC_URL }}img/logo.png">{% trans 'REST API' %}</a>
<a class="brand" href="/api/"><img class="logo" src="{{ STATIC_URL }}img/logo.png">{% block branding_title %}{% trans 'REST API' %}{% endblock %}</a>
{% endblock %}
{% block userlinks %}
@ -139,8 +140,9 @@ html body #push {
{% block footer %}
<div id="footer">
<img class="awxlogo" src="{{ STATIC_URL }}img/AWX_logo.png" /><br/>
Copyright &copy; 2013 <a href="http://www.ansibleworks.com/">AnsibleWorks, Inc.</a> All rights reserved.<br />
1482 East Valley Road, Suite 888 &middot; Montecito, California, 93108 &middot; <a href="tel:18008250212">+1-800-825-0212<a/></div>
Copyright &copy; 2013 AnsibleWorks, Inc. All rights reserved.<br />
1482 East Valley Road, Suite 888 &middot; Santa Barbara, California 93108 &middot; +1-800-825-0212<br />
<a href="http://www.ansibleworks.com" target="_blank">www.ansibleworks.com</a>
{% endblock %}
{% block script %}

View File

@ -13,22 +13,20 @@ urlpatterns = patterns('',
url(r'^api/', include('awx.api.urls', namespace='api', app_name='api')),
)
urlpatterns += patterns('awx.main.views',
url(r'^403.html$', 'handle_403'),
url(r'^404.html$', 'handle_404'),
url(r'^500.html$', 'handle_500'),
)
if 'django.contrib.admin' in settings.INSTALLED_APPS:
from django.contrib import admin
admin.autodiscover()
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('awx.main.views',
url(r'^403.html$', 'handle_403'),
url(r'^404.html$', 'handle_404'),
url(r'^500.html$', 'handle_500'),
url(r'^admin/403.html$', 'handle_403'),
url(r'^admin/404.html$', 'handle_404'),
url(r'^admin/500.html$', 'handle_500'),
)
if 'django.contrib.admin' in settings.INSTALLED_APPS:
urlpatterns += patterns('awx.main.views',
url(r'^admin/403.html$', 'handle_403'),
url(r'^admin/404.html$', 'handle_404'),
url(r'^admin/500.html$', 'handle_500'),
)