def add_elements(self, elements: Iterable[AutoupdateElement]): """ Method to add elements to the history. This does not trigger autoupdate. """ with transaction.atomic(): instances = [] history_time = now() for element in elements: if element.get("disable_history"): # Do not update history if history is disabled. continue # HistoryData is not a root rest element so there is no autoupdate and not history saving here. data = HistoryData.objects.create( full_data=element.get("full_data")) instance = self.model( element_id=get_element_id(element["collection_string"], element["id"]), now=history_time, information=element.get("information", []), user_id=element.get("user_id"), full_data=data, ) instance.save() instances.append(instance) return instances
async def set_config(key, value): """ Set a config variable in the element_cache without hitting the database. """ collection_string = config.get_collection_string() config_id = config.key_to_id[key] # type: ignore full_data = {'id': config_id, 'key': key, 'value': value} await element_cache.change_elements( {get_element_id(collection_string, config_id): full_data}) await sync_to_async(inform_data_collection_element_list)([ CollectionElement.from_values(collection_string, config_id, full_data=full_data) ])
def _add_elements_other_dbs(self, elements, history_time): history_entries = [] for element in elements: # HistoryData is not a root rest element so there is no autoupdate and not history saving here. data = HistoryData.objects.create(full_data=element.get("full_data")) instance = self.model( element_id=get_element_id(element["collection_string"], element["id"]), now=history_time, information=element.get("information", []), user_id=element.get("user_id"), full_data=data, ) instance.save() history_entries.append(instance) return history_entries
def _add_elements_postgres(self, elements, history_time): """ Postgres supports returning ids from bulk requests, so after doing `bulk_create` every HistoryData has an id. This can be used to bulk_create History-Models in a second step. """ history_data = [ HistoryData(full_data=element.get("full_data")) for element in elements ] HistoryData.objects.bulk_create(history_data) history_entries = [ self.model( element_id=get_element_id(element["collection_string"], element["id"]), now=history_time, information=element.get("information", []), user_id=element.get("user_id"), full_data_id=hd.id, ) for element, hd in zip(elements, history_data) ] self.bulk_create(history_entries) return history_entries