13822ea4eac1c0d14f5281b2821000f351b376a6
[mkwsxb-moved-to-github.git] / mkwsxb / mkwsxb.py
1 """Embed widgets from MKWS, the MasterKey Widget Set"""
2
3 import pkg_resources
4 import random
5
6 from xblock.core import XBlock
7 from xblock.fields import Integer, Scope, String, Any, Boolean, Dict
8 from xblock.fragment import Fragment
9
10 class MKWSXB(XBlock):
11     """Embed widgets from MKWS, the MasterKey Widget Set"""
12
13     # Fields
14     query = String(
15       help="Search query",
16       default="water",
17       scope=Scope.content
18     )
19     display_name = String(
20       default="MKWS Widget",
21       scope=Scope.settings
22     )
23
24     def resource_string(self, path):
25         """Helper for accessing resources."""
26         data = pkg_resources.resource_string(__name__, path)
27         return data.decode("utf8")
28
29     def student_view(self, context=None):
30         """The primary view of the MKWS XBlock, shown to students when viewing courses."""
31         html = self.resource_string("static/html/mkwsxb.html")
32         frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000)))
33         # mkwsxb.js uses require.js as it cannot guarantee mkws-complete.js has loaded 
34         # in studio without it
35         frag.add_javascript_url("/static/js/vendor/require.js");
36         frag.add_javascript(self.resource_string("static/js/src/mkwsxb.js"))
37         frag.add_css(self.resource_string("static/css/mkws-widget-ru.css"))
38         frag.initialize_js('MKWSXB')
39         return frag;
40
41     def studio_view(self, context=None):
42         """Studio configuration view."""
43         html = self.resource_string("static/html/settings.html")
44         frag = Fragment(html.format(query=self.query))
45         frag.add_javascript(self.resource_string("static/js/src/settings.js"))
46         frag.initialize_js('MKWSXBSettings')
47         return frag
48
49     @XBlock.json_handler
50     def update_settings(self, data, suffix=''):
51         """Studio configuration callback."""
52         self.query = data['query']
53         return {"result": "success"}
54
55     @staticmethod
56     def workbench_scenarios():
57         """A canned scenario for display in the workbench."""
58         return [
59             ("MKWSXB",
60              """<vertical_demo>
61                 <mkwsxb/>
62                 </vertical_demo>
63              """),
64         ]