Skip to content

Commit

Permalink
[task_dispensers] add database and course_id to TaskDispenser (#795)
Browse files Browse the repository at this point in the history
Co-authored-by: lethblak <lethblak@ubunt-server.lan>
Co-authored-by: Matthieu Leclercq <maleclercq@github.com>
  • Loading branch information
3 people committed Mar 10, 2022
1 parent 34c7e09 commit 98b8e33
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion inginious/frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def get_app(config):
DisplayableMatchProblem]
}

course_factory, task_factory = create_factories(fs_provider, default_task_dispensers, default_problem_types, plugin_manager)
course_factory, task_factory = create_factories(fs_provider, default_task_dispensers, default_problem_types, plugin_manager, database)

user_manager = UserManager(database, config.get('superadmins', []))

Expand Down
9 changes: 5 additions & 4 deletions inginious/frontend/course_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class CourseFactory(object):
""" Load courses from disk """
_logger = logging.getLogger("inginious.course_factory")

def __init__(self, filesystem: FileSystemProvider, task_factory, plugin_manager, task_dispensers):
def __init__(self, filesystem: FileSystemProvider, task_factory, plugin_manager, task_dispensers, database):
self._filesystem = filesystem
self._task_factory = task_factory
self._plugin_manager = plugin_manager
self._task_dispensers = task_dispensers
self._cache = {}
self._database = database

def add_task_dispenser(self, task_dispenser):
"""
Expand Down Expand Up @@ -230,14 +231,14 @@ def _update_cache(self, courseid):
last_modif["$i18n/" + lang + ".mo"] = translations_fs.get_last_modification_time(lang + ".mo")

self._cache[courseid] = (
Course(courseid, course_descriptor, self.get_course_fs(courseid), self._task_factory, self._plugin_manager, self._task_dispensers),
Course(courseid, course_descriptor, self.get_course_fs(courseid), self._task_factory, self._plugin_manager, self._task_dispensers, self._database),
last_modif
)

self._task_factory.update_cache_for_course(courseid)


def create_factories(fs_provider, task_dispensers, task_problem_types, plugin_manager=None):
def create_factories(fs_provider, task_dispensers, task_problem_types, plugin_manager=None, database=None):
"""
Shorthand for creating Factories
:param fs_provider: A FileSystemProvider leading to the courses
Expand All @@ -249,4 +250,4 @@ def create_factories(fs_provider, task_dispensers, task_problem_types, plugin_ma
plugin_manager = PluginManager()

task_factory = TaskFactory(fs_provider, plugin_manager, task_problem_types)
return CourseFactory(fs_provider, task_factory, plugin_manager, task_dispensers), task_factory
return CourseFactory(fs_provider, task_factory, plugin_manager, task_dispensers, database), task_factory
4 changes: 2 additions & 2 deletions inginious/frontend/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _migrate_from_v_0_6(content, task_list):
class Course(object):
""" A course with some modification for users """

def __init__(self, courseid, content, course_fs, task_factory, plugin_manager, task_dispensers):
def __init__(self, courseid, content, course_fs, task_factory, plugin_manager, task_dispensers, database):
self._id = courseid
self._content = content
self._fs = course_fs
Expand Down Expand Up @@ -83,7 +83,7 @@ def __init__(self, courseid, content, course_fs, task_factory, plugin_manager, t
# Here we use a lambda to encourage the task dispenser to pass by the task_factory to fetch course tasks
# to avoid them to be cached along with the course object. Passing the task factory as argument
# would require to pass the course too, and have a useless reference back.
self._task_dispenser = task_dispenser_class(lambda: self._task_factory.get_all_tasks(self), self._content.get("dispenser_data", ''))
self._task_dispenser = task_dispenser_class(lambda: self._task_factory.get_all_tasks(self), self._content.get("dispenser_data", ''), database, self.get_id())
except:
raise Exception("Course has an invalid YAML spec: " + self.get_id())

Expand Down
1 change: 0 additions & 1 deletion inginious/frontend/pages/course_admin/task_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def POST_AUTH(self, courseid): # pylint: disable=arguments-differ

errors = []
user_input = flask.request.form

if "task_dispenser" in user_input:
selected_task_dispenser = user_input.get("task_dispenser", "toc")
task_dispenser_class = self.course_factory.get_task_dispensers().get(selected_task_dispenser, None)
Expand Down
5 changes: 3 additions & 2 deletions inginious/frontend/task_dispensers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from abc import ABCMeta, abstractmethod


class TaskDispenser(metaclass=ABCMeta):
def __init__(self, task_list_func, dispenser_data):
def __init__(self, task_list_func, dispenser_data, database, course_id):
"""
Instantiate a new TaskDispenser
:param task_list_func: A function returning the list of available course tasks from the task factory
:param dispenser_data: The dispenser data structure/configuration
:param database: The MongoDB database
:param course_id: A String that is the id of the course
"""
pass

Expand Down
2 changes: 1 addition & 1 deletion inginious/frontend/task_dispensers/combinatory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CombinatoryTest(TaskDispenser):

def __init__(self, task_list_func, dispenser_data):
def __init__(self, task_list_func, dispenser_data, database, course_id):
self._task_list_func = task_list_func
self._data = SectionsList(dispenser_data)

Expand Down
2 changes: 1 addition & 1 deletion inginious/frontend/task_dispensers/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class TableOfContents(TaskDispenser):

def __init__(self, task_list_func, dispenser_data):
def __init__(self, task_list_func, dispenser_data, database, course_id):
self._task_list_func = task_list_func
self._toc = SectionsList(dispenser_data)

Expand Down

0 comments on commit 98b8e33

Please sign in to comment.