Source code for tcms.rpc.api.product

# -*- coding: utf-8 -*-

from django.forms.models import model_to_dict
from modernrpc.core import rpc_method

from import Product
from import ProductForm
from tcms.rpc.decorators import permissions_required

[docs] @rpc_method(name="Product.create") @permissions_required("management.add_product") def create(values): """ .. function:: RPC Product.create(values) Create a new Product object and store it in the database. :param values: Field values for :class:`` :type values: dict :return: Serialized :class:`` object :rtype: dict :raises ValueError: if input values don't validate :raises PermissionDenied: if missing *management.add_product* permission """ form = ProductForm(values) if form.is_valid(): product = return model_to_dict(product) raise ValueError(list(form.errors.items()))
[docs] @permissions_required("management.view_product") @rpc_method(name="Product.filter") def filter(query=None): # pylint: disable=redefined-builtin """ .. function:: RPC Product.filter(query) Perform a search and return the resulting list of products. :param query: Field lookups for :class:`` :type query: dict :return: Serialized list of :class:`` objects :rtype: dict Example:: # Get all of products named 'Red Hat Enterprise Linux 5' >>> Product.filter({'name': 'Red Hat Enterprise Linux 5'}) """ if query is None: query = {} return list( Product.objects.filter(**query) .values( "id", "name", "description", "classification", ) .distinct() .order_by("pk") )