diff --git a/inginious/frontend/webapp/plugins/task_cache/__init__.py b/inginious/frontend/webapp/plugins/task_cache/__init__.py index ac6fb603..6a7e9d7c 100644 --- a/inginious/frontend/webapp/plugins/task_cache/__init__.py +++ b/inginious/frontend/webapp/plugins/task_cache/__init__.py @@ -1,8 +1,24 @@ import logging import json +import os.path +from . import pages +from inginious.frontend.webapp.plugins.utils import create_static_resource_page +_PLUGIN_PATH = os.path.dirname(__file__) +_BASE_RENDERER_PATH = _PLUGIN_PATH +_BASE_STATIC_FOLDER = os.path.join(_PLUGIN_PATH, 'static') +_logger = logging.getLogger("inginious.frontend.webapp.plugins.task_cache") + + +def tag_selection_tab(course, taskid, task_data, template_helper): + tab_id = 'tab_task_tags' + link = '  Tags' + template_helper.add_css("https://cdn.jsdelivr.net/bootstrap.tagsinput/0.4.2/bootstrap-tagsinput.css") + template_helper.add_javascript("https://cdn.jsdelivr.net/bootstrap.tagsinput/0.4.2/bootstrap-tagsinput.min.js") + tags = task_data.get('tags', "") + content = template_helper.get_custom_renderer(_BASE_RENDERER_PATH, + layout=False).tags(course, taskid, task_data, tags) + return tab_id, link, content -_BASE_RENDERER_PATH = 'frontend/webapp/plugins/hooks_example' -_logger = logging.getLogger("inginious.frontend.webapp.plugins.hooks_example") def init(plugin_manager, course_factory, client, config): @@ -11,7 +27,7 @@ def on_task_updated(courseid, taskid, new_content): descriptor = course_factory.get_course(courseid)._task_factory.get_task_descriptor_content(courseid, taskid) task_author = descriptor["author"] task_context = descriptor["context"] - tags = new_content.get("tags", []) + tags = new_content.get("tags", "").split(',') task_data = { "task_name": task_name, "task_id": taskid, @@ -54,8 +70,12 @@ def on_course_updated(courseid, new_content): if "tasks_cache" not in plugin_manager.get_database().collection_names(): plugin_manager.get_database().create_collection("tasks_cache") + plugin_manager.get_database().tasks_cache.create_index([("course_id", 1), ("task_id", 1)], unique=True) + plugin_manager.add_page(r'/static/task_cache/(.*)', create_static_resource_page(_BASE_STATIC_FOLDER)) + plugin_manager.add_page('/api/tags', pages.TagsApi) + plugin_manager.add_hook('task_editor_tab', tag_selection_tab) plugin_manager.add_hook('task_updated', on_task_updated) plugin_manager.add_hook('task_deleted', on_task_deleted) plugin_manager.add_hook('course_updated', on_course_updated) diff --git a/inginious/frontend/webapp/plugins/task_cache/pages.py b/inginious/frontend/webapp/plugins/task_cache/pages.py new file mode 100644 index 00000000..324a9911 --- /dev/null +++ b/inginious/frontend/webapp/plugins/task_cache/pages.py @@ -0,0 +1,45 @@ +import web +import inginious.frontend.webapp.pages.api._api_page as api +from inginious.frontend.webapp.pages.api._api_page import APIAuthenticatedPage +from inginious.common.course_factory import CourseNotFoundException, InvalidNameException, CourseUnreadableException + + +class TagsApi(APIAuthenticatedPage): + def API_GET(self): + self.validate_parameters() + return self.tags() + + def validate_parameters(self): + user_input = web.input(user_input=None).user_input + + if user_input is None: + raise api.APIError(400, {"error": "user_input is mandatory"}) + + def tags(self): + user_input = web.input().user_input + + pattern = ".*" + user_input + ".*" + + suggested_tags = self.database.tasks_cache.aggregate([ + { + "$unwind": {"path": "$tags"} + }, + { + "$match": {"tags": {"$regex": pattern}} + }, + { + "$group": + { + "_id": 0, + "tags": {"$addToSet": "$tags"} + } + }, + { + "$project": { + "_id": 0, + "tags": "$tags" + } + } + ]) + + return 200, list(suggested_tags) \ No newline at end of file diff --git a/inginious/frontend/webapp/plugins/task_cache/tags.html b/inginious/frontend/webapp/plugins/task_cache/tags.html new file mode 100644 index 00000000..3e1615d0 --- /dev/null +++ b/inginious/frontend/webapp/plugins/task_cache/tags.html @@ -0,0 +1,8 @@ +$def with (course, taskid, task_data, tags) + +

${taskid}'s Tags

+
+
+ +
+
\ No newline at end of file