From ed13b2e1b56392856f0ae2449a826bae8bce26a3 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 2 Aug 2024 10:51:04 +0200 Subject: [PATCH] Update records by bulk --- python/nav/bin/collect_ouis.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/python/nav/bin/collect_ouis.py b/python/nav/bin/collect_ouis.py index 0690f31965..3ec4a61fcd 100755 --- a/python/nav/bin/collect_ouis.py +++ b/python/nav/bin/collect_ouis.py @@ -15,13 +15,15 @@ # along with NAV. If not, see . # -from typing import List, Tuple +from typing import List, Iterable import sys import requests from requests.exceptions import RequestException +from django.db import transaction + from nav.bootstrap import bootstrap_django bootstrap_django(__file__) @@ -50,14 +52,15 @@ def download_oui_file(url: str) -> str: return response.text -def update_database(oui_data: List[Tuple[str, str]]) -> List[OUI]: - for oui, vendor in oui_data: - print(f"Registering OUI {oui} for vendor {vendor}") - instance, _ = OUI.objects.update_or_create(oui=oui, defaults={"vendor": vendor}) - instance.save() +@transaction.atomic +def update_database(ouis: Iterable[OUI]): + print("Deleting existing records") + OUI.objects.all().delete() + print("Creating new records") + OUI.objects.bulk_create(ouis, ignore_conflicts=True) -def parse_ouis(oui_data: str) -> List[Tuple[str, str]]: +def parse_ouis(oui_data: str) -> List[OUI]: """Returns lists of tuples containing OUI and vendor name for each vendor """ @@ -69,13 +72,13 @@ def parse_ouis(oui_data: str) -> List[Tuple[str, str]]: return oui_list -def parse_line(line: str) -> Tuple[str, str]: +def parse_line(line: str) -> OUI: line = line.strip() split_line = line.split() oui = split_line[0] + "-00-00-00" split_vendor = split_line[3:] vendor = " ".join(split_vendor) - return oui, vendor + return OUI(oui=oui, vendor=vendor) if __name__ == '__main__':