Пример #1
0
def test_expand_translated_choice_sheets():
    s1 = {'survey': [{'type': 'select_one yn',
                      'label::En': 'English Select1',
                      'label::Fr': 'French Select1',
                      }],
          'choices': [{'list_name': 'yn',
                       'name': 'y',
                       'label::En': 'En Y',
                       'label::Fr': 'Fr Y',
                       },
                      {
                       'list_name': 'yn',
                       'name': 'n',
                       'label::En': 'En N',
                       'label::Fr': 'Fr N',
                      }],
          'translations': ['En', 'Fr']}
    expand_content(s1, in_place=True)
    assert s1 == {'survey': [{
                  'type': 'select_one',
                  'select_from_list_name': 'yn',
                  'label': ['English Select1', 'French Select1'],
                  }],
                  'choices': [{'list_name': 'yn',
                               'name': 'y',
                               'label': ['En Y', 'Fr Y'],
                               },
                              {
                               'list_name': 'yn',
                               'name': 'n',
                               'label': ['En N', 'Fr N'],
                               }],
                  'schema': SCHEMA_VERSION,
                  'translated': ['label'],
                  'translations': ['En', 'Fr']}
Пример #2
0
def test_expand_translations_null_lang():
    s1 = {
        'survey': [{
            'type': 'text',
            'label': 'NoLang',
            'label::English': 'EnglishLang'
        }],
        'translated': ['label'],
        'translations': [UNTRANSLATED, 'English'],
    }
    x1 = {
        'survey': [{
            'type': 'text',
            'label': ['NoLang', 'EnglishLang']
        }],
        'schema': SCHEMA_VERSION,
        'translated': ['label'],
        'translations': [UNTRANSLATED, 'English'],
    }
    s1_copy = copy.deepcopy(s1)
    expand_content(s1, in_place=True)
    assert s1.get('translations') == x1.get('translations')
    assert s1.get('translated') == ['label']
    assert s1.get('survey')[0] == x1.get('survey')[0]
    assert s1 == x1
    flatten_content(s1, in_place=True)
    s1_copy.pop('translated')
    s1_copy.pop('translations')
    assert s1 == s1_copy
Пример #3
0
def test_expand_translations():
    s1 = {
        'survey': [{
            'type': 'text',
            'label::English': 'OK?',
            'label::Français': 'OK!'
        }]
    }
    x1 = {
        'survey': [{
            'type': 'text',
            'label': ['OK?', 'OK!']
        }],
        'schema': SCHEMA_VERSION,
        'translated': ['label'],
        'translations': ['English', 'Français'],
    }
    expand_content(s1, in_place=True)
    assert s1 == x1
    flatten_content(s1, in_place=True)
    assert s1 == {
        'survey': [{
            'type': 'text',
            'label::English': 'OK?',
            'label::Français': 'OK!'
        }],
    }
Пример #4
0
def test_expand_constraint_message():
    s1 = {'survey': [{'type': 'integer',
                      'constraint': '. > 3',
                      'label::XX': 'X number',
                      'label::YY': 'Y number',
                      'constraint_message::XX': 'X: . > 3',
                      'constraint_message::YY': 'Y: . > 3',
                      }],
          'translated': ['constraint_message', 'label'],
          'translations': ['XX', 'YY']}
    s1_copy = copy.deepcopy(s1)
    x1 = {'survey': [{'type': 'integer',
                      'constraint': '. > 3',
                      'label': ['X number', 'Y number'],
                      'constraint_message': ['X: . > 3', 'Y: . > 3'],
                      }],
          'schema': SCHEMA_VERSION,
          'translated': ['constraint_message', 'label'],
          'translations': ['XX', 'YY'],
          }
    expand_content(s1, in_place=True)
    assert s1 == x1
    flatten_content(x1, in_place=True)
    s1_copy.pop('translated')
    s1_copy.pop('translations')
    assert x1 == s1_copy
Пример #5
0
def test_convert_select_objects():
    s1 = {
        'survey': [
            {
                'type': {
                    'select_one': 'xyz'
                }
            },
            {
                'type': {
                    'select_one_or_other': 'xyz'
                }
            },
            {
                'type': {
                    'select_multiple': 'xyz'
                }
            },
        ]
    }
    expand_content(s1, in_place=True)
    # print('_row', _row)
    _row = s1['survey'][0]
    assert _row['type'] == 'select_one'
    assert _row['select_from_list_name'] == 'xyz'

    _row = s1['survey'][1]
    assert _row['type'] == 'select_one'
    assert _row['select_from_list_name'] == 'xyz'

    _row = s1['survey'][2]
    assert _row['type'] == 'select_multiple'
    assert _row['select_from_list_name'] == 'xyz'
Пример #6
0
def test_expand_hints_and_labels():
    """
    This was an edge case that triggered some weird behavior
    """
    s1 = {
        'survey': [{
            'type': 'select_one yn',
            'label': 'null lang select1',
        }],
        'choices': [
            {
                'list_name': 'yn',
                'name': 'y',
                'label': 'y',
                'hint::En': 'En Y',
            },
            {
                'list_name': 'yn',
                'name': 'n',
                'label': 'n',
                'hint::En': 'En N',
            },
        ],
    }
    expand_content(s1, in_place=True)
    # Python3 raises a TypeError:
    # `'<' not supported between instances of 'NoneType' and 'str'`
    # when sorting a list with `None` values.
    # We need
    assert sorted(s1['translations'], key=orderable_with_none) == [None, 'En']
Пример #7
0
def test_expand_translated_media_with_no_translated():
    s1 = {
        'survey': [{
            'type': 'note',
            'media::image': 'nolang.jpg',
            'media::image::English': 'eng.jpg',
        }],
        'translations': ['English', UNTRANSLATED],
    }
    expand_content(s1, in_place=True)
    assert s1 == {
        'survey': [{
            'type': 'note',
            'media::image': ['eng.jpg', 'nolang.jpg']
        }],
        'schema': SCHEMA_VERSION,
        'translated': ['media::image'],
        'translations': ['English', UNTRANSLATED],
    }
    flatten_content(s1, in_place=True)
    assert s1 == {
        'survey': [{
            'type': 'note',
            'media::image': 'nolang.jpg',
            'media::image::English': 'eng.jpg',
        }],
    }
Пример #8
0
def standardize_content_in_place(content):
    if 'settings' not in content:
        content['settings'] = {}
    if 'survey' not in content:
        content['survey'] = []
    expand_content(content, in_place=True)
    replace_aliases(content, in_place=True, allowed_types=ALLOWED_TYPES)
Пример #9
0
def test_graceful_double_expand():
    s1 = {'survey': [{'type': 'note',
                      'label::English': 'english',
                      'hint::English': 'hint',
                      }]}
    content = expand_content(s1)
    assert content['translations'] == ['English']
    assert content['translated'] == ['hint', 'label']

    content = expand_content(content)
    assert content['translations'] == ['English']
    assert content['translated'] == ['hint', 'label']
Пример #10
0
def test_expand_translations():
    s1 = {'survey': [{'type': 'text',
                      'label::English': 'OK?',
                      'label::Français': 'OK!'}]}
    x1 = {'survey': [{'type': 'text',
                      'label': ['OK?', 'OK!']}],
          'schema': SCHEMA_VERSION,
          'translated': ['label'],
          'translations': ['English', 'Français']}
    expand_content(s1, in_place=True)
    assert s1 == x1
    flatten_content(s1, in_place=True)
    assert s1 == {'survey': [{'type': 'text',
                              'label::English': 'OK?',
                              'label::Français': 'OK!'}],
                  }
Пример #11
0
def test_convert_select_objects():
    s1 = {'survey': [{'type': {'select_one': 'xyz'}},
                     {'type': {'select_one_or_other': 'xyz'}},
                     {'type': {'select_multiple': 'xyz'}}
                     ]}
    expand_content(s1, in_place=True)
    # print('_row', _row)
    _row = s1['survey'][0]
    assert _row['type'] == 'select_one'
    assert _row['select_from_list_name'] == 'xyz'

    _row = s1['survey'][1]
    assert _row['type'] == 'select_one'
    assert _row['select_from_list_name'] == 'xyz'

    _row = s1['survey'][2]
    assert _row['type'] == 'select_multiple'
    assert _row['select_from_list_name'] == 'xyz'
Пример #12
0
def test_expand_media():
    s1 = {'survey': [{'type': 'note', 'media::image': 'ugh.jpg'}]}
    expand_content(s1, in_place=True)
    assert s1 == {
        'survey': [{
            'type': 'note',
            'media::image': ['ugh.jpg']
        }],
        'translated': ['media::image'],
        'translations': [UNTRANSLATED],
        'schema': SCHEMA_VERSION,
    }
    flatten_content(s1, in_place=True)
    assert s1 == {
        'survey': [{
            'type': 'note',
            'media::image': 'ugh.jpg',
        }],
    }
Пример #13
0
def test_expand_translated_media():
    s1 = {'survey': [{'type': 'note',
                      'media::image::English': 'eng.jpg'
                      }]}
    expand_content(s1, in_place=True)
    assert s1 == {'survey': [
            {'type': 'note',
                'media::image': ['eng.jpg']
             }
        ],
        'translated': ['media::image'],
        'schema': SCHEMA_VERSION,
        'translations': ['English']}
    flatten_content(s1, in_place=True)
    assert s1 == {'survey': [{
        'type': 'note',
        'media::image::English': 'eng.jpg',
      }],
      }
Пример #14
0
def test_expand_media():
    s1 = {'survey': [{'type': 'note',
                      'media::image': 'ugh.jpg'}]}
    expand_content(s1, in_place=True)
    assert s1 == {'survey': [
            {
              'type': 'note',
              'media::image': ['ugh.jpg']
            }
        ],
        'translated': ['media::image'],
        'translations': [UNTRANSLATED],
        'schema': SCHEMA_VERSION,
        }
    flatten_content(s1, in_place=True)
    assert s1 == {'survey': [{
        'type': 'note',
        'media::image': 'ugh.jpg',
      }],
    }
Пример #15
0
def test_expand_translations_null_lang():
    s1 = {'survey': [{'type': 'text',
                      'label': 'NoLang',
                      'label::English': 'EnglishLang'}],
          'translated': ['label'],
          'translations': [UNTRANSLATED, 'English']}
    x1 = {'survey': [{'type': 'text',
                      'label': ['NoLang', 'EnglishLang']}],
          'schema': SCHEMA_VERSION,
          'translated': ['label'],
          'translations': [UNTRANSLATED, 'English']}
    s1_copy = copy.deepcopy(s1)
    expand_content(s1, in_place=True)
    assert s1.get('translations') == x1.get('translations')
    assert s1.get('translated') == ['label']
    assert s1.get('survey')[0] == x1.get('survey')[0]
    assert s1 == x1
    flatten_content(s1, in_place=True)
    s1_copy.pop('translated')
    s1_copy.pop('translations')
    assert s1 == s1_copy
Пример #16
0
def test_expand_hints_and_labels():
    '''
    this was an edge case that triggered some weird behavior
    '''
    s1 = {'survey': [{'type': 'select_one yn',
                      'label': 'null lang select1',
                      }],
          'choices': [{'list_name': 'yn',
                       'name': 'y',
                       'label': 'y',
                       'hint::En': 'En Y',
                      },
                      {
                       'list_name': 'yn',
                       'name': 'n',
                       'label': 'n',
                       'hint::En': 'En N',
                      }],
          }
    expand_content(s1, in_place=True)
    assert sorted(s1['translations']) == [None, 'En']
Пример #17
0
    def save(self, *args, **kwargs):
        # populate summary
        if self.content is not None:
            if 'survey' in self.content:
                self._strip_empty_rows(self.content['survey'],
                                       required_key='type')
                self._assign_kuids(self.content['survey'])
                expand_content(self.content)
            if 'choices' in self.content:
                self._strip_empty_rows(self.content['choices'],
                                       required_key='name')
                self._assign_kuids(self.content['choices'])
            if 'settings' in self.content:
                if self.asset_type != 'survey':
                    del self.content['settings']
                else:
                    self._pull_form_title_from_settings()
        self._populate_summary()

        # infer asset_type only between question and block
        if self.asset_type in ['question', 'block']:
            row_count = self.summary.get('row_count')
            if row_count == 1:
                self.asset_type = 'question'
            elif row_count > 1:
                self.asset_type = 'block'

        new_content_json = json.dumps(self.content)
        if self._initial_content_json != new_content_json or (
                not self.pk or not self.versions().exists()):
            # Create a new version if the content has been changed, or if no
            # version exists yet
            with reversion.create_revision():
                super(Asset, self).save(*args, **kwargs)
            # Reset `_initial_content` since the change has been written to the
            # database
            self._initial_content = new_content_json
        else:
            super(Asset, self).save(*args, **kwargs)
Пример #18
0
def test_expand_translated_media_mangled_format():
    """
    An unfortunate bug seen in formpack#115 has resulted in needing to account
    for this behaviour if surveys used image::lang rather than
    media::image::lang
    """
    s1 = {
        'survey': [
            {
                'type': 'note',
                'media::image': ['eng.jpg'],
            },
        ],
        'translated': ['image'],  # Bug 🐛: not coming through as media::image
        'schema': SCHEMA_VERSION,
        'translations': ['English (en)'],
    }
    expand_content(s1, in_place=True)
    assert s1 == {
        'survey': [
            {
                'type': 'note',
                'media::image': ['eng.jpg'],
            },
        ],
        'translated': ['media::image'],
        'schema': SCHEMA_VERSION,
        'translations': ['English (en)'],
    }
    flatten_content(s1, in_place=True)
    assert s1 == {
        'survey': [
            {
                'type': 'note',
                'media::image::English (en)': 'eng.jpg',
            },
        ],
    }
Пример #19
0
def test_translated_label_hierarchy():
    survey = {
        'survey': [
            {
                'type': 'begin_group',
                'name': 'group',
                'label::English': 'Group',
                'label::Español': 'Grupo',
            },
            {
                'type': 'text',
                'name': 'question',
                'label::English': 'Question',
                'label::Español': 'Pregunta',
            },
            {
                'type': 'begin_repeat',
                'name': 'repeat',
                'label::English': 'Repeat',
                'label::Español': 'Repetición',
            },
            {
                'type': 'text',
                'name': 'repeated_question',
                'label::English': 'Repeated Question',
                'label::Español': 'Pregunta con repetición',
            },
            {
                'type': 'end_repeat'
            },
            {
                'type': 'end_group'
            },
        ]
    }
    schema = {'content': expand_content(survey), 'version': 1}
    version = FormPack([schema], 'title').versions[1]

    assert version.sections['title'].fields['question'].get_labels(
        hierarchy_in_labels=True, lang='English') == ['Group/Question']
    assert version.sections['title'].fields['question'].get_labels(
        hierarchy_in_labels=True, lang='Español') == ['Grupo/Pregunta']
    assert version.sections['repeat'].fields['repeated_question'].get_labels(
        hierarchy_in_labels=True,
        lang='English') == ['Group/Repeat/Repeated Question']
    assert version.sections['repeat'].fields['repeated_question'].get_labels(
        hierarchy_in_labels=True,
        lang='Español') == ['Grupo/Repetición/Pregunta con repetición']
Пример #20
0
def test_get_translated_cols():
    x1 = {'survey': [
          {'type': 'text', 'something::a': 'something-a', 'name': 'q1',
           'something_else': 'x'}
          ],
          'choices': [
          {'list_name': 'x', 'name': 'x1', 'something': 'something',
           'something_else::b': 'something_else::b'}
          ],
          'translations': [None]}
    expanded = expand_content(x1)
    assert expanded['translated'] == ['something', 'something_else']
    assert expanded['translations'] == [None, 'a', 'b']
    assert type(expanded['choices'][0]['something']) == list
    assert expanded['survey'][0]['something'] == [None, 'something-a', None]
    assert expanded['survey'][0]['something_else'] == ['x', None, None]
    assert expanded['choices'][0]['something'] == ['something', None, None]
Пример #21
0
def test_expand_select_multiple():
    s1 = {'survey': [{'type': 'select_multiple dogs'}]}
    expand_content(s1, in_place=True)
    assert s1['survey'][0]['type'] == 'select_multiple'
    assert s1['survey'][0]['select_from_list_name'] == 'dogs'
Пример #22
0
def test_expand_select_one_or_other():
    s1 = {'survey': [{'type': 'select_one dogs or_other'}]}
    expand_content(s1, in_place=True)
    assert s1['survey'][0]['type'] == 'select_one'
    assert s1['survey'][0]['select_from_list_name'] == 'dogs'
Пример #23
0
def test_expand_hxl_tags():
    s1 = {'survey': [{'type': 'text', 'hxl': '#tag+attr'}]}
    expand_content(s1, in_place=True)
    assert 'hxl' not in s1['survey'][0]
    assert s1['survey'][0]['tags'] == ['hxl:#tag', 'hxl:+attr']
Пример #24
0
 def to_formpack_schema(self):
     return {
         'content': expand_content(self._deployed_content()),
         'version': self.uid,
         'version_id_key': '__version__',
     }
Пример #25
0
 def to_formpack_schema(self):
     return {
         'content': expand_content(self._deployed_content()),
         'version': self.uid,
         'version_id_key': '__version__',
     }