Example #1
0
 def test_good(self):
     good_name = "foobar"
     self.assertFalse(is_field_name_invalid(good_name))
Example #2
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)

        # validate fields
        validation_errors = []
        for field_name, options in fields_update['fields'].items():
            method = options.keys()
            if 'update' in method:
                field_name = options['update']
            if field_name.startswith('xml') and 'remove' not in method:
                validation_errors.append(
                    _("Field name \"%s\" cannot begin with 'xml'.") % field_name
                )
            if is_field_name_invalid(field_name) and 'remove' not in method:
                validation_errors.append(
                    _("Field name \"%s\" cannot include /, "
                      "\\, <, >, or spaces.") % field_name
                )
        if validation_errors:
            return json_response({
                'validation_errors': validation_errors,
                'error_msg': _(
                    "Could not update table because field names were not "
                    "correctly formatted"),
            })

        fields_patches = fields_update["fields"]
        data_tag = fields_update["tag"]
        is_global = fields_update["is_global"]
        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))
Example #3
0
 def test_brackets(self):
     bad_name = "<space>"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #4
0
 def test_combo(self):
     bad_name = "<space>\<dadgg sd"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #5
0
 def test_space(self):
     bad_name = "space cadet"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #6
0
 def test_backslash(self):
     bad_name = "space\\cadet"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #7
0
 def test_good(self):
     good_name = "foobar"
     self.assertFalse(is_field_name_invalid(good_name))
Example #8
0
 def test_slash(self):
     bad_name = "will/crash"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #9
0
 def test_combo(self):
     bad_name = "<space>\<dadgg sd"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #10
0
 def test_brackets(self):
     bad_name = "<space>"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #11
0
 def test_backslash(self):
     bad_name = "space\\cadet"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #12
0
 def test_space(self):
     bad_name = "space cadet"
     self.assertTrue(is_field_name_invalid(bad_name))
Example #13
0
 def test_slash(self):
     bad_name = "will/crash"
     self.assertTrue(is_field_name_invalid(bad_name))