Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #41 from praekelt/feature/issue-41-fix-boolean-ope…
Browse files Browse the repository at this point in the history
…rator-bugs

Fix boolean operator bugs
  • Loading branch information
nathanbegbie committed Nov 7, 2017
2 parents a0a3fd4 + dd6f272 commit 0062e28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
8 changes: 1 addition & 7 deletions molo/surveys/migrations/0018_add_combination_rule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-11-07 07:01
# Generated by Django 1.9.13 on 2017-11-07 09:55
from __future__ import unicode_literals

from django.db import migrations, models
Expand All @@ -13,7 +13,6 @@
class Migration(migrations.Migration):

dependencies = [
('wagtail_personalisation', '0013_auto_20171026_1748'),
('surveys', '0017_make_count_positive_integer'),
]

Expand All @@ -29,9 +28,4 @@ class Migration(migrations.Migration):
'verbose_name': 'Rule Combination',
},
),
migrations.AlterField(
model_name='articletagrule',
name='date_to',
field=models.DateTimeField(blank=True, help_text='All times are UTC. Leave both fields blank to search all time.', null=True),
),
]
47 changes: 26 additions & 21 deletions molo/surveys/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,31 +394,36 @@ def description(self):

def clean(self):
super(CombinationRule, self).clean()
if isinstance(self.body.stream_data[0], dict):
newData = [block['type'] for block in self.body.stream_data]
elif isinstance(self.body.stream_data[0], tuple):
newData = [block[0] for block in self.body.stream_data]
if len(self.body.stream_data) > 0:
if isinstance(self.body.stream_data[0], dict):
newData = [block['type'] for block in self.body.stream_data]
elif isinstance(self.body.stream_data[0], tuple):
newData = [block[0] for block in self.body.stream_data]

if (len(newData) - 1) % 2 != 0:
if len(newData) == 1 or (len(newData) - 1) % 2 != 0:
raise StreamBlockValidationError(non_block_errors=[_(
'Rule Combination must follow the <Rule/NestedLogic>'
'<Operator> <Rule/NestedLogic> pattern.')])

iterations = (len(newData) - 1) / 2
for i in range(iterations):
first_rule_index = i * 2
operator_index = (i * 2) + 1
second_rule_index = (i * 2) + 2

if not (
(newData[first_rule_index] == 'Rule' or
newData[first_rule_index] == 'NestedLogic') and
(newData[operator_index] == 'Operator') and
(newData[second_rule_index] == 'Rule' or
newData[second_rule_index] == 'NestedLogic')):
raise StreamBlockValidationError(non_block_errors=[_(
'Rule Combination must follow the <Rule/NestedLogic> '
'<Operator> <Rule/NestedLogic> pattern.')])
else:
raise StreamBlockValidationError(non_block_errors=[_(
'Rule Combination must follow the <Rule/NestedLogic>'
'<Operator> <Rule/NestedLogic> pattern.')])

iterations = (len(newData) - 1) / 2
for i in range(iterations):
first_rule_index = i * 2
operator_index = (i * 2) + 1
second_rule_index = (i * 2) + 2

if not (
(newData[first_rule_index] == 'Rule' or
newData[first_rule_index] == 'NestedLogic') and
(newData[operator_index] == 'Operator') and
(newData[second_rule_index] == 'Rule' or
newData[second_rule_index] == 'NestedLogic')):
raise StreamBlockValidationError(non_block_errors=[_(
'Rule Combination must follow the <Rule/NestedLogic> '
'<Operator> <Rule/NestedLogic> pattern.')])

class Meta:
verbose_name = _('Rule Combination')

0 comments on commit 0062e28

Please sign in to comment.