@receiver(email_confirmed)<EOL>def linkUserToMostRecentCustomer(sender,**kwargs):
email_address = kwargs.get('<STR_LIT>',None)<EOL>if not email_address or not email_address.primary or not email_address.verified:<EOL><INDENT>return<EOL><DEDENT>user = email_address.user<EOL>if not hasattr(user, '<STR_LIT>'):<EOL><INDENT>last_reg = Registration.objects.filter(<EOL>customer__email=email_address.email,<EOL>customer__user__isnull=True,<EOL>dateTime__isnull=False<EOL>).order_by('<STR_LIT>').first()<EOL>if last_reg:<EOL><INDENT>customer = last_reg.customer<EOL>customer.user = user<EOL>customer.save()<EOL>if not user.first_name and not user.last_name:<EOL><INDENT>user.first_name = customer.first_name<EOL>user.last_name = customer.last_name<EOL>user.save()<EOL><DEDENT><DEDENT><DEDENT>
If a new primary email address has just been confirmed, check if the user
associated with that email has an associated customer object yet.  If not,
then look for the customer with that email address who most recently
registered for something and that is not associated with another user.
Automatically associate the User with with Customer, and if missing, fill in
the user's name information with the Customer's name.  This way, when a new
or existing customer creates a user account, they are seamlessly linked to
their most recent existing registration at the time they verify their email
address.