mpri-webdam/profiles/management/commands/import_interests.py
2018-02-25 16:10:22 +01:00

49 lines
1.5 KiB
Python

""" Small module that import interests into the database.
"""
import json
from datetime import datetime
from django.core.management.base import BaseCommand
from django.db import models
from profiles.models import Keyword, Interest, Place, Website, Event
def import_file(filename):
with open(filename, mode='r') as file:
data = json.load(file)
for interest in data:
import_interest(interest)
def import_interest(_interest):
keywords = []
places = []
websites = []
for keyword in _interest.get("keywords", []):
if not Keyword.objects.get(keyword["keyword"]):
keywords.append(
Keyword(
text=keyword["keyword"]
)
)
print("New keyword %s" % new_keywords)
else:
keywords.append(Keyword.objects.get(text=keyword["keyword"]))
for place in _interest.get("places", []):
places.append(Place.objects.get(name=place["place"]))
for website in _interest.get("websites", []):
websites.append(Website.objects.get(name=website["website"]))
interest = Interest(
name=_interest.get("name", ""),
)
for keyword in keywords:
interest.keywords.add(keyword)
for place in places:
interest.places.add(place)
for website in websites:
interest.websites.add(website)
interest.save()
class Command(BaseCommand):
def handle(self, *args, **kwargs):
import_file("data/events.json")