def test_parse_bool_value_with_rejected_args(self): rejected_args = (3, -1, "", "True", "False", "yes", "no", "33", "-11", [], (), {}, None) for arg in rejected_args: with self.assertRaisesRegex(ValueError, 'Unacceptable bool value.'): U.parse_bool_value(arg)
def filter(query=None, **kwargs): # pylint: disable=redefined-builtin """ .. function:: XML-RPC User.filter(query) Search and return the resulting list of users. :param query: Field lookups for :class:`django.contrib.auth.models.User` :type query: dict :return: Serialized :class:`django.contrib.auth.models.User` object without the password field! :rtype: dict .. note:: If query is ``None`` will return the user issuing the RPC request. """ if not query: query = {'pk': kwargs.get(REQUEST_KEY).user.pk} if 'is_active' in query: query['is_active'] = parse_bool_value(query['is_active']) users = User.objects.filter(**query) filtered_users = [] for user in users: filtered_users.append(_get_user_dict(user)) return filtered_users
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(build_id=build_id) def _update_value(obj, name, value): setattr(obj, name, value) update_fields.append(name) 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', parse_bool_value(values.get( 'is_active', True))) selected_build.save(update_fields=update_fields) return selected_build.serialize()
def test_parse_bool_value(self): self.assertFalse(U.parse_bool_value(0)) self.assertFalse(U.parse_bool_value('0')) self.assertFalse(U.parse_bool_value(False)) self.assertTrue(U.parse_bool_value(1)) self.assertTrue(U.parse_bool_value('1')) self.assertTrue(U.parse_bool_value(True))
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=parse_bool_value(values.get('is_active', True)) ).serialize()
def value_from_datadict(self, data, files, name): if name not in data: return False value = parse_bool_value(data.get(name)) return value