Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions inginious/frontend/webapp/plugins/task_cache/__init__.py
Original file line number Diff line number Diff line change
@@ -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 = '<i class="fa fa-tags fa-fw"></i>&nbsp; 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):

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions inginious/frontend/webapp/plugins/task_cache/pages.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions inginious/frontend/webapp/plugins/task_cache/tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$def with (course, taskid, task_data, tags)

<h3>${taskid}'s Tags</h3>
<div class="form-group">
<div class="col-sm-8" id="tags">
<input type="text" value="$tags" data-role="tagsinput" name="tags"/>
</div>
</div>