def save_user(migration_rules, migration_event, user_dict, user_obj, instance): """ Save otm1 user record into otm2. In the case of validation problems, username can be arbitrarily changed. Since we log the otm1_username in the relic, it's simple enough to query for all users that have a different username than the one stored in their relic and take further action as necessary. """ # don't save another user if this email address already exists. # just observe and report users_with_this_email = User.objects.filter( email__iexact=user_dict['fields']['email']) if users_with_this_email.exists(): user_obj = users_with_this_email[0] else: # replace spaces in the username. # then, if the sanitized username already exists, # uniquify it. This transformation order is important. # uniquification must happen as the last step. user_obj = user_obj user_obj.username = uniquify_username( sanitize_username(user_obj.username)) user_obj.save() try: InstanceUser.objects.get(instance=instance, user=user_obj) except InstanceUser.DoesNotExist: InstanceUser.objects.create(instance=instance, user=user_obj, role=instance.default_role) relic = OTM1UserRelic(instance=instance, migration_event=migration_event, otm2_model_id=user_obj.pk, otm1_model_id=user_dict['pk'], otm1_username=user_dict['fields']['username'], email=user_dict['fields']['email']) relic.save() return user_obj
def save_user(migration_rules, migration_event, user_dict, user_obj, instance, **kwargs): """ Save otm1 user record into otm2. In the case of validation problems, username can be arbitrarily changed. Since we log the otm1_username in the relic, it's simple enough to query for all users that have a different username than the one stored in their relic and take further action as necessary. """ otm1_email = user_dict['fields']['email'] assert otm1_email != '' # don't save another user if this email address already exists. # just observe and report users_with_this_email = User.objects.filter(email__iexact=otm1_email) if users_with_this_email.exists(): assert len(users_with_this_email) == 1 user_obj = users_with_this_email[0] last_login = inflate_date(user_dict['fields']['last_login']) date_joined = inflate_date(user_dict['fields']['date_joined']) first_name = user_dict['fields']['first_name'] last_name = user_dict['fields']['last_name'] # coerce to UTC. This may not be perfectly correct, but # given how unlikely it is for a user to create a new account # on the same day they logged in with an existing account, # we can live with the ambiguity. if last_login.replace(tzinfo=pytz.UTC) > user_obj.last_login: user_obj.username = user_dict['fields']['username'] user_obj.password = user_dict['fields']['password'] user_obj.last_login = last_login # we're keeping the *login* info for the most recently used # account, but the *joined* info for the earliest created account. if date_joined.replace(tzinfo=pytz.UTC) < user_obj.date_joined: user_obj.date_joined = date_joined if first_name: user_obj.first_name = first_name if last_name: user_obj.last_name = last_name if user_dict['fields']['is_active']: user_obj.is_active = True else: # replace spaces in the username. # then, if the sanitized username already exists, # uniquify it. This transformation order is important. # uniquification must happen as the last step. user_obj = user_obj user_obj.username = uniquify_username( sanitize_username(user_obj.username)) user_obj.save() try: InstanceUser.objects.get(instance=instance, user=user_obj) except InstanceUser.DoesNotExist: InstanceUser.objects.create(instance=instance, user=user_obj, role=instance.default_role) relic = OTM1UserRelic(instance=instance, migration_event=migration_event, otm2_model_id=user_obj.pk, otm1_model_id=user_dict['pk'], otm1_username=user_dict['fields']['username'], email=otm1_email) relic.save() return user_obj