From ce258131df73e771c6a716c5ff071728295f9d87 Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Wed, 10 Mar 2021 13:17:27 -0400 Subject: [PATCH 1/4] Add post register action demo trigger --- lms/envs/devstack.py | 8 ++++++++ openedx/core/djangoapps/user_authn/exceptions.py | 1 - openedx/core/djangoapps/user_authn/views/register.py | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lms/envs/devstack.py b/lms/envs/devstack.py index 6a6ea3fffb27..32e597a8da47 100644 --- a/lms/envs/devstack.py +++ b/lms/envs/devstack.py @@ -432,3 +432,11 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing # Used in edx-proctoring for ID generation in lieu of SECRET_KEY - dummy value # (ref MST-637) PROCTORING_USER_OBFUSCATION_KEY = '85920908f28904ed733fe576320db18cabd7b6cd' +HOOKS_EXTENSIONS = { + "openedx.lms.auth.post_register.action.v1": { + "pipeline": [ + "openedx_basic_hooks.actions.post_register.post_register_tweet", + ], + "async": True + }, +} diff --git a/openedx/core/djangoapps/user_authn/exceptions.py b/openedx/core/djangoapps/user_authn/exceptions.py index 4dc1f2527672..de2c1aacc95b 100644 --- a/openedx/core/djangoapps/user_authn/exceptions.py +++ b/openedx/core/djangoapps/user_authn/exceptions.py @@ -1,6 +1,5 @@ """ User Authn related Exceptions. """ - from openedx.core.djangolib.markup import Text diff --git a/openedx/core/djangoapps/user_authn/views/register.py b/openedx/core/djangoapps/user_authn/views/register.py index 0ae77188a589..56179e9d9d2c 100644 --- a/openedx/core/djangoapps/user_authn/views/register.py +++ b/openedx/core/djangoapps/user_authn/views/register.py @@ -22,6 +22,7 @@ from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie from django.views.decorators.debug import sensitive_post_parameters from edx_django_utils.monitoring import set_custom_attribute +from edx_django_utils.hooks import trigger_action from edx_toggles.toggles import LegacyWaffleFlag, LegacyWaffleFlagNamespace from pytz import UTC from ratelimit.decorators import ratelimit @@ -248,6 +249,8 @@ def create_account_with_params(request, params): # Announce registration REGISTER_USER.send(sender=None, user=user, registration=registration) + trigger_action("openedx.lms.auth.post_register.action.v1", user, registration) + create_comments_service_user(user) try: From 55b309e3b74d5e012d0e841726072263dcb50eab Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Thu, 18 Mar 2021 15:19:47 -0400 Subject: [PATCH 2/4] Add pre register filter demo --- lms/envs/devstack.py | 9 ++++++++- openedx/core/djangoapps/user_authn/exceptions.py | 1 + .../core/djangoapps/user_authn/views/register.py | 13 ++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lms/envs/devstack.py b/lms/envs/devstack.py index 32e597a8da47..e9eec9df8f3d 100644 --- a/lms/envs/devstack.py +++ b/lms/envs/devstack.py @@ -432,11 +432,18 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing # Used in edx-proctoring for ID generation in lieu of SECRET_KEY - dummy value # (ref MST-637) PROCTORING_USER_OBFUSCATION_KEY = '85920908f28904ed733fe576320db18cabd7b6cd' -HOOKS_EXTENSIONS = { +HOOKS_EXTENSIONS_CONFIG = { "openedx.lms.auth.post_register.action.v1": { "pipeline": [ "openedx_basic_hooks.actions.post_register.post_register_tweet", ], "async": True }, + "openedx.lms.auth.pre_register.filter.v1": { + "pipeline": [ + "openedx_basic_hooks.filters.pre_register.check_year_of_birth", + ], + "async": False + } + } diff --git a/openedx/core/djangoapps/user_authn/exceptions.py b/openedx/core/djangoapps/user_authn/exceptions.py index de2c1aacc95b..4dc1f2527672 100644 --- a/openedx/core/djangoapps/user_authn/exceptions.py +++ b/openedx/core/djangoapps/user_authn/exceptions.py @@ -1,5 +1,6 @@ """ User Authn related Exceptions. """ + from openedx.core.djangolib.markup import Text diff --git a/openedx/core/djangoapps/user_authn/views/register.py b/openedx/core/djangoapps/user_authn/views/register.py index 56179e9d9d2c..74ce81dd4782 100644 --- a/openedx/core/djangoapps/user_authn/views/register.py +++ b/openedx/core/djangoapps/user_authn/views/register.py @@ -22,7 +22,8 @@ from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie from django.views.decorators.debug import sensitive_post_parameters from edx_django_utils.monitoring import set_custom_attribute -from edx_django_utils.hooks import trigger_action +from edx_django_utils.hooks import trigger_action, trigger_filter +from edx_django_utils.hooks.exceptions import HookException from edx_toggles.toggles import LegacyWaffleFlag, LegacyWaffleFlagNamespace from pytz import UTC from ratelimit.decorators import ratelimit @@ -525,6 +526,16 @@ def post(self, request): if response: return response + try: + trigger_filter("openedx.lms.auth.pre_register.filter.v1", data) + except HookException as err: + errors = { + "hook_error": [{"user_message": err.message}] + } + + return self._create_response(request, errors, status_code=err.status_code) + + response, user = self._create_account(request, data) if response: return response From fb46994dd4f6e1d9f96aff84b554fe2344575971 Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Fri, 19 Mar 2021 15:34:49 -0400 Subject: [PATCH 3/4] Add post enrollment trigger --- .../djangoapps/student/signals/receivers.py | 22 +++++++++++++++++++ lms/envs/devstack.py | 6 +++++ 2 files changed, 28 insertions(+) diff --git a/common/djangoapps/student/signals/receivers.py b/common/djangoapps/student/signals/receivers.py index 93cda209eeec..c7b672f126e2 100644 --- a/common/djangoapps/student/signals/receivers.py +++ b/common/djangoapps/student/signals/receivers.py @@ -10,9 +10,12 @@ from django.db.models.signals import post_save, pre_save from django.dispatch import receiver +from edx_django_utils.hooks import trigger_action + from lms.djangoapps.courseware.toggles import courseware_mfe_progress_milestones_are_active from common.djangoapps.student.helpers import EMAIL_EXISTS_MSG_FMT, USERNAME_EXISTS_MSG_FMT, AccountValidationError from common.djangoapps.student.models import CourseEnrollment, CourseEnrollmentCelebration, is_email_retired, is_username_retired # lint-amnesty, pylint: disable=line-too-long +from common.djangoapps.util.model_utils import get_changed_fields_dict @receiver(pre_save, sender=get_user_model()) @@ -70,3 +73,22 @@ def create_course_enrollment_celebration(sender, instance, created, **kwargs): except IntegrityError: # A celebration object was already created. Shouldn't happen, but ignore it if it does. pass + + +@receiver(pre_save, sender=CourseEnrollment) +def enrollment_pre_save_callback(sender, **kwargs): + """ + Capture old fields on the user instance before save and cache them as a + private field on the current model for use in the post_save callback. + """ + enrollment = kwargs['instance'] + enrollment._changed_fields = get_changed_fields_dict(enrollment, sender) # lint-amnesty, pylint: disable=protected-access + + +@receiver(post_save, sender=CourseEnrollment) +def trigger_post_enrollment_actions(sender, instance, created, **kwargs): + changed_fields = instance._changed_fields + if created or 'is_active' in changed_fields: + if instance.is_active: + print("triggering action") + trigger_action('openedx.lms.enrollment.post_enrollment.action.v1', instance, created) diff --git a/lms/envs/devstack.py b/lms/envs/devstack.py index e9eec9df8f3d..1d5dad7997bb 100644 --- a/lms/envs/devstack.py +++ b/lms/envs/devstack.py @@ -438,6 +438,12 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing "openedx_basic_hooks.actions.post_register.post_register_tweet", ], "async": True + }, + "openedx.lms.enrollment.post_enrollment.action.v1": { + "pipeline": [ + "openedx_basic_hooks.actions.post_enrollment.post_enrollment_tweet", + ], + "async": True }, "openedx.lms.auth.pre_register.filter.v1": { "pipeline": [ From 48f2090090d6a5f9e97acb628c51368147972554 Mon Sep 17 00:00:00 2001 From: mariagrimaldi Date: Wed, 24 Mar 2021 15:40:23 -0400 Subject: [PATCH 4/4] Update hooks configuration name --- lms/envs/devstack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lms/envs/devstack.py b/lms/envs/devstack.py index 1d5dad7997bb..9e1b9f416e1b 100644 --- a/lms/envs/devstack.py +++ b/lms/envs/devstack.py @@ -432,7 +432,7 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing # Used in edx-proctoring for ID generation in lieu of SECRET_KEY - dummy value # (ref MST-637) PROCTORING_USER_OBFUSCATION_KEY = '85920908f28904ed733fe576320db18cabd7b6cd' -HOOKS_EXTENSIONS_CONFIG = { +HOOKS_EXTENSION_CONFIG = { "openedx.lms.auth.post_register.action.v1": { "pipeline": [ "openedx_basic_hooks.actions.post_register.post_register_tweet",