Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement single expression QA checks #715

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions apollo/formsframework/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from xlwt import Workbook

from apollo.formsframework.models import Form
from apollo.submissions.qa.query_builder import build_expression
from apollo.utils import generate_identifier

gt_constraint_regex = re.compile(r'(?:.*\.\s*\>={0,1}\s*)(\d+)')
Expand Down Expand Up @@ -182,6 +183,10 @@ def _process_qa_worksheet(qa_data):
if 'name' in qa_dict:
if current_name != qa_dict['name']:
if current_check is not None:
if 'expression' not in current_check:
current_check.update(
expression=build_expression(current_check))
current_check.pop('criteria', None)
quality_checks.append(current_check)
current_name = qa_dict['name']
current_check = {
Expand Down Expand Up @@ -210,9 +215,15 @@ def _process_qa_worksheet(qa_data):
'comparator': qa_dict['relation'],
'rvalue': qa_dict['right']
}
qa_check.update(expression=build_expression(qa_check))
qa_check.pop('comparator')
qa_check.pop('lvalue')
qa_check.pop('rvalue')
quality_checks.append(qa_check)

if current_check is not None:
current_check.update(expression=build_expression(current_check))
current_check.pop('criteria', None)
quality_checks.append(current_check)

return quality_checks
Expand Down Expand Up @@ -280,8 +291,7 @@ def export_form(form):
'accredited_voters_tag', 'invalid_votes_tag',
'registered_voters_tag', 'blank_votes_tag',
'quality_checks_enabled', 'vote_shares']
qa_header = ['name', 'description', 'left', 'relation', 'right',
'conjunction']
qa_header = ['name', 'description', 'expression']

# output headers
for col, value in enumerate(survey_header):
Expand Down Expand Up @@ -415,22 +425,10 @@ def export_form(form):
if quality_checks and qa_sheet:
row = 1
for check in quality_checks:
if 'criteria' in check:
for term in check['criteria']:
qa_sheet.write(row, 0, check['name'])
qa_sheet.write(row, 1, check['description'])
qa_sheet.write(row, 2, term['lvalue'])
qa_sheet.write(row, 3, term['comparator'])
qa_sheet.write(row, 4, term['rvalue'])
qa_sheet.write(row, 5, term['conjunction'])
row += 1
else:
if 'expression' in check:
qa_sheet.write(row, 0, check['name'])
qa_sheet.write(row, 1, check['description'])
qa_sheet.write(row, 2, check['lvalue'])
qa_sheet.write(row, 3, check['comparator'])
qa_sheet.write(row, 4, check['rvalue'])
qa_sheet.write(row, 5, '&&')
qa_sheet.write(row, 2, check['expression'])
row += 1

return book
23 changes: 3 additions & 20 deletions apollo/formsframework/views_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,9 @@ def quality_controls(view, form_id):

quality_control['name'] = quality_check['name']
quality_control['description'] = quality_check['description']
quality_control['criteria'] = []

if 'criteria' in quality_check:
for index, criterion in enumerate(quality_check['criteria']):
quality_control['criteria'].append({
'lvalue': criterion['lvalue'],
'comparator': criterion['comparator'],
'rvalue': criterion['rvalue'],
'conjunction': criterion['conjunction'],
'id': str(index)
})
else:
quality_control['criteria'].append({
'lvalue': quality_check['lvalue'],
'comparator': quality_check['comparator'],
'rvalue': quality_check['rvalue'],
'conjunction': '&&',
'id': '0'
})
if 'expression' in quality_check:
quality_control['expression'] = quality_check['expression']

quality_controls.append(quality_control)

Expand Down Expand Up @@ -421,8 +405,7 @@ def export_form(id):
workbook.save(memory_file)
memory_file.seek(0)
current_timestamp = datetime.utcnow()
filename = slugify(
f'{form.name}-{current_timestamp:%Y %m %d %H%M%S}') + '.xls'
filename = slugify(f'{form.name}-{current_timestamp:%Y %m %d %H%M%S}.xls')

return send_file(
memory_file, attachment_filename=filename,
Expand Down
Loading