Skip to content

Commit

Permalink
export profile (except the 1st one) with fake locations
Browse files Browse the repository at this point in the history
  • Loading branch information
pysailor committed May 24, 2015
1 parent 2f9dd3e commit 4521634
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions scripts/xml2md.py
Expand Up @@ -21,6 +21,7 @@

STATES = ['answered', 'postponed', 'unvisited']
EVALUATION_TYPES = ['risk_calculated', 'risk_estimated', 'risk_direct', 'priority', 'policy']
LOCATIONS = [u'Abby Road', u'Baker Street']

MAX_NUMBER_MODULES = 1
MAX_NUMBER_PROFILES = 2
Expand Down Expand Up @@ -213,7 +214,24 @@ def create_module(module, parent_id=None, number="1"):
risk_number = increment_number(risk_number)


def create_profile_question(profile_question, number="1"):
def create_location(location_number, location_name, parent_id):
id = str2filename(location_name)
content = u"""---
layout: location
fid: {fid}
number: {location_number}
title: {location_name}
parent_id: {parent_id}
---
""".format(
location_name=location_name,
location_number=location_number,
parent_id=parent_id,
fid=id)
write_md(id, content)


def create_profile_question(profile_question, number="1", locations=[]):
question_template = u"""---
layout: profile
fid: {id}
Expand All @@ -232,7 +250,7 @@ def create_profile_question(profile_question, number="1"):
fields = {
"id": id,
"title": title,
"number": number + '.0.0',
"number": len(locations) and "{0}.0.0.0".format(number) or "{0}.0.0".format(number),
"images": "",
"body": escape2markdown(description),
}
Expand All @@ -241,14 +259,28 @@ def create_profile_question(profile_question, number="1"):
write_md(id, content)

sub_modules = profile_question.findChildren("module", recursive=False)
sub_number = number + ".1"
sub_count = 0
for sub_module in sub_modules:
create_module(sub_module, parent_id=id, number=sub_number)
sub_number = increment_number(sub_number)
sub_count += 1
if sub_count >= MAX_NUMBER_SUBMODULES:
break
if len(locations):
for i in range(len(locations)):
location_number = "{0}.{1}.0.0".format(number, i + 1)
create_location(location_number, locations[i], parent_id=id)
sub_number = "{0}.{1}.1".format(number, i + 1)
location_id = "{0}-{1}".format(id, str2filename(locations[i]))
sub_count = 0
for sub_module in sub_modules:
create_module(sub_module, parent_id=location_id, number=sub_number)
sub_number = increment_number(sub_number)
sub_count += 1
if sub_count >= MAX_NUMBER_SUBMODULES:
break
else:
sub_number = number + ".1"
sub_count = 0
for sub_module in sub_modules:
create_module(sub_module, parent_id=id, number=sub_number)
sub_number = increment_number(sub_number)
sub_count += 1
if sub_count >= MAX_NUMBER_SUBMODULES:
break


if __name__ == "__main__":
Expand Down Expand Up @@ -276,6 +308,7 @@ def create_profile_question(profile_question, number="1"):
os.makedirs(media_file_path)

number = 1
profile_number = 1

profile_questions = survey.findChildren("profile-question",
recursive=False)
Expand All @@ -284,8 +317,12 @@ def create_profile_question(profile_question, number="1"):
if MAX_NUMBER_PROFILES and number > MAX_NUMBER_PROFILES:
break
print "Export Profile Question '{0}'".format(profile_question.title.text)
create_profile_question(profile_question, number=str(number))
if profile_number > 1:
create_profile_question(profile_question, number=str(number), locations=LOCATIONS)
else:
create_profile_question(profile_question, number=str(number))
number += 1
profile_number += 1

modules = survey.findChildren("module", recursive=False)
for module in modules:
Expand Down

0 comments on commit 4521634

Please sign in to comment.