@disable_for_loaddata<EOL>def handle_user_post_save(sender, **kwargs):  
created = kwargs.get("<STR_LIT>", False)<EOL>user_instance = kwargs.get("<STR_LIT>", None)<EOL>if user_instance is None:<EOL><INDENT>return  <EOL><DEDENT>try:<EOL><INDENT>pending_ecu = PendingEnterpriseCustomerUser.objects.get(user_email=user_instance.email)<EOL><DEDENT>except PendingEnterpriseCustomerUser.DoesNotExist:<EOL><INDENT>return  <EOL><DEDENT>if not created:<EOL><INDENT>try:<EOL><INDENT>existing_record = EnterpriseCustomerUser.objects.get(user_id=user_instance.id)<EOL>message_template = "<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>logger.info(message_template.format(<EOL>user=user_instance, enterprise_customer=existing_record.enterprise_customer<EOL>))<EOL>pending_ecu.delete()<EOL>return<EOL><DEDENT>except EnterpriseCustomerUser.DoesNotExist:<EOL><INDENT>pass  <EOL><DEDENT><DEDENT>enterprise_customer_user = EnterpriseCustomerUser.objects.create(<EOL>enterprise_customer=pending_ecu.enterprise_customer,<EOL>user_id=user_instance.id<EOL>)<EOL>pending_enrollments = list(pending_ecu.pendingenrollment_set.all())<EOL>if pending_enrollments:<EOL><INDENT>def _complete_user_enrollment():  <EOL><INDENT>for enrollment in pending_enrollments:<EOL><INDENT>enterprise_customer_user.enroll(<EOL>enrollment.course_id, enrollment.course_mode, cohort=enrollment.cohort_name)<EOL>track_enrollment('<STR_LIT>', user_instance.id, enrollment.course_id)<EOL><DEDENT>pending_ecu.delete()<EOL><DEDENT>transaction.on_commit(_complete_user_enrollment)<EOL><DEDENT>else:<EOL><INDENT>pending_ecu.delete()<EOL><DEDENT>
Handle User model changes - checks if pending enterprise customer user record exists and upgrades it to actual link.

If there are pending enrollments attached to the PendingEnterpriseCustomerUser, then this signal also takes the
newly-created users and enrolls them in the relevant courses.