Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 268d03a

Browse files
authored
Merge pull request openedx#22996 from cpennington/move-course-date-signal-handling
Duplicate signals handlers for course content dates from edx-when
2 parents 124505d + 7e411cd commit 268d03a

File tree

8 files changed

+98
-7
lines changed

8 files changed

+98
-7
lines changed

lms/djangoapps/instructor/tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from django.urls import reverse as django_reverse
2727
from django.utils.translation import ugettext as _
2828
from edx_when.api import get_overrides_for_user
29-
from edx_when.signals import extract_dates
3029
from mock import Mock, NonCallableMock, patch
3130
from opaque_keys.edx.keys import CourseKey
3231
from opaque_keys.edx.locator import UsageKey
@@ -62,6 +61,7 @@
6261
QueueConnectionError,
6362
generate_already_running_error_message
6463
)
64+
from openedx.core.djangoapps.course_date_signals.handlers import extract_dates
6565
from openedx.core.djangoapps.course_groups.cohorts import set_course_cohorted
6666
from openedx.core.djangoapps.django_comment_common.models import FORUM_ROLE_COMMUNITY_TA
6767
from openedx.core.djangoapps.django_comment_common.utils import seed_permissions_roles

lms/djangoapps/instructor/tests/test_tools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
from opaque_keys.edx.keys import CourseKey
1616
from pytz import UTC
1717

18-
from edx_when import api, signals
18+
from edx_when import api
1919
from edx_when.field_data import DateLookupFieldData
20+
from openedx.core.djangoapps.course_date_signals import handlers
2021
from student.tests.factories import UserFactory
2122
from xmodule.fields import Date
2223
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, SharedModuleStoreTestCase
@@ -215,7 +216,7 @@ def setUp(self):
215216
week3 = ItemFactory.create(parent=course)
216217
homework = ItemFactory.create(parent=week1)
217218
assignment = ItemFactory.create(parent=homework, due=due)
218-
signals.extract_dates(None, course.id)
219+
handlers.extract_dates(None, course.id)
219220

220221
user = UserFactory.create()
221222

@@ -296,7 +297,7 @@ def setUp(self):
296297
self.week2 = week2
297298
self.user1 = user1
298299
self.user2 = user2
299-
signals.extract_dates(None, course.id)
300+
handlers.extract_dates(None, course.id)
300301

301302
def test_dump_module_extensions(self):
302303
extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC)

openedx/core/djangoapps/course_date_signals/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Django app configuration for openedx.core.djangoapps.course_dates_signals
3+
"""
4+
5+
from django.apps import AppConfig
6+
7+
8+
class CourseDatesSignalsConfig(AppConfig):
9+
name = 'openedx.core.djangoapps.course_dates_signals'
10+
11+
def ready(self):
12+
"""
13+
Connect handlers to signals.
14+
"""
15+
from . import handlers # pylint: disable=unused-variable
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Signal handlers for writing course dates into edx_when."""
2+
from __future__ import absolute_import, unicode_literals
3+
4+
import logging
5+
6+
from django.dispatch import receiver
7+
from six import text_type
8+
from xblock.fields import Scope
9+
from xmodule.modulestore.django import SignalHandler, modulestore
10+
11+
from edx_when.api import FIELDS_TO_EXTRACT, set_dates_for_course
12+
13+
log = logging.getLogger(__name__)
14+
15+
16+
def date_field_values(date_fields, xblock):
17+
"""
18+
Read field values for the specified date fields from the supplied xblock.
19+
"""
20+
result = {}
21+
for field_name in date_fields:
22+
if field_name not in xblock.fields:
23+
continue
24+
field = xblock.fields[field_name]
25+
if field.scope == Scope.settings and field.is_set_on(xblock):
26+
try:
27+
result[field.name] = field.read_from(xblock)
28+
except TypeError as exception:
29+
exception_message = "{message}, Block-location:{location}, Field-name:{field_name}".format(
30+
message=text_type(exception),
31+
location=text_type(xblock.location),
32+
field_name=field.name
33+
)
34+
raise TypeError(exception_message)
35+
return result
36+
37+
38+
def extract_dates_from_course(course):
39+
"""
40+
Extract all dates from the supplied course.
41+
"""
42+
log.info('Publishing course dates for %s', course.id)
43+
if course.self_paced:
44+
metadata = date_field_values(FIELDS_TO_EXTRACT, course)
45+
# self-paced courses may accidentally have a course due date
46+
metadata.pop('due', None)
47+
date_items = [(course.location, metadata)]
48+
else:
49+
date_items = []
50+
items = modulestore().get_items(course.id)
51+
log.info('extracting dates from %d items in %s', len(items), course.id)
52+
for item in items:
53+
date_items.append((item.location, date_field_values(FIELDS_TO_EXTRACT, item)))
54+
return date_items
55+
56+
57+
@receiver(SignalHandler.course_published)
58+
def extract_dates(sender, course_key, **kwargs): # pylint: disable=unused-argument
59+
"""
60+
Extract dates from blocks when publishing a course.
61+
"""
62+
log.info("Extracting dates from %s", course_key)
63+
64+
course = modulestore().get_course(course_key)
65+
66+
if not course:
67+
log.info("No course found for key %s", course_key)
68+
return
69+
70+
date_items = extract_dates_from_course(course)
71+
72+
try:
73+
set_dates_for_course(course_key, date_items)
74+
except Exception: # pylint: disable=broad-except
75+
log.exception('setting dates for %s', course_key)

requirements/edx/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ edx-sga==0.10.0
121121
edx-submissions==3.0.3
122122
edx-tincan-py35==0.0.5 # via edx-enterprise
123123
edx-user-state-client==1.1.2
124-
edx-when==0.6.0
124+
edx-when==0.7.0
125125
edxval==1.2.3
126126
elasticsearch==1.9.0 # via edx-search
127127
enum34==1.1.6 # via edxval

requirements/edx/development.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ edx-sphinx-theme==1.5.0
134134
edx-submissions==3.0.3
135135
edx-tincan-py35==0.0.5
136136
edx-user-state-client==1.1.2
137-
edx-when==0.6.0
137+
edx-when==0.7.0
138138
edxval==1.2.3
139139
elasticsearch==1.9.0
140140
entrypoints==0.3

requirements/edx/testing.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ edx-sga==0.10.0
129129
edx-submissions==3.0.3
130130
edx-tincan-py35==0.0.5
131131
edx-user-state-client==1.1.2
132-
edx-when==0.6.0
132+
edx-when==0.7.0
133133
edxval==1.2.3
134134
elasticsearch==1.9.0
135135
entrypoints==0.3 # via flake8

0 commit comments

Comments
 (0)