Skip to content

Commit

Permalink
Merge 455e984 into b563d02
Browse files Browse the repository at this point in the history
  • Loading branch information
XiChenn committed Feb 7, 2023
2 parents b563d02 + 455e984 commit c55c289
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pybossa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,15 +1192,16 @@ def process_annex_load(tp_code, response_value):
regex = r"(\w+)\.(loadDocument|loadDocumentLite)\s*\(\s*.*\s*(\))"
matches = re.finditer(regex, tp_code)
count = 0
for match in matches: # maching for pure javascript Annex code
for match in matches: # matching docx Annex code
annex_tab = match[1] # the first group: (\w+)
code_to_append = f".then(() => {annex_tab}.loadAnnotationFromJson('{odfoa}'))"
right_parenthesis_end = match.end(3) + len(code_to_append) * count # the 3rd group: (\)) - exclusive
tp_code = tp_code[:right_parenthesis_end] + code_to_append + tp_code[right_parenthesis_end:]
count += 1

# Looking for code snippet like shell.setAttribute("hash", "abc");
regex = r"(\w+)\.setAttribute\(\"hash\",\s*\"\w+\"\);"
# Looking for code snippet like shell = document.getElementById("annex-viewer"); or
# shell = document.getElementById("shell-container");
regex = r"(\w+)\s*=\s*document\.getElementById\s*\(\s*('|\")?(annex-viewer|shell-container)('|\")?\s*\)\s*;"
matches = re.finditer(regex, tp_code)
count = 0
for match in matches:
Expand Down Expand Up @@ -1273,7 +1274,17 @@ def process_table_component(tp_code, user_response, task):
break
existing_data = request_fields
elif initial_data_str.strip().startswith("["): # if it is a list
existing_data = json.loads(initial_data_str)
try:
pattern = r"task\.info(\.\w+)+"
for m in re.finditer(pattern, initial_data_str):
old_str = m.group(0)
new_str = extract_task_info_data(task, old_str)
initial_data_str = initial_data_str.replace(old_str, new_str)
existing_data = json.loads(initial_data_str)
except Exception as e:
current_app.logger.error(
'parsing initial_data_str: {} error: {}'.format(
initial_data_str, str(e)))

# merge existing_data into response_value
for i in range(min(len(existing_data), len(response_values))):
Expand Down Expand Up @@ -1332,3 +1343,19 @@ def remove_element_attributes(page_element, attribute):
for attr in attributes:
if page_element.has_attr(attr):
del page_element[attr]


def extract_task_info_data(task, task_info_str):
"""
Extract data from javascript string.
e.g. task.info.a.b -> task.info.get('a').get('b')
:param task: task object
:param task_info_str: javascript task info string
:return: JSON string
"""
attributes = task_info_str.split(".")
request_fields = task.info
for attribute in attributes[2:]: # skip "task" and "info"
request_fields = request_fields.get(attribute, {})
return json.dumps(request_fields)
58 changes: 58 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,27 @@ def test_process_table_component_with_initial_data_from_task(self):
</div>
</table-element>
<table-element
:key='task.id'
:data='[{"cusip":task.info.CUSIP,"refix_date":task.info.FLT_REFIX_DT,"c":task.info.c.d,"e":task.info.c.e}]'
:columns='["cusip","refix_date","nested_data","c","e"]'
:options='{
"headings": {
"cusip": "Report CUSIP",
"refix_date": "Refix Date",
"nested_data": "Nested Data",
"c": "CCC",
"e": "EEE"
}
}'
column-id='__col_id'
>
</table-element>
<table-element
:key='task.id'
:data='[{"cusip":bad_string}]'
>
</table-element>
<task-timer>2</task-timer>
</task-presenter>
</div>
Expand Down Expand Up @@ -1204,6 +1225,12 @@ def test_process_table_component_with_initial_data_from_task(self):
'hgrt_variable_type': 'MONEY'}
]
},
'CUSIP': 'Test Cusip',
'FLT_REFIX_DT': ['test1', 'test2'],
'c': {
'd': 'nested d',
"e": 123
},
'ruleset_name': 'SR_Q1',
'work_item_completion_date': '3/24/2020'}
result = util.process_table_component(tp_code, user_response, task)
Expand All @@ -1216,6 +1243,10 @@ def test_process_table_component_with_initial_data_from_task(self):
assert expected_result in result
assert ':initial-value="props.row.QC_Reason"' in result
assert ':initial-value="props.row.QC_Notes"' in result
assert '"Test Cusip"' in result, result
assert '["test1", "test2"]' in result, result
assert '"nested d"' in result, result
assert '123' in result, result

def test_get_first_existing_data(self):
tag = BeautifulSoup('<b id="boldest">bold</b>', 'html.parser').b
Expand Down Expand Up @@ -1245,6 +1276,33 @@ def test_remove_element_attributes(self):
assert not tag.has_attr(":id")
assert not tag.has_attr("id")

@with_request_context
def test_extract_task_info_data(self):
project = ProjectFactory.create(published=True, id=1)
task = TaskFactory.create(id=101, project=project)

task.info = {'a': {'b': 'answer1'},
'c': 'answer2',
'd': ['hello', 'world'],
'e': 123
}

task_info_str = 'task.info.a.b'
res = util.extract_task_info_data(task, task_info_str)
assert res == '"answer1"', res

task_info_str = 'task.info.c'
res = util.extract_task_info_data(task, task_info_str)
assert res == '"answer2"', res

task_info_str = 'task.info.d'
res = util.extract_task_info_data(task, task_info_str)
assert res == '["hello", "world"]', res

task_info_str = 'task.info.e'
res = util.extract_task_info_data(task, task_info_str)
assert res == '123', res


class TestIsReservedName(object):
from test import flask_app as app
Expand Down

0 comments on commit c55c289

Please sign in to comment.