Point mkwsref at the temporary home of the templated mkws
[mkwsxb-moved-to-github.git] / mkwsbiblio / mkwsbiblio / mkwsbiblio.py
1 """Embed bibliographic widget 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 MKWSBiblio(XBlock):
11     """Embed bibliographic widget 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 bibliographic details",
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/student.html")
32         frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000)))
33         # student.js uses require.js as it cannot guarantee mkws-complete.js has loaded 
34         # in studio without it. We'll need to add it if we're in the LMS:
35         frag.add_javascript_url("/static/js/vendor/require.js");
36         # frag.add_resource_url("//mkws.indexdata.com/mkws-complete", "text/javascript", "head");
37         # frag.add_resource('<script src="//mkws.indexdata.com/mkws-complete.js"></script>', "text/html", "head");
38         frag.add_javascript(self.resource_string("static/js/src/student.js"))
39         frag.initialize_js('MKWSBiblio')
40         return frag;
41
42     def author_view(self, context=None):
43         """The primary view of the MKWS XBlock, shown when authoring courses."""
44         # This should closely mirror the student_view. Here all we do is not include
45         # require.js as it's already in Studio and the lms path won't work.
46         html = self.resource_string("static/html/student.html")
47         frag = Fragment(html.format(query=self.query, team=random.randint(0, 100000)))
48         frag.add_javascript(self.resource_string("static/js/src/student.js"))
49         frag.initialize_js('MKWSBiblio')
50         return frag;
51
52     def studio_view(self, context=None):
53         """Studio configuration view."""
54         html = self.resource_string("static/html/settings.html")
55         frag = Fragment(html.format(query=self.query))
56         frag.add_javascript(self.resource_string("static/js/settings.js"))
57         frag.initialize_js('MKWSBiblioSettings')
58         return frag
59
60     @XBlock.json_handler
61     def update_settings(self, data, suffix=''):
62         """Studio configuration callback."""
63         self.query = data['query']
64         return {"result": "success"}
65
66     @staticmethod
67     def workbench_scenarios():
68         """A canned scenario for display in the workbench."""
69         return [
70             ("MKWSBiblio",
71              """<vertical_demo>
72                 <mkwsbiblio/>
73                 </vertical_demo>
74              """),
75         ]