mpri-webdam/profiles/management/commands/import_website.py
2018-02-25 11:49:11 +01:00

46 lines
1.3 KiB
Python

""" Small module that import events 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 Webpage, Website, Keyword
def import_file(filename):
with open(filename, mode='r') as file:
data = json.load(file)
for website in data:
import_website(website)
def import_website(_website):
keywords = []
webpages = []
for keyword in _website.get("keywords", []):
new_keyword = Keyword(
text=keyword.get("keyword", "")
)
new_keyword.save()
keywords.append(new_keyword)
for webpage in _website.get("notable_pages",[]):
new_webpage = Webpage(
url=webpage.get("keyword", "")
)
new_webpage.save()
webpages.append(new_webpage)
website = Website(
name=_website.get("name", ""),
url=_website.get("url", ""),
)
website.save()
for keyword in keywords:
website.keywords.add(keyword)
for webpage in webpages:
website.notable_pages.add(webpage)
print(website)
#website.save()
class Command(BaseCommand):
def handle(self, *args, **kwargs):
import_file("data/website.json")