-
Notifications
You must be signed in to change notification settings - Fork 73
/
app_renderers.py
178 lines (143 loc) · 5.9 KB
/
app_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
App renderers are objects that wrap an instance of an AbstractApp and the
the context where the app will be rendered into a single object. App renderers
can then passed to templates where their render method should be called to
render the html of the app.
App renderers are most useful with BasePlugin apps since they can be rendered
in many different views with different contexts. A helper function
build_plugin_renderers is provided for consistent and simple renderer building
in views.
Plugin view is a term that is used as an abstraction of the apps architecture
of A+. It consists of a name and definition of the context in that view. For
example, a in the course_instance view, plugins have the UserProfile of the
user logged in and the CourseInstance being viewed available for the plugin
renderer to use while rendering the plugin for the course_instance view. If the
plugin would for example just render a greeting for the user, it could use the
user's name in the greeting as it is available through the UserProfile object.
The available plugin views are
- submission
- exercise
- course_instance
The definition of the context of each plugin view can read from the code. The
code that calls the build_plugin_renderers is responsible of giving the
data required by the plugin view.
"""
import logging
import urllib.request
from bs4 import BeautifulSoup
from django.template.loader import get_template
from lib.helpers import update_url_params
logger = logging.getLogger("aplus.apps")
def build_plugin_renderers(plugins, # pylint: disable=too-many-arguments
view_name,
user_profile=None,
submission=None,
exercise=None,
course_instance=None,
course=None, # pylint: disable=unused-argument
course_module=None, # pylint: disable=unused-argument
category=None): # pylint: disable=unused-argument
try:
if view_name == "submission":
context = {
"user_profile": user_profile,
"course_instance": course_instance,
"exercise": exercise,
"submission": submission,
}
elif view_name == "exercise":
context = {
"user_profile": user_profile,
"course_instance": course_instance,
"exercise": exercise,
}
#elif view_name == "course_instance":
else:
context = {
"user_profile": user_profile,
"course_instance": course_instance,
}
#else:
# raise ValueError(view_name + " is not supported for plugins.")
plugins = plugins.filter(views__contains=view_name)
renderers = []
for p in plugins:
if hasattr(p, "get_renderer_class"):
renderers.append(p.get_renderer_class()(p, view_name, context))
else:
renderers.append(p)
return renderers
except Exception:
# If anything goes wrong, just return an empty list so that this isn't
# a show-stopper for the A+ core functionality.
logger.exception("Failed to create plugin renderers.")
return []
class ExternalIFramePluginRenderer:
def __init__(self, plugin, view_name, context):
self.plugin = plugin
self.view_name = view_name
self.context = context
def _build_src(self):
params = {
"view_name": self.view_name
}
for k, v in list(self.context.items()):
if v is not None:
params[k + "_id"] = v.id
return update_url_params(self.plugin.service_url, params)
def render(self):
try:
t = get_template("plugins/iframe_to_service_plugin.html")
return t.render({
"height": self.plugin.height,
"width": self.plugin.width,
"src": self._build_src(),
"title": self.plugin.title,
"view_name": self.view_name
})
except Exception:
# If anything goes wrong, just return an empty string so that this
# isn't a show-stopper for the A+ core functionality.
logger.exception("Failed to render an external iframe plugin.")
return ""
class ExternalIFrameTabRenderer:
def __init__(self, tab, user_profile, course_instance):
self.tab = tab
self.user_profile = user_profile
self.course_instance = course_instance
def _build_src(self):
params = {
"course_instance_id": self.course_instance.id,
"user_profile_id": self.user_profile.id
}
return update_url_params(self.tab.content_url, params)
def render(self):
t = get_template("plugins/external_iframe_tab.html")
return t.render({
"height": self.tab.height,
"width": self.tab.width,
"src": self._build_src(),
})
class TabRenderer:
def __init__(self, tab, user_profile, course_instance):
self.tab = tab
self.user_profile = user_profile
self.course_instance = course_instance
def _build_src(self):
params = {
"course_instance_id": self.course_instance.id,
"user_profile_id": self.user_profile.id
}
return update_url_params(self.tab.content_url, params)
def render(self):
url = self._build_src()
opener = urllib.request.build_opener()
content = opener.open(url, timeout=5).read()
soup = BeautifulSoup(content)
# If there's no element specified, use the BODY.
if self.tab.element_id == "":
html = str(soup.find("body"))
else:
html = str(soup.find(id=self.tab.element_id))
# TODO: should make relative link addresses absolute
return html