Beispiel #1
0
def item_view(modelcls, ident_prop, primary_key, schema, is_org, **kargs):
    """Generic view function to handle operations on single resource items."""

    ident = {prop: kargs.get(prop) for prop in ident_prop}

    if is_org:
        ident["org_id"] = current_org.id

    try:
        model = modelcls.query.filter_by(**ident).one()
    except NoResultFound:
        abort(404)

    if request.method == "GET":
        return model

    if request.method == "DELETE":
        database.session.delete(model)
        database.session.commit()
        return model

    if request.method == "PATCH":
        payload = request.get_json()
        v = Validator(schema, modelcls)

        if v.validate(payload, update=True, model=model) is False:
            raise ValidationError(v.errors)

        data = v.normalized(payload)
        _import_data(model, data)
        database.session.commit()

        return model
Beispiel #2
0
    def _parse_query_string(self, cls, qs):
        qs = qs.to_dict(flat=True)
        schema = generate_schema(cls)
        mapper = inspect(cls)
        columns = [column.name for column in mapper.c]
        query_string_schema = {
            "select": {
                "type": "dict",
                "allowed": columns,
                "valueschema": {
                    "type": "integer"
                },
            },
            "search": {
                "type": "dict",
                "schema": {
                    "t": {
                        "required": True
                    },
                    "f": {
                        "required": True,
                        "type": "list",
                        "allowed": columns
                    },
                },
            },
            "filter": {
                "type": "dict",
                "schema": schema
            },
            "sort": {},
            "limit": {
                "type": "integer",
                "coerce": int
            },
            "page": {
                "type": "integer",
                "coerce": int
            },
        }

        # First decode all modifiers with JSON string
        json_keys = {"select", "search", "filter"}
        decoded_qs = {}

        for key, value in qs.items():
            if key in json_keys:
                try:
                    decoded_qs[key] = json_loads(value)
                except JSONDecodeError:
                    raise ValidationError({key: "Invalid JSON string"})
            else:
                decoded_qs[key] = value

        v = Validator(query_string_schema)

        if v.validate(decoded_qs, update=True) is False:
            raise ValidationError(v.errors)

        return v.normalized(decoded_qs)
Beispiel #3
0
    def test_unique_rule_update_with_model_argument(self, ctx):
        model = Product.query.filter_by(name="Acme anvils").one()

        v = Validator({"name": {"unique": True}}, Product)

        kargs = {"update": True, "model": model}
        assert v.validate({"name": "Acme anvils"}, **kargs) is True
        assert v.validate({"name": "new product name"}, **kargs) is True
        assert v.validate({"name": "Binocular"}, **kargs) is False

        error_message = "Must be unique, but 'Binocular' already exist"
        assert error_message in v.errors["name"]
Beispiel #4
0
def add_view_func(modelcls, ident_prop, primary_key, schema, is_org, **kargs):
    payload = request.get_json()

    if is_org:
        payload["org_id"] = current_org.id

    v = Validator(schema, modelcls)

    if v.validate(payload) is False:
        raise ValidationError(v.errors)

    model = modelcls()
    data = v.normalized(payload)
    _import_data(model, data)

    database.session.add(model)
    database.session.commit()

    return model, 201
Beispiel #5
0
    def test_unique_rule_update_without_model_argument(self, ctx):
        v = Validator({"name": {"unique": True}}, Product)

        assert v.validate({"name": "Update name"}, update=True) is True
        assert v.validate({"name": "Acme anvils"}, update=True) is False
Beispiel #6
0
 def test_unique_rule_when_name_already_exist(self, ctx):
     v = Validator({"name": {"unique": True}}, Product)
     assert v.validate({"name": "Acme anvils"}) is False
     assert "Must be unique, but 'Acme anvils' already exist" in v.errors[
         "name"]
Beispiel #7
0
 def test_unique_rule(self, ctx):
     v = Validator({"name": {"unique": True}}, Product)
     assert v.validate({"name": "New product name"}) is True
Beispiel #8
0
    def test_unique_rule_missing_model_class(self, ctx):
        v = Validator({"name": {"unique": True}})

        with pytest.raises(RuntimeError):
            v.validate({"name": "Update name"})
Beispiel #9
0
 def test_constructor(self):
     v = Validator({}, Order)
     assert v.model_class is Order