-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
signals.py
469 lines (374 loc) · 16.1 KB
/
signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# coding=utf-8
"""
written by: Lawrence McDaniel
https://lawrencemcdaniel.com
date: feb-2022
usage: listen for Django Signals published by Open edX
see https://docs.djangoproject.com/en/4.1/topics/signals/
"""
import json
import logging
from attr import asdict
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in, user_logged_out
from openedx.core.djangoapps.user_authn.views.register import REGISTER_USER
from .utils import serialize_course_key, PluginJSONEncoder, masked_dict
from .waffle import waffle_switches, SIGNALS
log = logging.getLogger(__name__)
log.info("openedx_plugin.signals loaded")
def signals_enabled() -> bool:
try:
return waffle_switches[SIGNALS]
except Exception: # noqa: B902
# to resolve a race condition during application launch.
# the waffle_switches are inspected before the db service
# has initialized.
return False
"""
-------------------------------------------------------------------------------
------------------------------- LEGACY RECEIVERS ------------------------------
-------------------------------------------------------------------------------
"""
@receiver(user_logged_in, dispatch_uid="example_user_logged_in")
def post_login(sender, request, user, **kwargs): # lint-amnesty, pylint: disable=unused-argument
if not signals_enabled():
return
log.info("openedx_plugin received user_logged_in signal for {username}".format(username=user.username))
@receiver(user_logged_out, dispatch_uid="example_user_logged_out")
def post_logout(sender, request, user, **kwargs): # lint-amnesty, pylint: disable=unused-argument
if not signals_enabled():
return
log.info("openedx_plugin received user_logged_out signal for {username}".format(username=user.username))
@receiver(REGISTER_USER, dispatch_uid="example_REGISTER_USER")
def register_user(sender, user, registration, **kwargs): # pylint: disable=unused-argument
if not signals_enabled():
return
log.info("openedx_plugin received REGISTER_USER signal for {username}".format(username=user.username))
"""
-------------------------------------------------------------------------------
--------------------------- NEW STYLE OF RECEIVER -----------------------------
-------------------- https://github.com/openedx/openedx-events ----------------
-------------------------------------------------------------------------------
Reference: edx-platform/docs/guides/hooks/
https://github.com/openedx/openedx-events
I scaffolded these from https://github.com/eduNEXT/openedx-events-2-zapier
"""
def student_registration_completed(user, **kwargs): # pylint: disable=unused-argument
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.STUDENT_REGISTRATION_COMPLETED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L25
event_type: org.openedx.learning.student.registration.completed.v1
event_name: STUDENT_REGISTRATION_COMPLETED
event_description: emitted when the user registration process in the LMS is completed.
event_data: UserData
example user and kwargs data:
'user_id': 39,
'user_is_active': True,
'user_pii_username': 'test',
'user_pii_email': '[email protected]',
'user_pii_name': 'test',
'event_metadata_id': UUID('b1be2fac-1af1-11ec-bdf4-0242ac12000b'),
'event_metadata_event_type': 'org.openedx.learning.student.registration.completed.v1',
'event_metadata_minorversion': 0,
'event_metadata_source': 'openedx/lms/web',
'event_metadata_sourcehost': 'lms.devstack.edx',
'event_metadata_time': datetime.datetime(2021, 9, 21, 15, 36, 31, 311506),
'event_metadata_sourcelib': [0, 6, 0]
"""
if not signals_enabled():
return
user_info = asdict(user)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"user": user_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received STUDENT_REGISTRATION_COMPLETED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def session_login_completed(user, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.SESSION_LOGIN_COMPLETED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L37
event_type: org.openedx.learning.auth.session.login.completed.v1
event_name: SESSION_LOGIN_COMPLETED
event_description: emitted when the user's login process in the LMS is completed.
event_data: UserData
"""
if not signals_enabled():
return
user_info = asdict(user)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"user": user_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received SESSION_LOGIN_COMPLETED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def course_enrollment_created(enrollment, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.COURSE_ENROLLMENT_CREATED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L49
event_type: org.openedx.learning.course.enrollment.created.v1
event_name: COURSE_ENROLLMENT_CREATED
event_description: emitted when the user's enrollment process is completed.
event_data: CourseEnrollmentData
example enrollment and kwargs data:
'enrollment_user_id': 42,
'enrollment_user_is_active': True,
'enrollment_user_pii_username': 'test',
'enrollment_user_pii_email': '[email protected]',
'enrollment_user_pii_name': 'test',
'enrollment_course_course_key': 'course-v1:edX+100+2021',
'enrollment_course_display_name':'Demonstration Course',
'enrollment_course_start': None,
'enrollment_course_end': None,
'enrollment_mode':
'audit', 'enrollment_is_active': True,
'enrollment_creation_date': datetime.datetime(2021, 9, 21, 17, 40, 27, 401427, tzinfo=<UTC>),
'enrollment_created_by': None,
'event_metadata_id': UUID('02672f60-1b03-11ec-953b-0242ac12000b'),
'event_metadata_event_type': 'org.openedx.learning.course.enrollment.created.v1',
'event_metadata_minorversion': 0,
'event_metadata_source': 'openedx/lms/web',
'event_metadata_sourcehost': 'lms.devstack.edx',
'event_metadata_time': datetime.datetime(2021, 9, 21, 17, 40, 28, 81160),
'event_metadata_sourcelib': [0, 6, 0]
"""
if not signals_enabled():
return
enrollment_info = asdict(
enrollment,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"enrollment": enrollment_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received COURSE_ENROLLMENT_CREATED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def course_enrollment_changed(enrollment, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.COURSE_ENROLLMENT_CHANGED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L61
event_type: org.openedx.learning.course.enrollment.changed.v1
event_name: COURSE_ENROLLMENT_CHANGED
event_description: emitted when the user's enrollment update process is completed.
event_data: CourseEnrollmentData
"""
if not signals_enabled():
return
enrollment_info = asdict(
enrollment,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"enrollment": enrollment_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received COURSE_ENROLLMENT_CREATED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def course_unenrollment_completed(enrollment, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.COURSE_UNENROLLMENT_COMPLETED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L73
event_type: org.openedx.learning.course.unenrollment.completed.v1
event_name: COURSE_UNENROLLMENT_COMPLETED
event_description: emitted when the user's unenrollment process is completed.
event_data: CourseEnrollmentData
"""
if not signals_enabled():
return
enrollment_info = asdict(
enrollment,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"enrollment": enrollment_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received COURSE_UNENROLLMENT_COMPLETED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def certificate_created(certificate, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.COURSE_UNENROLLMENT_COMPLETED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L85
event_type: org.openedx.learning.certificate.created.v1
event_name: CERTIFICATE_CREATED
event_description: emitted when the user's certificate creation process is completed.
event_data: CertificateData
"""
if not signals_enabled():
return
certificate_info = asdict(
certificate,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"certificate": certificate_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received CERTIFICATE_CREATED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def certificate_changed(certificate, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.CERTIFICATE_CHANGED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L97
event_type: org.openedx.learning.certificate.changed.v1
event_name: CERTIFICATE_CHANGED
event_description: emitted when the user's certificate update process is completed.
event_data: CertificateData
"""
if not signals_enabled():
return
certificate_info = asdict(
certificate,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"certificate": certificate_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received CERTIFICATE_CHANGED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def certificate_revoked(certificate, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.CERTIFICATE_REVOKED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L109
event_type: org.openedx.learning.certificate.revoked.v1
event_name: CERTIFICATE_REVOKED
event_description: emitted when the user's certificate annulation process is completed.
event_data: CertificateData
"""
if not signals_enabled():
return
certificate_info = asdict(
certificate,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"certificate": certificate_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received CERTIFICATE_REVOKED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def persistent_grade_summary_changed(grade, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.PERSISTENT_GRADE_SUMMARY_CHANGED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L145
event_type: org.openedx.learning.course.persistent_grade.summary.v1
event_name: PERSISTENT_GRADE_SUMMARY_CHANGED
event_description: emitted when a grade changes in the course
event_data: PersistentCourseGradeData
example grade and kwargs data:
'grade_user_id': 42,
'grade_course_course_key': 'course-v1:edX+100+2021',
'grade_course_display_name': 'Demonstration Course',
'grade_course_edited_timestamp': datetime.datetime(2021, 9, 21, 17, 40, 27),
'grade_course_version': '',
'grade_grading_policy_hash': '',
'grade_percent_grade': 80,
'grade_letter_grade': 'Great',
'grade_passed_timestamp': datetime.datetime(2021, 9, 21, 17, 40, 27),
'event_metadata_id': UUID('b1be2fac-1af1-11ec-bdf4-0242ac12000b'),
'event_metadata_event_type': 'org.openedx.learning.student.registration.completed.v1',
'event_metadata_minorversion': 0,
'event_metadata_source': 'openedx/lms/web',
'event_metadata_sourcehost': 'lms.devstack.edx',
'event_metadata_time': datetime.datetime(2021, 9, 21, 15, 36, 31, 311506),
'event_metadata_sourcelib': [0, 6, 0]
"""
if not signals_enabled():
return
grade_info = asdict(
grade,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"grade": grade_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received COURSE_ENROLLMENT_CREATED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def cohort_membership_changed(cohort, **kwargs):
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.COHORT_MEMBERSHIP_CHANGED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L121
event_type: org.openedx.learning.cohort_membership.changed.v1
event_name: COHORT_MEMBERSHIP_CHANGED
event_description: emitted when the user's cohort update is completed.
event_data: CohortData
"""
if not signals_enabled():
return
cohort_info = asdict(
cohort,
value_serializer=serialize_course_key,
)
event_metadata = asdict(kwargs.get("metadata"))
payload = {
"cohort": cohort_info,
"event_metadata": event_metadata,
}
log.info(
"openedx_plugin received COHORT_MEMBERSHIP_CHANGED signal for {payload}".format(
payload=json.dumps(masked_dict(payload), cls=PluginJSONEncoder, indent=4)
)
)
def course_discussions_changed(configuration, **kwargs): # lint-amnesty, pylint: disable=unused-argument
"""
see apps.py plugin_app["signals_config"]["lms.djangoapp"]["receivers"]
signal_path: openedx_events.learning.signals.COHORT_MEMBERSHIP_CHANGED
https://github.com/openedx/openedx-events/blob/main/openedx_events/learning/signals.py#L145
event_type: org.openedx.learning.discussions.configuration.changed.v1
event_name: COURSE_DISCUSSIONS_CHANGED
event_description: emitted when the configuration for a course's discussions changes in the course
Warning: This event is currently incompatible with the event bus, list/dict cannot be serialized yet
event_data: CourseDiscussionConfigurationData
"""
if not signals_enabled():
return
log.info("openedx_plugin received COURSE_DISCUSSIONS_CHANGED signal")