def save_audit(migration_rules, migration_event, relic_ids, model_dict, audit_obj, instance, **kwargs): fields = model_dict['fields'] # the migrator uses downcase names model_name = to_object_name(fields['model']) model_id = relic_ids[model_name][fields['model_id']] if model_id == models.UNBOUND_MODEL_ID: print("cannot save this audit. " "The underlying model '%s' was discarded." % model_name) return None audit_obj.model_id = model_id if ((fields['field'] == 'id' and fields['current_value'] == fields['model_id'])): audit_obj.current_value = model_id # after the initial save, `created` can be updated without # getting clobbered by `auto_now_add`. # save the object, then set the created time. audit_obj.save() audit_obj.created = inflate_date(fields['created']) audit_obj.save() OTM1ModelRelic.objects.create(instance=instance, migration_event=migration_event, otm1_model_id=model_dict['pk'], otm2_model_name='audit', otm2_model_id=audit_obj.pk) return audit_obj
def save_treefavorite(migration_rules, migration_event, fav_dict, fav_obj, instance, **kwargs): fav_obj.save() fav_obj.created = inflate_date(fav_dict['fields']['date_created']) fav_obj.save() OTM1ModelRelic.objects.create( instance=instance, migration_event=migration_event, otm1_model_id=fav_dict['pk'], otm2_model_name='treefavorite', otm2_model_id=fav_obj.pk) return fav_obj
def save_treefavorite(migration_rules, migration_event, fav_dict, fav_obj, instance, **kwargs): raise NotImplementedError( 'TreeFavorite to Favorite conversion has not been implemented yet!') # TODO: The following code is likely to work, but has not been tested at all. # fav_obj.map_feature_id = (Tree # .objects # .values_list('plot__id', flat=True) # .get(pk=fav_dict.tree_id)) fav_obj.save() fav_obj.created = inflate_date(fav_dict['fields']['date_created']) fav_obj.save() OTM1ModelRelic.objects.create(instance=instance, migration_event=migration_event, otm1_model_id=fav_dict['pk'], otm2_model_name='favorite', otm2_model_id=fav_obj.pk) return fav_obj
def save_treefavorite(migration_rules, migration_event, fav_dict, fav_obj, instance, **kwargs): raise NotImplementedError( 'TreeFavorite to Favorite conversion has not been implemented yet!') # TODO: The following code is likely to work, but has not been tested at all. # fav_obj.map_feature_id = (Tree # .objects # .values_list('plot__id', flat=True) # .get(pk=fav_dict.tree_id)) fav_obj.save() fav_obj.created = inflate_date(fav_dict['fields']['date_created']) fav_obj.save() OTM1ModelRelic.objects.create( instance=instance, migration_event=migration_event, otm1_model_id=fav_dict['pk'], otm2_model_name='favorite', otm2_model_id=fav_obj.pk) return fav_obj
def save_audit(migration_rules, migration_event, relic_ids, model_dict, audit_obj, instance, **kwargs): fields = model_dict['fields'] # the migrator uses downcase names model_name = to_object_name(fields['model']) model_id = relic_ids[model_name][fields['model_id']] if model_id == models.UNBOUND_MODEL_ID: print("cannot save this audit. " "The underlying model '%s' was discarded." % model_name) return None audit_obj.model_id = model_id if ((fields['field'] == 'id' and fields['current_value'] == fields['model_id'])): audit_obj.current_value = model_id # after the initial save, `created` can be updated without # getting clobbered by `auto_now_add`. # save the object, then set the created time. audit_obj.save() audit_obj.created = inflate_date(fields['created']) audit_obj.save() OTM1ModelRelic.objects.create( instance=instance, migration_event=migration_event, otm1_model_id=model_dict['pk'], otm2_model_name='audit', otm2_model_id=audit_obj.pk) return audit_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