Skip to content
Snippets Groups Projects
Commit a75cf469 authored by Remco Tukker's avatar Remco Tukker
Browse files

start of the actual mapping code

parent ef4e82ae
Branches
Tags
1 merge request!47Feature/comp 218 migrating 2022 data
......@@ -9,6 +9,7 @@ it can be used to prefill the 2023 survey.
"""
import logging
import click
import json
from sqlalchemy import delete, text, select
......@@ -20,6 +21,8 @@ from compendium_v2.survey_db import model as survey_model
from compendium_v2.db.model import NREN
from compendium_v2.db.survey_model import Survey, SurveyResponse
from compendium_v2.conversion.mapping import id_to_name
setup_logging()
logger = logging.getLogger('conversion')
......@@ -111,6 +114,49 @@ def query_nren(nren_id: int):
return answers
def convert_answers(answers):
data = {}
for id, question_name in id_to_name.items():
if not id in answers:
continue
if '{' in question_name:
continue
answer = answers[id]
if len(answer) > 1 and answer[0] == '"' and answer[-1] == '"':
answer = answer[1:-1]
if len(answer) > 1 and answer[0] == '[' and answer[-1] == ']':
answer = json.loads(answer)
question_names = question_name.split(":")
subdict = data
for name in question_names[0:-1]:
if name[-1] == "]":
index = name[-2]
if index == "[":
subdict = subdict.setdefault(name[:-2], [])
break # special case where json list is mapped to a list of dicts
sublist = subdict.setdefault(name[:-3], [])
index = int(index)
while len(sublist) <= index:
sublist.append({})
subdict = sublist[index]
else:
subdict = subdict.setdefault(name, {})
# print(question_names, answer)
if type(subdict) == list: # special case where json list is mapped to a list of dicts
for answer_entry in answer:
subdict.append({question_names[-1]: answer_entry})
else:
subdict[question_names[-1]] = answer
return {"data": data}
def _cli(app):
with app.app_context():
......@@ -132,12 +178,13 @@ def _cli(app):
db.session.add(survey)
for nren, answers in nren_surveys.items():
survey_dict = convert_answers(answers)
response = SurveyResponse(
nren=nren,
nren_id=nren.id,
survey_year=2022,
survey=survey,
answers=answers # TODO structure should be different
answers=survey_dict
)
db.session.add(response)
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment