Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BD-32] Hooks demo #210

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions common/djangoapps/student/signals/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
21 changes: 21 additions & 0 deletions lms/envs/devstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,24 @@ 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_EXTENSION_CONFIG = {
"openedx.lms.auth.post_register.action.v1": {
"pipeline": [
"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": [
"openedx_basic_hooks.filters.pre_register.check_year_of_birth",
],
"async": False
}

}
14 changes: 14 additions & 0 deletions openedx/core/djangoapps/user_authn/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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, 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
Expand Down Expand Up @@ -248,6 +250,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:
Expand Down Expand Up @@ -522,6 +526,16 @@ def post(self, request):
if response:
return response

try:
trigger_filter("openedx.lms.auth.pre_register.filter.v1", data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be data = trigger_filter("openedx.lms.auth.pre_register.filter.v1", data) right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do something like this.

out = trigger_filter(..)
data = out.get('data')

But in any case, I think that the changes to 'data' in the functions are already being stored in the data dictionary passed as parameter

Copy link
Member

@felipemontoya felipemontoya Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allright, out = trigger_filter(..) makes more sense.

Now, about the data being pass as reference, this could work, but we should not count on this. A filter could replace the dict with a new one and we would by mistake ignore that.

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
Expand Down