61 lines
1.2 KiB
Python
61 lines
1.2 KiB
Python
import sys
|
|
import json
|
|
from src.articles.model import Article, FullArticleSchema
|
|
from src.sections.model import Section, FullSectionSchema
|
|
from src.database import db_session
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
if len(sys.argv) <= 1:
|
|
raise Error("Kein Zieldateiname angegeben")
|
|
|
|
|
|
def load_article(a):
|
|
return Article.deserialize(a[0])
|
|
|
|
def load_section(s):
|
|
return Section.deserialize(s[0])
|
|
|
|
|
|
|
|
file = open(sys.argv[1], "r")
|
|
|
|
data=json.load(file)
|
|
articles=None
|
|
sections=None
|
|
organizations=None
|
|
|
|
if isinstance(data,dict):
|
|
if data.has_key("articles"):
|
|
articles=data["articles"]
|
|
if data.has_key("sections"):
|
|
sections=data["sections"]
|
|
|
|
else:
|
|
articles=data
|
|
|
|
|
|
articles= map (load_article, articles)
|
|
if sections is not None:
|
|
sections=map(load_section, sections)
|
|
|
|
for a in articles:
|
|
try:
|
|
db_session.add(a)
|
|
db_session.commit()
|
|
except IntegrityError:
|
|
db_session.rollback()
|
|
finally:
|
|
db_session.rollback()
|
|
if sections is not None:
|
|
for s in sections:
|
|
if not isinstance(s,Section):
|
|
print type(s)
|
|
try:
|
|
db_session.add(s)
|
|
db_session.commit()
|
|
except IntegrityError:
|
|
db_session.rollback()
|
|
|
|
|
|
file.close()
|