Skip to content

Commit 4c88fb3

Browse files
theparadoxer02nishutosh
authored andcommitted
added custom error page with test cases
1 parent cd2557d commit 4c88fb3

File tree

10 files changed

+293
-8
lines changed

10 files changed

+293
-8
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Contribution are welcome as always. Checkout the existing issues or create issue
1010

1111
- Always branch from updated `development` branch locally.
1212

13-
1413
- If resolving an issue then create a branch with name like `bugfix-#<issue_number>` or `enhancement-#<issue_number>`.
1514

1615
- Write a commit messages describing the changes you made
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% load staticfiles %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<!-- Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages -->
6+
7+
<meta charset="utf-8" />
8+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1" />
10+
11+
<link rel="stylesheet" href="{% static 'css/error.css' %}">
12+
13+
<title>We've got some trouble | 400 - Bad Request</title>
14+
15+
</head>
16+
17+
<body>
18+
<div class="cover">
19+
<h1>Bad Request <small>Error 400</small></h1>
20+
<p class="lead">The server cannot process the request due to something that is perceived to be a client error.</p>
21+
</div>
22+
23+
</body>
24+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% load staticfiles %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<!-- Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages -->
6+
7+
<meta charset="utf-8" />
8+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1" />
10+
<link rel="stylesheet" href="{% static 'css/error.css' %}">
11+
12+
<title>We've got some trouble | 403 - Access Denied</title>
13+
14+
</head>
15+
16+
<body>
17+
<div class="cover">
18+
<h1>Access Denied <small>Error 403</small></h1>
19+
<p class="lead">The requested resource requires an authentication.</p>
20+
</div>
21+
22+
</body>
23+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% load staticfiles %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<!-- Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages -->
6+
7+
<meta charset="utf-8" />
8+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1" />
10+
11+
<title>We've got some trouble | 404 - Resource not found</title>
12+
13+
<link rel="stylesheet" href="{% static 'css/error.css' %}">
14+
15+
</head>
16+
17+
<body>
18+
<div class="cover">
19+
<h1>Resource not found <small>Error 404</small></h1>
20+
<p class="lead">The requested resource could not be found but may be available again in the future.</p>
21+
</div>
22+
23+
</body>
24+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% load staticfiles %}
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<!-- Simple HttpErrorPages | MIT X11 License | https://github.com/AndiDittrich/HttpErrorPages -->
6+
7+
<meta charset="utf-8" />
8+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1" />
10+
11+
<title>We've got some trouble | 500 - Webservice currently unavailable</title>
12+
<link rel="stylesheet" href="{% static 'css/error.css' %}">
13+
14+
</head>
15+
16+
<body>
17+
<div class="cover">
18+
<h1>Webservice currently unavailable <small>Error 500</small></h1>
19+
<p class="lead">An unexpected condition was encountered.<br />
20+
Our service team has been dispatched to bring it back online.</p>
21+
</div>
22+
23+
</body>
24+
</html>

code_share/app_code_share/tests/test_urls.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
from test_plus.test import TestCase
33
from app_code_share.models import CodeShare
44
from django.utils.crypto import get_random_string
5+
from django.test.client import RequestFactory
6+
7+
from code_share.views import (
8+
page_not_found_view,
9+
my_custom_error_view,
10+
permission_denied_view,
11+
bad_request_view,
12+
)
513

614

715

@@ -35,3 +43,31 @@ def test_detail_resolve(self):
3543
resolve('/app/' + self.hash_value + '/').view_name,
3644
'code_share:view_by_hash'
3745
)
46+
47+
48+
class TestMyErrorPages(TestCase):
49+
50+
def test_400_error(self):
51+
factory = RequestFactory()
52+
request = factory.get('/400')
53+
response = bad_request_view(request)
54+
self.assertEqual(response.status_code, 400)
55+
56+
def test_403_error(self):
57+
factory = RequestFactory()
58+
request = factory.get('/403')
59+
response = permission_denied_view(request)
60+
self.assertEqual(response.status_code, 403)
61+
62+
def test_404_error(self):
63+
factory = RequestFactory()
64+
request = factory.get('/404')
65+
response = page_not_found_view(request)
66+
self.assertEqual(response.status_code, 404)
67+
68+
def test_500_error(self):
69+
factory = RequestFactory()
70+
request = factory.get('/500')
71+
response = my_custom_error_view(request)
72+
self.assertEqual(response.status_code, 500)
73+

code_share/app_code_share/views.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.shortcuts import render, redirect, get_object_or_404 as goo404
22
from .models import CodeShare
3-
import random
43
from django.utils.crypto import get_random_string
4+
from django.http import Http404
5+
56

67

78
def home(request):
@@ -31,10 +32,8 @@ def home(request):
3132
code_share = request.POST.get('code_snippet')
3233
file_name = request.POST.get('file_name')
3334
language = request.POST.get('language')
34-
3535
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
36-
hash_value= get_random_string(8, chars)
37-
36+
hash_value = get_random_string(8, chars)
3837
CodeShare.objects.create(code=code_share,
3938
hash_value=hash_value,
4039
file_name=file_name,
@@ -64,12 +63,16 @@ def view_by_hash(request, hash_id):
6463
"""
6564

6665
if request.method == 'GET':
67-
code_share = CodeShare.objects.get(hash_value=hash_id)
68-
return render(request, 'app_code_share/code_view.html', {'code_share': code_share})
66+
try:
67+
code_share = CodeShare.objects.get(hash_value=hash_id)
68+
except CodeShare.DoesNotExist:
69+
raise Http404("Codeshare does not exist")
70+
context = {'code_share': code_share, "filename": "yes"}
71+
return render(request, 'app_code_share/homepage.html', context)
6972

7073
if request.method == 'POST':
7174
code_share = request.POST.get('code_snippet')
72-
language =request.POST.get('language')
75+
language = request.POST.get('language')
7376
code_obj = goo404(CodeShare, hash_value=hash_id)
7477
code_obj.code = code_share
7578
code_obj.language = language

code_share/code_share/urls.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@
22
from django.contrib import admin
33
from django.conf import settings
44
from django.conf.urls.static import static
5+
from .views import (
6+
page_not_found_view,
7+
my_custom_error_view,
8+
permission_denied_view,
9+
bad_request_view,
10+
)
11+
512

613
urlpatterns = [
714
url(r'^admin', admin.site.urls),
815
url(r'^', include('app_code_share.urls', namespace='code_share')),
916
url(r'^api/', include('api_app.urls')),
1017
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
18+
19+
if settings.DEBUG:
20+
urlpatterns += [
21+
url(r'^404/$', page_not_found_view),
22+
url(r'^500/$', my_custom_error_view),
23+
url(r'^400/$', bad_request_view),
24+
url(r'^403/$', permission_denied_view),
25+
]
26+
27+
handler404 = page_not_found_view
28+
handler500 = my_custom_error_view
29+
handler403 = permission_denied_view
30+
handler400 = bad_request_view

code_share/code_share/views.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from django.template import loader
2+
from django.http import (
3+
HttpResponseNotFound,
4+
HttpResponseServerError,
5+
HttpResponseForbidden,
6+
HttpResponseBadRequest,
7+
)
8+
9+
10+
def page_not_found_view(request):
11+
t = loader.get_template('error/HTTP404.html')
12+
html = html = t.render({})
13+
return HttpResponseNotFound(html)
14+
15+
16+
def my_custom_error_view(request):
17+
t = loader.get_template('error/HTTP500.html')
18+
html = html = t.render({})
19+
return HttpResponseServerError(html)
20+
21+
22+
def permission_denied_view(request):
23+
t = loader.get_template('error/HTTP403.html')
24+
html = html = t.render({})
25+
return HttpResponseForbidden(html)
26+
27+
28+
def bad_request_view(request):
29+
t = loader.get_template('error/HTTP400.html')
30+
html = html = t.render({})
31+
return HttpResponseBadRequest(html)

code_share/static/css/error.css

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
html { font-family:sans-serif;line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100% }
3+
4+
body { margin:0 }
5+
6+
article,aside,footer,header,nav,section{ display:block }
7+
8+
h1 { font-size:2em;margin:.67em0 }
9+
10+
figcaption,figure,main{ display:block }
11+
12+
figure{ margin:1em 40px }
13+
14+
hr { box-sizing:content-box;height:0;overflow:visible }
15+
16+
pre { font-family:monospace,monospace;font-size:1em }
17+
18+
a{ background-color:transparent;-webkit-text-decoration-skip:objects }
19+
20+
a:active,a:hover{ outline-width:0 }
21+
22+
abbr[title]{ border-bottom:none;text-decoration:underline;text-decoration:underline dotted }
23+
24+
b,strong{ font-weight:inherit }
25+
26+
b,strong{ font-weight:bolder }
27+
28+
code,kbd,samp{ font-family:monospace,monospace;font-size:1em }
29+
30+
dfn{ font-style:italic }
31+
32+
mark{ background-color:#ff0;color:#000 }
33+
34+
small{ font-size:80% }
35+
36+
sub,sup{ font-size:75%;line-height:0;position:relative;vertical-align:baseline }
37+
38+
sub{ bottom:-.25em }
39+
40+
sup{ top:-.5em }
41+
42+
audio,video{ display:inline-block }
43+
44+
audio:not([controls]){ display:none;height:0 }
45+
46+
img{border-style:none}svg:not(:root){ overflow:hidden }
47+
48+
button,input,optgroup,select,textarea{ font-family:sans-serif;font-size:100%;line-height:1.15;margin:0 }
49+
50+
button,input{ overflow:visible }
51+
52+
button,select{ text-transform:none }
53+
54+
[type=reset],[type=submit],button,html
55+
56+
[type=button]{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{ border-style:none;padding:0 }
57+
58+
[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{ outline:1pxdottedButtonText }
59+
60+
fieldset{ border:1px solid silver;margin:0 2px;padding:.35em .625em .75em }
61+
62+
legend{ box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal }
63+
64+
progress{ display:inline-block;vertical-align:baseline }
65+
66+
textarea{ overflow:auto}[type=checkbox],[type=radio]{ box-sizing:border-box;padding:0 }
67+
68+
[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{ height:auto }
69+
70+
[type=search]{ -webkit-appearance:textfield;outline-offset:-2px }
71+
72+
[type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}
73+
74+
details,menu{display:block}summary{display:list-item}
75+
76+
canvas{display:inline-block}template{display:none}[hidden]{display:none}/*!
77+
78+
*/
79+
body,html { width:100%;height:100%;background-color:#21232a }
80+
81+
body {
82+
color:#fff;text-align:center;text-shadow:0 2px 4px
83+
rgba(0,0,0,.5);padding:0;min-height:100%;-webkit-box-shadow:inset 0 0 75pt
84+
rgba(0,0,0,.8);box-shadow:inset 0 0 75pt
85+
rgba(0,0,0,.8);display:table;font-family:"Open Sans",Arial,sans-serif
86+
}
87+
88+
h1{font-family:inherit;font-weight:500;line-height:1.1;color:inherit;font-size:36px}h1
89+
90+
small{font-size:68%;font-weight:400;line-height:1;color:#777}
91+
92+
a{text-decoration:none;color:#fff;font-size:inherit;border-bottom:dotted 1px #707070}
93+
94+
.lead{color:silver;font-size:21px;line-height:1.4}
95+
96+
.cover{display:table-cell;vertical-align:middle;padding:0 20px}
97+
98+
footer{position:fixed;width:100%;height:40px;left:0;bottom:0;color:#a0a0a0;font-size:14px}</style>
99+
100+
101+

0 commit comments

Comments
 (0)