-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathviews.py
120 lines (97 loc) · 4.07 KB
/
views.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
from django.http import Http404, HttpResponse
from django.template.loader import get_template
from django.utils.decorators import method_decorator
from django.utils.html import escape
from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import TemplateView
from pattern_library import get_base_template_names, get_pattern_base_template_name
from pattern_library.exceptions import PatternLibraryEmpty, TemplateIsNotPattern
from pattern_library.utils import (
get_pattern_config,
get_pattern_config_str,
get_pattern_context,
get_pattern_markdown,
get_pattern_templates,
get_sections,
get_template_ancestors,
is_pattern,
render_pattern,
)
class IndexView(TemplateView):
http_method_names = ("get",)
template_name = "pattern_library/index.html"
def first_template_from_group(self, templates):
try:
return templates["templates_stored"][0]
except IndexError:
for template_group in templates["template_groups"].values():
return self.first_template_from_group(template_group)
return None
def get_first_template(self, templates):
first_template = self.first_template_from_group(templates)
if first_template:
return first_template.origin.template_name
sections = get_sections()
if sections:
raise PatternLibraryEmpty(
"No templates found matching: '%s'" % str(sections)
)
else:
raise PatternLibraryEmpty(
"No 'SECTIONS' found in the 'PATTERN_LIBRARY' setting"
)
def get(self, request, pattern_template_name=None):
# Get all pattern templates
templates = get_pattern_templates()
if pattern_template_name is None:
# Just display the first pattern if a specific one isn't requested
pattern_template_name = self.get_first_template(templates)
if not is_pattern(pattern_template_name):
raise Http404
template = get_template(pattern_template_name)
pattern_config = get_pattern_config(pattern_template_name)
context = self.get_context_data()
context["pattern_templates"] = templates
context["pattern_template_name"] = pattern_template_name
context["pattern_source"] = escape(template.template.source)
context["pattern_config"] = escape(
get_pattern_config_str(pattern_template_name)
)
context["pattern_name"] = pattern_config.get("name", pattern_template_name)
context["pattern_markdown"] = get_pattern_markdown(pattern_template_name)
return self.render_to_response(context)
class RenderPatternView(TemplateView):
http_method_names = ("get",)
template_name = get_pattern_base_template_name()
@method_decorator(xframe_options_sameorigin)
def get(self, request, pattern_template_name=None):
pattern_template_ancestors = get_template_ancestors(
pattern_template_name,
context=get_pattern_context(self.kwargs["pattern_template_name"]),
)
pattern_is_fragment = set(pattern_template_ancestors).isdisjoint(
set(get_base_template_names())
)
try:
rendered_pattern = render_pattern(request, pattern_template_name)
except TemplateIsNotPattern:
raise Http404
if pattern_is_fragment:
context = self.get_context_data()
context["pattern_library_rendered_pattern"] = rendered_pattern
return self.render_to_response(context)
return HttpResponse(rendered_pattern)
@csrf_exempt
def render_pattern_api(request):
data = json.loads(request.body.decode("utf-8"))
template_name = data["template_name"]
config = data["config"]
try:
rendered_pattern = render_pattern(
request, template_name, allow_non_patterns=False, config=config
)
except TemplateIsNotPattern:
raise Http404
return HttpResponse(rendered_pattern)