Ejemplo n.º 1
0
    def test_does_tag_with_name_exist(self, app_request_context):
        name = "nazwa"
        result = Tag.does_tag_with_name_exist(name)

        assert result is False

        mock_1 = self.create_mock_tag(name=name)
        mock_1.save()

        result = Tag.does_tag_with_name_exist(name)
        assert result is True
Ejemplo n.º 2
0
def layers_tag_post_delete():
    required_args = {"layer_id", "tag_id"}

    missing_args = required_args - set(request.args.keys())
    if len(missing_args) > 0:
        return jsonify({"error": f"missing arguments: {missing_args}"}), 400

    layer_id = request.args["layer_id"]
    tag_id = request.args["tag_id"]

    layer_tag = LayerTag.get_or_none(layer_id=layer_id, tag=tag_id)

    if Tag.get_or_none(id=tag_id) is None:
        return jsonify({"error":
                        f"tag with ID '{tag_id}' does not exist"}), 400

    if request.method == "POST":
        if layer_tag is not None:
            return jsonify({
                "error":
                f"layer '{layer_id}' already tagged with tag '{tag_id}'"
            }), 400

        layer_tag = LayerTag(layer_id=layer_id, tag=tag_id)
        layer_tag.save()
    else:
        if layer_tag is None:
            return jsonify({
                "error":
                f"layer '{layer_id}' is not tagged with tag '{tag_id}'"
            }), 400

        layer_tag.delete_instance()

    return jsonify({}), 204
Ejemplo n.º 3
0
    def test_does_tag_with_name_exist_except_self(self, app_request_context):
        name = "nazwa"

        mock_1 = self.create_mock_tag()
        mock_1.save()

        result = Tag.does_tag_with_name_exist(name, except_=mock_1)

        assert result is False
Ejemplo n.º 4
0
def tags_tag_id_delete(tag_id: int):
    tag = Tag.get_or_none(id=tag_id)

    if tag is None:
        return jsonify({"error":
                        f"tag with ID '{tag_id}' does not exist"}), 404

    if request.method == "PUT":
        payload = request.get_json(force=True)
        name = payload["name"]

        if Tag.select().where(Tag.name == name, Tag.id != tag_id).exists():
            return jsonify({"error":
                            f"tag with name '{name}' already exists"}), 400
        Tag.update(**payload).where(Tag.id == tag_id).execute()

    if request.method == "DELETE":
        tag.delete_instance(recursive=True)

    return jsonify({}), 204
Ejemplo n.º 5
0
def tags_post():
    payload = request.get_json(force=True)
    required_attributes = ("name", "color")

    if set(payload.keys()) != set(required_attributes):
        return jsonify({
            "error":
            "invalid attributes - {} are required".format(required_attributes)
        }), 400

    name = payload["name"]

    if Tag.does_tag_with_name_exist(name):
        return jsonify({"error":
                        f"tag with name '{name}' already exists"}), 400

    tag = Tag(name=name, color=payload["color"])
    tag.save()

    return jsonify({"data": tag.id}), 201
Ejemplo n.º 6
0
def tags_get():
    query = Tag.select().order_by(Tag.id)
    result = list(query.dicts())

    return jsonify({"data": result})
Ejemplo n.º 7
0
 def create_mock_tag(self, name="nazwa_taga", color="czerowny") -> Tag:
     return Tag(name=name, color=color)