-
Notifications
You must be signed in to change notification settings - Fork 44
/
renderers.py
86 lines (66 loc) · 4.18 KB
/
renderers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import hashlib
from django.conf import settings
from rest_framework import renderers
from capapi.resources import cache_func
from scripts.generate_case_html import generate_html
class XMLRenderer(renderers.BaseRenderer):
media_type = 'application/xml'
format = 'xml'
def render(self, data, media_type=None, renderer_context=None):
if 'detail' in data:
if data['detail'] == "Not found.":
return generate_xml_error("Case Not Found.", "The case specified by the URL does not exist in our database.")
return generate_xml_error("Authentication Error", data['detail'])
# if user requested format=xml without requesting full casebody
if 'casebody' not in data:
return generate_xml_error("Full Case Parameter Missing in Non-JSON Request", "To retrieve the full case body, you must specify full_case=true in the URL. If you only want metadata, you must specify format=json in the URL. We only serve standalone metadata in JSON format.")
if data['casebody']['status'] != 'ok':
if data['casebody']['status'] == 'error_auth_required':
return generate_xml_error("Not Authenticated ({})".format(data['casebody']['status']), "You must be authenticated to view this case.")
return generate_xml_error("Could Not Load Case Body", data['casebody']['status'])
else:
return data['casebody']['data']
class HTMLRenderer(renderers.StaticHTMLRenderer):
def render(self, data, media_type=None, renderer_context=None):
if 'detail' in data:
if data['detail'] == "Not found.":
return generate_html_error("Case Not Found.", "The case specified by the URL does not exist in our database.")
return generate_html_error("Authentication Error", data['detail'])
# if user requested format=html without requesting full casebody
if 'casebody' not in data:
return generate_html_error("Full Case Parameter Missing in Non-JSON Request", "To retrieve the full case body, you must specify <span style='font-family: monospace; font-style: normal;'>full_case=true</span> in the URL. If you only want metadata you must specify <span style='font-family: monospace; font-style: normal;'>format=json</span> in the URL. We only serve standalone metadata in JSON format.")
if data['casebody']['status'] != 'ok':
if data['casebody']['status'] == 'error_auth_required':
return generate_html_error("Not Authenticated <span style='font-family: monospace; font-style: normal;'>({})</span>".format(data['casebody']['status']), "You must be authenticated to view this case.")
return generate_html_error("Could Not Load Case Body", data['casebody']['status'], data['first_page'], data['last_page'], data['name'])
return super().render(generate_html(data['casebody']['data'], case_body_only=False), media_type, renderer_context)
class BrowsableAPIRenderer(renderers.BrowsableAPIRenderer):
@cache_func(
key=lambda self, data, view, request: hashlib.md5(('filter-form:'+request.get_full_path()).encode('utf8')).hexdigest(),
timeout=settings.API_COUNT_CACHE_TIMEOUT,
)
def get_filter_form(self, data, view, request):
return super().get_filter_form(data, view, request)
def generate_xml_error(error_text, message_text):
return """
<data>
<error>%s</error>
<message>%s</message>
</data>
""" % (error_text, message_text)
def generate_html_error(error_text, message_text, first_page=None, last_page=None, case_name=None):
if first_page is not None and last_page is not None and case_name is not None:
section_and_title = """
<section data-data-firstpage="{0}" data-data-lastpage="{1}" data-class="casebody">
<h4>{2}</h4>
""".format(first_page, last_page, case_name)
else:
section_and_title = "<section>"
return """
{}
<article class='error'>
<h1>Error: {}</h1>
<p><span style="font-weight: bold;">Details:</span> <span style="font-style: italic;">{}</span></p>
</article>
</section>
""".format(section_and_title, error_text, message_text)