Skip to content

Commit

Permalink
Update records by bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
stveit committed Aug 5, 2024
1 parent ad8e7f8 commit ed13b2e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions python/nav/bin/collect_ouis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
# along with NAV. If not, see <http://www.gnu.org/licenses/>.
#

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__)
Expand Down Expand Up @@ -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
"""
Expand All @@ -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__':
Expand Down

0 comments on commit ed13b2e

Please sign in to comment.