Skip to content

Commit

Permalink
Merge pull request #13 from edx/dcs/self-paced
Browse files Browse the repository at this point in the history
Handle self-paced courses by not saving due dates in the database.
  • Loading branch information
Dave St.Germain authored May 21, 2019
2 parents 4de903b + 2f2d3bf commit cbc0fe9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion edx_when/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = '0.1.4'
__version__ = '0.1.5'

default_app_config = 'edx_when.apps.EdxWhenConfig' # pylint: disable=invalid-name
10 changes: 9 additions & 1 deletion edx_when/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

log = logging.getLogger('edx-when')

FIELDS_TO_EXTRACT = ('due', 'start')
FIELDS_TO_EXTRACT = ('due', 'start', 'end')


def _ensure_key(key_class, key_obj):
Expand Down Expand Up @@ -62,6 +62,14 @@ def set_dates_for_course(course_key, items):
set_date_for_block(course_key, location, field, val)


def clear_dates_for_course(course_key):
"""
Set all dates to inactive.
"""
course_key = _ensure_key(CourseKey, course_key)
models.ContentDate.objects.filter(course_id=course_key, active=True).update(active=False)


def get_dates_for_course(course_id, user=None, use_cached=True):
"""
Return dictionary of dates for the given course_id and optional user.
Expand Down
22 changes: 15 additions & 7 deletions edx_when/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import logging

from .api import set_dates_for_course
from .api import clear_dates_for_course, set_dates_for_course

log = logging.getLogger('edx-when')

Expand All @@ -15,12 +15,20 @@ def extract_dates(sender, course_key, **kwargs): # pylint: disable=unused-argum
from xmodule.modulestore.django import modulestore # pylint: disable=import-error
from xmodule.modulestore.inheritance import own_metadata # pylint: disable=import-error

log.info('publishing course dates for %s', course_key)
date_items = []
items = modulestore().get_items(course_key)
log.info('extracting dates from %d items in %s', len(items), course_key)
for item in items:
date_items.append((item.location, own_metadata(item)))
course = modulestore().get_course(course_key)
if not course:
return None
elif course.self_paced:
log.info('%s is self-paced. Clearing due dates', course_key)
clear_dates_for_course(course_key)
date_items = [(course.location, own_metadata(course))]
else:
log.info('Publishing course dates for %s', course_key)
date_items = []
items = modulestore().get_items(course_key)
log.info('extracting dates from %d items in %s', len(items), course_key)
for item in items:
date_items.append((item.location, own_metadata(item)))

try:
set_dates_for_course(course_key, date_items)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def test_get_dates_for_course(self):
# second time is cached
retrieved = api.get_dates_for_course(items[0][0].course_key)
assert len(retrieved) == 3
return items

def test_clear_dates_for_course(self):
items = self.test_get_dates_for_course()
api.clear_dates_for_course(items[0][0].course_key)
retrieved = api.get_dates_for_course(items[0][0].course_key, use_cached=False)
assert not retrieved

def test_set_user_override(self):
items = make_items()
Expand Down

0 comments on commit cbc0fe9

Please sign in to comment.