Skip to content

Commit

Permalink
Merge pull request #83 from eduNEXT/and/force_user_post_save_callback
Browse files Browse the repository at this point in the history
Add force_user_post_save_callback pipeline
  • Loading branch information
andrey-canon authored Feb 7, 2020
2 parents 315e54c + aca46a5 commit 82b3afd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions eox_core/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
The pipeline module defines functions that are used in the third party authentication flow
"""
import logging

from django.db.models.signals import post_save

from eox_core.edxapp_wrapper.users import get_user_profile

LOG = logging.getLogger(__name__)
Expand All @@ -20,3 +23,44 @@ def ensure_user_has_profile(backend, details, user=None, *args, **kwargs):
except user_profile_model.DoesNotExist:
user_profile_model.objects.create(user=user)
LOG.info('Created new profile for user during the third party pipeline: "%s"', user)


def force_user_post_save_callback(auth_entry, is_new, user=None, *args, **kwargs):
"""
Send the signal post_save in order to force the execution of user_post_save_callback, see it in
https://github.com/eduNEXT/edunext-platform/blob/4169327231de00991c46b6192327fe50b0406561/common/djangoapps/student/models.py#L652
this allows the automatic enrollments if a user is registered by a third party auth.
This depends on "third_party_auth.pipeline.parse_query_params".
the discussion left three options:
1)
user._changed_fields = {'is_active': user.is_active} # pylint: disable=protected-access
post_save.send_robust(
sender=user.__class__,
instance=user,
created=False
)
2)
user.is_active = False
user.save()
user.is_active = True
user.save()
3)
registration = Registration.objects.get_or_create(user=user)
user.is_active = False
user.save()
registration.activate()
The first one was selected because the second executed multiple unnecessary process and third one
needs to implement a new backend.
"""
if auth_entry == 'register' and user and is_new:
user._changed_fields = {'is_active': user.is_active} # pylint: disable=protected-access
post_save.send_robust(
sender=user.__class__,
instance=user,
created=False
)

0 comments on commit 82b3afd

Please sign in to comment.