-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |