Skip to content

Commit

Permalink
wip: start working on import script
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Nov 12, 2024
1 parent 499e007 commit 2a55dab
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions apis_instance_nsvis/management/commands/import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import json
import pathlib

from django.core.management.base import BaseCommand


from apis_instance_nsvis.models import MongoDbModel, Person, Institution


def parse_photographer(photographer):
print(photographer)
for membership in photographer.get("membership", {}).get("membershiplist", []):
oid = {"$oid": membership["membership"]}
mdbm = MongoDbModel.objects.get(filename="membership", data___id=oid)
print(mdbm)


class Command(BaseCommand):
help = "Import data from MongoDB Export"

def add_arguments(self, parser):
parser.add_argument("file", type=pathlib.Path)

def handle(self, *args, **options):
for json_file in options["file"].glob("*.json"):
data = json.loads(json_file.read_text())
for item in data:
mdbm, created = MongoDbModel.objects.get_or_create(data=item, filename=json_file.name)
if mdbm.content_object is None:
if json_file.stem in ["photographers", "agencypeople"]:
mdbm.content_object = Person.objects.create()
mdbm.save()
if json_file.stem in ["agencies", "inventories", "memberships", "parties"]:
mdbm.content_object = Institution.objects.create()
mdbm.save()
match json_file.stem:
case "photographers":
basicdata = mdbm.data["basicdata"]
mdbm.content_object.surname=basicdata["surname"]
mdbm.content_object.forename=basicdata["name"]
mdbm.content_object.save()
parse_photographer(basicdata)
case "agencypeople":
mdbm.content_object.surname=mdbm.data["surname"]
mdbm.content_object.forename=mdbm.data["name"]
mdbm.content_object.save()
case "agencies":
mdbm.content_object.label=mdbm.data["name"]
mdbm.content_object.save()
case "inventories":
mdbm.content_object.label=mdbm.data["fullname"]
mdbm.content_object.save()
case "memberships":
mdbm.content_object.label=mdbm.data["fullname"]
mdbm.content_object.save()
case "parties":
mdbm.content_object.label=mdbm.data["fullname"]
mdbm.content_object.save()

0 comments on commit 2a55dab

Please sign in to comment.