def create_historical_records( objects, model, history_type, batch_size=None, default_user=None, default_change_reason="", default_date=None): """ Very similar to bulk_history_create which is a method of the HistoryManager within the simple history package. This one though allows the history type to be passed which is necessary because we are using a deletion history type. The manager method only supports create and update history types for this method. We need this because we have created our own bulk_delete_with_history below. """ if model._meta.proxy: history_manager = get_history_manager_for_model( model._meta.proxy_for_model) else: history_manager = get_history_manager_for_model(model) historical_instances = [] for instance in objects: history_user = getattr( instance, "_history_user", default_user or history_manager.model.get_default_history_user( instance), ) row = history_manager.model( history_date=getattr( instance, "_history_date", default_date or timezone.now() ), history_user=history_user, history_change_reason=get_change_reason_from_object( instance) or default_change_reason, history_type=history_type, **{ field.attname: getattr(instance, field.attname) for field in instance._meta.fields if field.name not in history_manager.model._history_excluded_fields } ) if hasattr(history_manager.model, "history_relation"): row.history_relation_id = instance.pk historical_instances.append(row) return history_manager.bulk_create( historical_instances, batch_size=batch_size )
def get_prev_record(self): """ Get the previous history record for the instance. `None` if first. """ history = utils.get_history_manager_for_model(self.instance) return (history.filter( Q(history_date__lt=self.history_date)).order_by( "history_date").last())
def get_next_record(self): """ Get the next history record for the instance. `None` if last. """ history = utils.get_history_manager_for_model(self.instance) return (history.filter( Q(history_date__gt=self.history_date)).order_by( "history_date").first())
def get_prev_record(self): """ Get the previous history record for the instance. `None` if first. """ history = utils.get_history_manager_for_model(self.instance) return ( history.filter(Q(history_date__lt=self.history_date)) .order_by("history_date") .last() )
def get_next_record(self): """ Get the next history record for the instance. `None` if last. """ history = utils.get_history_manager_for_model(self.instance) return ( history.filter(Q(history_date__gt=self.history_date)) .order_by("history_date") .first() )
def custom_update_change_reason(instance, reason): attrs = {} model = type(instance) manager = instance if instance.id is not None else model history = get_history_manager_for_model(manager) for field in (fld for fld in instance._meta.fields if fld.primary_key): attrs[field.attname] = getattr(instance, field.attname) if record := history.filter(**attrs).order_by("-history_date").first(): record.history_change_reason = reason record.save()
def move_annotations(): ''' 1) Move all annotations 2) Move phage one-by-one 3) Move features associated with phage 4) Move search results ''' print('Starting moving annotations.') histories = [] annos = [] useqs = set() for annotation in Annotation.objects.using('old-mas').all(): if annotation.sequence in useqs: continue else: useqs.add(annotation.sequence) for history in annotation.history.using('old-mas').iterator(): history.history_id = None if history.assigned_to_id: assigned_to = User.objects.using('old-mas').get(id=history.assigned_to_id) history.assigned_to = User.objects.get(username=assigned_to.username) if history.history_user_id: history_user = User.objects.using('old-mas').get(id=history.history_user_id) history.history_user = User.objects.get(username=history_user.username) # history.save(using='default') histories.append(history) annotation.id = None if annotation.assigned_to_id: assigned_to = User.objects.using('old-mas').get(id=annotation.assigned_to_id) annotation.assigned_to = User.objects.get(username=assigned_to.username) annos.append(annotation) # annotation.save(using='default') Annotation.objects.using('default').bulk_create(annos, batch_size=1000) get_history_manager_for_model(Annotation).using('default').bulk_create(histories, batch_size=1000) print('Done moving Annotations.')