def test_xml(self):
     bad_name = "xml_and_more"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #2
0
 def test_punctuation(self):
     bad_name = "ノイ丂 ム イ尺ムア! \_(ツ)_/¯"
     self.assertTrue(is_identifier_invalid(bad_name))
 def test_unicode(self):
     bad_name = u"ノイ丂 ム イ尺ムア! \_(ツ)_/¯"
     self.assertTrue(is_identifier_invalid(bad_name))
 def test_slash(self):
     bad_name = "will/crash"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #5
0
def update_tables(request, domain, data_type_id, test_patch=None):
    """
    receives a JSON-update patch like following
    {
        "_id":"0920fe1c6d4c846e17ee33e2177b36d6",
        "tag":"growth",
        "view_link":"/a/gsid/fixtures/view_lookup_tables/?table_id:0920fe1c6d4c846e17ee33e2177b36d6",
        "is_global":false,
        "fields":{"genderr":{"update":"gender"},"grade":{}}
    }
    """
    if test_patch is None:
        test_patch = {}
    if data_type_id:
        try:
            data_type = FixtureDataType.get(data_type_id)
        except ResourceNotFound:
            raise Http404()

        assert(data_type.doc_type == FixtureDataType._doc_type)
        assert(data_type.domain == domain)

        if request.method == 'GET':
            return json_response(strip_json(data_type))

        elif request.method == 'DELETE':
            with CouchTransaction() as transaction:
                data_type.recursive_delete(transaction)
            clear_fixture_cache(domain)
            return json_response({})
        elif not request.method == 'PUT':
            return HttpResponseBadRequest()

    if request.method == 'POST' or request.method == "PUT":
        fields_update = test_patch or _to_kwargs(request)
        fields_patches = fields_update["fields"]
        data_tag = fields_update["tag"]
        is_global = fields_update["is_global"]

        # validate tag and fields
        validation_errors = []
        if is_identifier_invalid("{}_list".format(data_tag)):
            validation_errors.append(data_tag)
        for field_name, options in fields_update['fields'].items():
            method = list(options.keys())
            if 'update' in method:
                field_name = options['update']
            if is_identifier_invalid(field_name) and 'remove' not in method:
                validation_errors.append(field_name)
        validation_errors = [_(
            "\"%s\" cannot include special characters, begin or end with a space, "
            "or begin with \"xml\" or a number") % e for e in validation_errors
        ]
        if len(data_tag) < 1 or len(data_tag) > 31:
            validation_errors.append(_("Table ID must be between 1 and 31 characters."))

        if validation_errors:
            return json_response({
                'validation_errors': validation_errors,
                'error_msg': _(
                    "Could not update table because field names were not "
                    "correctly formatted"),
            })

        with CouchTransaction() as transaction:
            if data_type_id:
                data_type = update_types(fields_patches, domain, data_type_id, data_tag, is_global, transaction)
                update_items(fields_patches, domain, data_type_id, transaction)
            else:
                if FixtureDataType.fixture_tag_exists(domain, data_tag):
                    return HttpResponseBadRequest("DuplicateFixture")
                else:
                    data_type = create_types(fields_patches, domain, data_tag, is_global, transaction)
        clear_fixture_cache(domain)
        return json_response(strip_json(data_type))
 def test_combo(self):
     bad_name = "<space>\<dadgg sd"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #7
0
 def test_starts_with_number(self):
     bad_name = "0hello"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #8
0
 def test_combo(self):
     bad_name = "<space>\<dadgg sd"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #9
0
    def from_row(cls, row_dict):
        tag = row_dict.get('table_id') or row_dict.get('tag')
        if tag is None:
            raise FixtureUploadError([
                _(FAILURE_MESSAGES['has_no_column']).format(column_name='table_id')])
        if is_identifier_invalid(tag):
            raise FixtureUploadError([
                _(FAILURE_MESSAGES['invalid_table_id']).format(tag=tag)
            ])

        field_names = row_dict.get('field')
        item_attributes = row_dict.get('property')

        if field_names is None and item_attributes is None:
            raise FixtureUploadError([_(FAILURE_MESSAGES['neither_fields_nor_attributes']).format(tag=tag)])

        field_names = [] if field_names is None else field_names
        item_attributes = [] if item_attributes is None else item_attributes

        def _get_field_properties(prop_key):
            properties = []
            if prop_key in row_dict and 'property' in row_dict[prop_key]:
                properties = row_dict[prop_key]["property"]
                if not isinstance(properties, list):
                    error_message = _(FAILURE_MESSAGES["wrong_property_syntax"]).format(
                        prop_key=prop_key,
                    )
                    raise FixtureUploadError([error_message])
            return properties

        def _get_field_is_indexed(prop_key):
            is_indexed = False
            if prop_key in row_dict and 'is_indexed' in row_dict[prop_key]:
                is_indexed = row_dict[prop_key]["is_indexed"]
                if not isinstance(is_indexed, bool):
                    error_message = _(FAILURE_MESSAGES["wrong_index_syntax"]).format(
                        prop_key=prop_key,
                    )
                    raise FixtureUploadError([error_message])
            return is_indexed

        def is_number(text):
            text = six.text_type(text)
            try:
                float(text)
                return True
            except ValueError:
                return False

        for i, field_name in enumerate(field_names):
            if is_number(field_name):
                message = _(FAILURE_MESSAGES['invalid_field_name_numerical']).format(
                    i=i + 1,
                    val=field_name,
                )
                raise FixtureUploadError([message])

        fields = [
            FixtureTypeField(
                field_name=field,
                properties=_get_field_properties('field {count}'.format(count=i + 1)),
                is_indexed=_get_field_is_indexed('field {count}'.format(count=i + 1)),
            ) for i, field in enumerate(field_names)
        ]

        return cls(
            table_id=tag,
            fields=fields,
            item_attributes=item_attributes,
            is_global=row_dict.get('is_global', False),
            uid=row_dict.get('UID'),
            delete=(row_dict.get(DELETE_HEADER) or '').lower() == 'y',
        )
Example #10
0
def update_tables(request, domain, data_type_id):
    """
    receives a JSON-update patch like following
    {
        "_id":"0920fe1c6d4c846e17ee33e2177b36d6",
        "tag":"growth",
        "view_link":"/a/gsid/fixtures/view_lookup_tables/?table_id:0920fe1c6d4c846e17ee33e2177b36d6",
        "is_global":false,
        "fields":{"genderr":{"update":"gender"},"grade":{}}
    }
    """
    if data_type_id:
        try:
            data_type = FixtureDataType.get(data_type_id)
        except ResourceNotFound:
            raise Http404()

        if data_type.domain != domain:
            raise Http404()

        if request.method == 'GET':
            return json_response(strip_json(data_type))

        elif request.method == 'DELETE':
            with CouchTransaction() as transaction:
                data_type.recursive_delete(transaction)
            clear_fixture_cache(domain)
            return json_response({})
        elif not request.method == 'PUT':
            return HttpResponseBadRequest()

    if request.method == 'POST' or request.method == "PUT":
        fields_update = _to_kwargs(request)
        fields_patches = fields_update["fields"]
        data_tag = fields_update["tag"]
        is_global = fields_update["is_global"]
        description = fields_update["description"]

        # validate tag and fields
        validation_errors = []
        if is_identifier_invalid("{}_list".format(data_tag)):
            validation_errors.append(data_tag)
        for field_name, options in fields_update['fields'].items():
            method = list(options.keys())
            if 'update' in method:
                field_name = options['update']
            if is_identifier_invalid(field_name) and 'remove' not in method:
                validation_errors.append(field_name)
        validation_errors = [
            _("\"%s\" cannot include special characters, begin or end with a space, "
              "or begin with \"xml\" or a number") % e
            for e in validation_errors
        ]
        if len(data_tag) < 1 or len(data_tag) > 31:
            validation_errors.append(
                _("Table ID must be between 1 and 31 characters."))

        if validation_errors:
            return json_response({
                'validation_errors':
                validation_errors,
                'error_msg':
                _("Could not update table because field names were not "
                  "correctly formatted"),
            })

        with CouchTransaction() as transaction:
            if data_type_id:
                data_type = _update_types(fields_patches, domain, data_type_id,
                                          data_tag, is_global, description,
                                          transaction)
                _update_items(fields_patches, domain, data_type_id,
                              transaction)
            else:
                if FixtureDataType.fixture_tag_exists(domain, data_tag):
                    return HttpResponseBadRequest("DuplicateFixture")
                else:
                    data_type = _create_types(fields_patches, domain, data_tag,
                                              is_global, description,
                                              transaction)
        clear_fixture_cache(domain)
        return json_response(strip_json(data_type))
Example #11
0
    def from_row(cls, row_dict):
        tag = row_dict.get('table_id') or row_dict.get('tag')
        if tag is None:
            raise FixtureUploadError([
                _(FAILURE_MESSAGES['has_no_column']).format(
                    column_name='table_id')
            ])
        if is_identifier_invalid(tag):
            raise FixtureUploadError(
                [_(FAILURE_MESSAGES['invalid_table_id']).format(tag=tag)])

        field_names = row_dict.get('field')
        item_attributes = row_dict.get('property')

        if field_names is None and item_attributes is None:
            raise FixtureUploadError([
                _(FAILURE_MESSAGES['neither_fields_nor_attributes']).format(
                    tag=tag)
            ])

        field_names = [] if field_names is None else field_names
        item_attributes = [] if item_attributes is None else item_attributes

        def _get_field_properties(prop_key):
            properties = []
            if prop_key in row_dict and 'property' in row_dict[prop_key]:
                properties = row_dict[prop_key]["property"]
                if not isinstance(properties, list):
                    error_message = _(
                        FAILURE_MESSAGES["wrong_property_syntax"]).format(
                            prop_key=prop_key, )
                    raise FixtureUploadError([error_message])
            return properties

        def _get_field_is_indexed(prop_key):
            is_indexed = False
            if prop_key in row_dict and 'is_indexed' in row_dict[prop_key]:
                is_indexed = row_dict[prop_key]["is_indexed"]
                if not isinstance(is_indexed, bool):
                    error_message = _(
                        FAILURE_MESSAGES["wrong_index_syntax"]).format(
                            prop_key=prop_key, )
                    raise FixtureUploadError([error_message])
            return is_indexed

        def is_number(text):
            text = six.text_type(text)
            try:
                float(text)
                return True
            except ValueError:
                return False

        for i, field_name in enumerate(field_names):
            if is_number(field_name):
                message = _(
                    FAILURE_MESSAGES['invalid_field_name_numerical']).format(
                        i=i + 1,
                        val=field_name,
                    )
                raise FixtureUploadError([message])

        fields = [
            FixtureTypeField(
                field_name=field,
                properties=_get_field_properties(
                    'field {count}'.format(count=i + 1)),
                is_indexed=_get_field_is_indexed(
                    'field {count}'.format(count=i + 1)),
            ) for i, field in enumerate(field_names)
        ]

        return cls(
            table_id=tag,
            fields=fields,
            item_attributes=item_attributes,
            is_global=row_dict.get('is_global', False),
            uid=row_dict.get('UID'),
            delete=(row_dict.get(DELETE_HEADER) or '').lower() == 'y',
        )
Example #12
0
 def test_alphanumeric_unicode(self):
     good_name = "田纳西一二三"
     self.assertFalse(is_identifier_invalid(good_name))
Example #13
0
 def test_alphanumeric_nonascii(self):
     good_name = "província"
     self.assertFalse(is_identifier_invalid(good_name))
Example #14
0
 def test_backslash(self):
     bad_name = "space\\cadet"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #15
0
 def test_punctuation(self):
     bad_name = "ノイ丂 ム イ尺ムア! \_(ツ)_/¯"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #16
0
 def test_brackets(self):
     bad_name = "<space>"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #17
0
 def test_alphanumeric_nonascii(self):
     good_name = "província"
     self.assertFalse(is_identifier_invalid(good_name))
Example #18
0
 def test_good(self):
     good_name = "fooxmlbar"
     self.assertFalse(is_identifier_invalid(good_name))
Example #19
0
 def test_alphanumeric_unicode(self):
     good_name = "田纳西一二三"
     self.assertFalse(is_identifier_invalid(good_name))
 def test_brackets(self):
     bad_name = "<space>"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #21
0
def update_tables(request, domain, data_type_id, test_patch=None):
    """
    receives a JSON-update patch like following
    {
        "_id":"0920fe1c6d4c846e17ee33e2177b36d6",
        "tag":"growth",
        "view_link":"/a/gsid/fixtures/view_lookup_tables/?table_id:0920fe1c6d4c846e17ee33e2177b36d6",
        "is_global":false,
        "fields":{"genderr":{"update":"gender"},"grade":{}}
    }
    """
    if test_patch is None:
        test_patch = {}
    if data_type_id:
        try:
            data_type = FixtureDataType.get(data_type_id)
        except ResourceNotFound:
            raise Http404()

        assert(data_type.doc_type == FixtureDataType._doc_type)
        assert(data_type.domain == domain)

        if request.method == 'GET':
            return json_response(strip_json(data_type))

        elif request.method == 'DELETE':
            with CouchTransaction() as transaction:
                data_type.recursive_delete(transaction)
            return json_response({})
        elif not request.method == 'PUT':
            return HttpResponseBadRequest()

    if request.method == 'POST' or request.method == "PUT":
        fields_update = test_patch or _to_kwargs(request)
        fields_patches = fields_update["fields"]
        data_tag = fields_update["tag"]
        is_global = fields_update["is_global"]

        # validate tag and fields
        validation_errors = []
        if is_identifier_invalid(data_tag):
            validation_errors.append(data_tag)
        for field_name, options in fields_update['fields'].items():
            method = options.keys()
            if 'update' in method:
                field_name = options['update']
            if is_identifier_invalid(field_name) and 'remove' not in method:
                validation_errors.append(field_name)
        validation_errors = map(lambda e: _("\"%s\" cannot include special characters or "
                                            "begin with \"xml\" or a number.") % e, validation_errors)
        if validation_errors:
            return json_response({
                'validation_errors': validation_errors,
                'error_msg': _(
                    "Could not update table because field names were not "
                    "correctly formatted"),
            })

        with CouchTransaction() as transaction:
            if data_type_id:
                data_type = update_types(fields_patches, domain, data_type_id, data_tag, is_global, transaction)
                update_items(fields_patches, domain, data_type_id, transaction)
            else:
                if FixtureDataType.fixture_tag_exists(domain, data_tag):
                    return HttpResponseBadRequest("DuplicateFixture")
                else:
                    data_type = create_types(fields_patches, domain, data_tag, is_global, transaction)
        return json_response(strip_json(data_type))
 def test_starts_with_number(self):
     bad_name = "0hello"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #23
0
 def test_slash(self):
     bad_name = "will/crash"
     self.assertTrue(is_identifier_invalid(bad_name))
 def test_good(self):
     good_name = "fooxmlbar0123"
     self.assertFalse(is_identifier_invalid(good_name))
Example #25
0
 def test_space(self):
     bad_name = "space cadet"
     self.assertTrue(is_identifier_invalid(bad_name))
 def test_space(self):
     bad_name = "space cadet"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #27
0
 def test_xml(self):
     bad_name = "xml_and_more"
     self.assertTrue(is_identifier_invalid(bad_name))
 def test_backslash(self):
     bad_name = "space\\cadet"
     self.assertTrue(is_identifier_invalid(bad_name))
Example #29
0
 def test_unicode(self):
     bad_name = "ノイ丂 ム イ尺ムア! \_(ツ)_/¯"
     self.assertTrue(is_identifier_invalid(bad_name))