Example #1
0
    def validate_for_build(self, validate_module):
        errors = []

        try:
            module = self.form.get_module()
        except AttributeError:
            module = None

        meta = {
            'form_type': self.form.form_type,
            'module': module.validator.get_module_info() if module else {},
            'form': {
                "id": self.form.id if hasattr(self.form, 'id') else None,
                "name": self.form.name,
                'unique_id': self.form.unique_id,
            }
        }

        xml_valid = False
        if self.form.source == '' and self.form.form_type != 'shadow_form':
            errors.append(dict(type="blank form", **meta))
        else:
            try:
                _parse_xml(self.form.source)
                xml_valid = True
            except XFormException as e:
                errors.append(dict(
                    type="invalid xml",
                    message=str(e) if self.form.source else '',
                    **meta
                ))
            except ValueError:
                logging.error("Failed: _parse_xml(string=%r)" % self.form.source)
                raise

        try:
            questions = self.form.cached_get_questions()
        except XFormException as e:
            error = {'type': 'validation error', 'validation_message': str(e)}
            error.update(meta)
            errors.append(error)

        if not errors:
            has_questions = any(not q.get('is_group') for q in questions)
            if not has_questions and self.form.form_type != 'shadow_form':
                errors.append(dict(type="blank form", **meta))
            else:
                try:
                    self.form.validate_form()
                except XFormValidationError as e:
                    error = {'type': 'validation error', 'validation_message': str(e)}
                    error.update(meta)
                    errors.append(error)
                except XFormValidationFailed:
                    pass  # ignore this here as it gets picked up in other places

        if self.form.post_form_workflow == WORKFLOW_FORM:
            if not self.form.form_links:
                errors.append(dict(type="no form links", **meta))
            for form_link in self.form.form_links:
                try:
                    self.form.get_app().get_form(form_link.form_id)
                except FormNotFoundException:
                    errors.append(dict(type='bad form link', **meta))
        elif self.form.post_form_workflow == WORKFLOW_MODULE:
            if module.put_in_root:
                errors.append(dict(type='form link to display only forms', **meta))
        elif self.form.post_form_workflow == WORKFLOW_PARENT_MODULE:
            if not module.root_module:
                errors.append(dict(type='form link to missing root', **meta))
            if module.root_module.put_in_root:
                errors.append(dict(type='form link to display only forms', **meta))

        # this isn't great but two of FormBase's subclasses have form_filter
        if hasattr(self.form, 'form_filter') and self.form.form_filter:
            with self.form.timing_context("validate_xpath"):
                is_valid, message = validate_xpath(self.form.form_filter, allow_case_hashtags=True)
            if not is_valid:
                error = {
                    'type': 'form filter has xpath error',
                    'xpath_error': message,
                }
                error.update(meta)
                errors.append(error)

        errors.extend(self.extended_build_validation(meta, xml_valid, validate_module))

        return errors
Example #2
0
    def validate_for_build(self, validate_module):
        errors = []

        try:
            module = self.form.get_module()
        except AttributeError:
            module = None

        meta = {
            'form_type': self.form.form_type,
            'module': module.validator.get_module_info() if module else {},
            'form': {
                "id": self.form.id if hasattr(self.form, 'id') else None,
                "name": self.form.name,
                'unique_id': self.form.unique_id,
            }
        }

        xml_valid = False
        if self.form.source == '' and self.form.form_type != 'shadow_form':
            errors.append(dict(type="blank form", **meta))
        else:
            try:
                _parse_xml(self.form.source)
                xml_valid = True
            except XFormException as e:
                errors.append(dict(
                    type="invalid xml",
                    message=six.text_type(e) if self.form.source else '',
                    **meta
                ))
            except ValueError:
                logging.error("Failed: _parse_xml(string=%r)" % self.form.source)
                raise

        try:
            questions = self.form.cached_get_questions()
        except XFormException as e:
            error = {'type': 'validation error', 'validation_message': six.text_type(e)}
            error.update(meta)
            errors.append(error)

        if not errors:
            has_questions = any(not q.get('is_group') for q in questions)
            if not has_questions and self.form.form_type != 'shadow_form':
                errors.append(dict(type="blank form", **meta))
            else:
                try:
                    self.form.validate_form()
                except XFormValidationError as e:
                    error = {'type': 'validation error', 'validation_message': six.text_type(e)}
                    error.update(meta)
                    errors.append(error)
                except XFormValidationFailed:
                    pass  # ignore this here as it gets picked up in other places

        if self.form.post_form_workflow == WORKFLOW_FORM:
            if not self.form.form_links:
                errors.append(dict(type="no form links", **meta))
            for form_link in self.form.form_links:
                try:
                    self.form.get_app().get_form(form_link.form_id)
                except FormNotFoundException:
                    errors.append(dict(type='bad form link', **meta))
        elif self.form.post_form_workflow == WORKFLOW_PARENT_MODULE:
            if not module.root_module:
                errors.append(dict(type='form link to missing root', **meta))

        # this isn't great but two of FormBase's subclasses have form_filter
        if hasattr(self.form, 'form_filter') and self.form.form_filter:
            with self.form.timing_context("validate_xpath"):
                is_valid, message = validate_xpath(self.form.form_filter, allow_case_hashtags=True)
            if not is_valid:
                error = {
                    'type': 'form filter has xpath error',
                    'xpath_error': message,
                }
                error.update(meta)
                errors.append(error)

        errors.extend(self.extended_build_validation(meta, xml_valid, validate_module))

        return errors