@receiver(post_registration)<EOL>def linkCustomerToVerifiedUser(sender, **kwargs):
registration = kwargs.get('<STR_LIT>', None)<EOL>if not registration or (hasattr(registration.customer,'<STR_LIT:user>') and registration.customer.user):<EOL><INDENT>return<EOL><DEDENT>logger.debug('<STR_LIT>')<EOL>customer = registration.customer<EOL>try:<EOL><INDENT>verified_email = EmailAddress.objects.get(<EOL>email=customer.email,<EOL>verified=True,<EOL>primary=True,<EOL>user__customer__isnull=True<EOL>)<EOL>logger.info("<STR_LIT>", verified_email.user.id, customer.id)<EOL>customer.user = verified_email.user<EOL>customer.save()<EOL>if not customer.user.first_name and not customer.user.last_name:<EOL><INDENT>customer.user.first_name = customer.first_name<EOL>customer.user.last_name = customer.last_name<EOL>customer.user.save()<EOL><DEDENT><DEDENT>except ObjectDoesNotExist:<EOL><INDENT>logger.info("<STR_LIT>", customer.id)<EOL><DEDENT>except MultipleObjectsReturned:<EOL><INDENT>errmsg = "<STR_LIT>"<EOL>logger.exception(errmsg, customer.email)<EOL><DEDENT>
If a Registration is processed in which the associated Customer does not yet
have a User, then check to see if the Customer's email address has been
verified as belonging to a specific User, and if that User has an associated
Customer.  If such a User is found, then associated this Customer with that
User.  This way, if a new User verifies their email account before they have
submitted any Registrations, their Customer account is seamlessly linked when
they do complete their first Registration.