def update(build_id, values): """ .. function:: RPC Build.update(build_id, values) Updates the fields of the selected build. :param build_id: PK of Build to modify :type build_id: int :param values: Field values for :class:`tcms.management.models.Build` :type values: dict :return: Serialized :class:`tcms.management.models.Build` object :rtype: dict :raises: Build.DoesNotExist if build not found :raises: PermissionDenied if missing *management.change_build* permission """ selected_build = Build.objects.get(pk=build_id) # todo: this needs to start using model forms update_fields = list() if values.get("product"): _update_value(selected_build, "product", pre_check_product(values), update_fields) if values.get("name"): _update_value(selected_build, "name", values["name"], update_fields) if values.get("is_active") is not None: _update_value(selected_build, "is_active", values["is_active"], update_fields) selected_build.save(update_fields=update_fields) return selected_build.serialize()
def create(values): """ .. function:: XML-RPC Version.create(values) Add new version. :param values: Field values for :class:`tcms.management.models.Version` :type values: dict :return: Serialized :class:`tcms.management.models.Version` object :rtype: dict :raises ValueError: if input data validation fails :raises PermissionDenied: if missing *management.add_version* permission Example:: # Add version for specified product: >>> Version.create({'value': 'devel', 'product': 272}) {'product': 'QE Test Product', 'id': '1106', 'value': 'devel', 'product_id': 272} """ product = pre_check_product(values) form_values = values.copy() form_values['product'] = product.pk form = VersionForm(form_values) if form.is_valid(): version = form.save() return version.serialize() raise ValueError(form_errors_to_list(form))
def update(build_id, values): """ .. function:: XML-RPC Build.update(build_id, values) Updates the fields of the selected build. :param build_id: PK of Build to modify :type build_id: int :param values: Field values for :class:`tcms.management.models.Build` :type values: dict :return: Serialized :class:`tcms.management.models.Build` object :rtype: dict :raises: Build.DoesNotExist if build not found :raises: PermissionDenied if missing *management.change_build* permission """ selected_build = Build.objects.get(pk=build_id) def _update_value(obj, name, value): setattr(obj, name, value) update_fields.append(name) # todo: this needs to start using model forms update_fields = list() if values.get('product'): _update_value(selected_build, 'product', pre_check_product(values)) if values.get('name'): _update_value(selected_build, 'name', values['name']) if values.get('is_active') is not None: _update_value(selected_build, 'is_active', values['is_active']) selected_build.save(update_fields=update_fields) return selected_build.serialize()
def create(values): """ .. function:: XML-RPC Build.create(values) Create a new build object and store it in the database. :param values: Field values for :class:`tcms.management.models.Build` :type values: dict :return: Serialized :class:`tcms.management.models.Build` object :rtype: dict :raises ValueError: if product or name not specified :raises: PermissionDenied if missing *management.add_build* permission """ if not values.get('product') or not values.get('name'): raise ValueError('Product and name are both required.') return Build.objects.create(product=pre_check_product(values), name=values['name'], is_active=values.get('is_active', True)).serialize()
def create(values, **kwargs): """ .. function:: XML-RPC Component.create(values) Create new component. :param values: Field values for :class:`tcms.management.models.Component` :type values: dict :param kwargs: Dict providing access to the current request, protocol entry point name and handler instance from the rpc method :return: Serialized :class:`tcms.management.models.Component` object :rtype: dict :raises: PermissionDenied if missing *management.add_component* permission .. note:: If ``initial_owner_id`` or ``initial_qa_owner_id`` are not specified or don't exist in the database these fields are set to the user issuing the RPC request! """ initial_owner_id = values.get('initial_owner_id', None) initial_qa_contact_id = values.get('initial_qa_contact_id', None) product = pre_check_product(values) request = kwargs.get(REQUEST_KEY) if User.objects.filter(pk=initial_owner_id).exists(): _initial_owner_id = initial_owner_id else: _initial_owner_id = request.user.pk if User.objects.filter(pk=initial_qa_contact_id).exists(): _initial_qa_contact_id = initial_qa_contact_id else: _initial_qa_contact_id = request.user.pk return Component.objects.create( name=values['name'], product=product, initial_owner_id=_initial_owner_id, initial_qa_contact_id=_initial_qa_contact_id, ).serialize()
def test_pre_check_product_with_name(self): product = U.pre_check_product("World Of Warcraft") self.assertEqual(product.name, "World Of Warcraft")
def test_pre_check_product_with_number(self): product = U.pre_check_product(self.product.pk) self.assertEqual(product.name, "World Of Warcraft") self.assertRaises(ObjectDoesNotExist, U.pre_check_product, str(self.product.pk))
def test_pre_check_product_with_illegal_types(self): for arg in [(), [], self]: with self.assertRaisesRegex( ValueError, "The type of product is not recognizable."): U.pre_check_product(arg)
def test_pre_check_product_with_dict(self): product = U.pre_check_product({"product": self.product.pk}) self.assertEqual(product.name, "World Of Warcraft") product = U.pre_check_product({"product": "World Of Warcraft"}) self.assertEqual(product.name, "World Of Warcraft")