MCPcopy Create free account
hub / github.com/Flagsmith/flagsmith / create_audit_log_from_historical_record

Function create_audit_log_from_historical_record

api/audit/tasks.py:81–138  ·  view source on GitHub ↗
(  # type: ignore[no-untyped-def]
    history_instance_id: int,
    history_user_id: typing.Optional[int],
    history_record_class_path: str,
)

Source from the content-addressed store, hash-verified

79
80@register_task_handler(priority=TaskPriority.HIGHEST)
81def create_audit_log_from_historical_record( # type: ignore[no-untyped-def]
82 history_instance_id: int,
83 history_user_id: typing.Optional[int],
84 history_record_class_path: str,
85):
86 model_class = AuditLog.get_history_record_model_class(history_record_class_path)
87 history_instance = model_class.objects.get(history_id=history_instance_id) # type: ignore[attr-defined]
88
89 if (
90 history_instance.history_type == "~"
91 and history_instance.prev_record
92 and not history_instance.diff_against(history_instance.prev_record).changes
93 ):
94 return
95
96 instance = history_instance.instance
97 if instance.get_skip_create_audit_log():
98 return
99
100 if history_user_id is not None:
101 user_model = get_user_model()
102 history_user = user_model.objects.filter(id=history_user_id).first()
103 else:
104 history_user = instance.get_audit_log_author(history_instance)
105
106 if not (history_user or history_instance.master_api_key):
107 return
108
109 environment, project = instance.get_environment_and_project()
110
111 related_object_id = instance.get_audit_log_related_object_id(history_instance)
112 related_object_type = instance.get_audit_log_related_object_type(history_instance)
113
114 if not related_object_id:
115 return
116
117 log_message = {
118 "+": instance.get_create_log_message,
119 "-": instance.get_delete_log_message,
120 "~": instance.get_update_log_message,
121 }[history_instance.history_type](history_instance)
122
123 if not log_message:
124 return
125
126 AuditLog.objects.create(
127 history_record_id=history_instance.history_id,
128 history_record_class_path=history_record_class_path,
129 environment=environment,
130 project=project,
131 author=history_user,
132 related_object_id=related_object_id,
133 related_object_type=related_object_type.name,
134 log=log_message,
135 master_api_key=history_instance.master_api_key,
136 created_date=history_instance.history_date,
137 **instance.get_extra_audit_log_kwargs(history_instance),
138 )

Used in the wild real call sites across dependent graphs

searching dependent graphs…