def test_to_xmlrpc(self): result = TestPlan.to_xmlrpc(query={'pk__in': self.plan_pks}) self.assertEqual(len(result), 2) # Verify fields sample_testplan = result[0] sample_fields = set(name for name in sample_testplan.keys()) test_fields = set(self.test_fields) test_result = list(sample_fields ^ test_fields) self.assertEqual(test_result, []) result = dict([(item['plan_id'], item) for item in result]) plan = result[self.plan_pks[0]] sample_plan = TestPlan.objects.get(pk=self.plan_pks[0]) self.assertEqual(plan['name'], sample_plan.name) self.assertEqual(plan['is_active'], sample_plan.is_active) self.assertEqual(plan['product_version_id'], sample_plan.product_version_id) components = plan['component'] components.sort() sample_components = [item.pk for item in sample_plan.component.all()] sample_components.sort() self.assertEqual(components, sample_components) plan = result[self.plan_pks[1]] sample_plan = TestPlan.objects.get(pk=self.plan_pks[1]) self.assertEqual(plan['name'], sample_plan.name) self.assertEqual(plan['is_active'], sample_plan.is_active) self.assertEqual(plan['product_version_id'], sample_plan.product_version_id)
def filter(request, values={}): """Performs a search and returns the resulting list of test plans. :param dict values: a mapping containing these criteira. * author: ForeignKey: Auth.User * attachment: ForeignKey: Attachment * case: ForeignKey: TestCase * create_date: DateTime * env_group: ForeignKey: Environment Group * name: (str) * plan_id: (int) * product: ForeignKey: Product * product_version: ForeignKey: Version * tag: ForeignKey: TestTag * text: ForeignKey: Test Plan Text * type: ForeignKey: Test Plan Type :return: list of mappings of found :class:`TestPlan`. :rtype: list[dict] Example:: # Get all of plans contain 'TCMS' in name >>> TestPlan.filter({'name__icontain': 'TCMS'}) # Get all of plans create by xkuang >>> TestPlan.filter({'author__username': '******'}) # Get all of plans the author name starts with x >>> TestPlan.filter({'author__username__startswith': 'x'}) # Get plans contain the case ID 1, 2, 3 >>> TestPlan.filter({'case__case_id__in': [1, 2, 3]}) """ return TestPlan.to_xmlrpc(values)
def filter(request, values={}): """Performs a search and returns the resulting list of test plans. :param dict values: a mapping containing these criteira. * author: ForeignKey: Auth.User * attachment: ForeignKey: Attachment * case: ForeignKey: TestCase * create_date: DateTime * env_group: ForeignKey: Environment Group * name: (str) * plan_id: (int) * product: ForeignKey: Product * product_version: ForeignKey: Version * tag: ForeignKey: TestTag * text: ForeignKey: Test Plan Text * type: ForeignKey: Test Plan Type :return: list of mappings of found :class:`TestPlan`. :rtype: list[dict] Example:: # Get all of plans contain 'TCMS' in name TestPlan.filter({'name__icontain': 'TCMS'}) # Get all of plans create by xkuang TestPlan.filter({'author__username': '******'}) # Get all of plans the author name starts with x TestPlan.filter({'author__username__startswith': 'x'}) # Get plans contain the case ID 1, 2, 3 TestPlan.filter({'case__case_id__in': [1, 2, 3]}) """ return TestPlan.to_xmlrpc(values)
def test_to_xmlrpc(self): result = TestPlan.to_xmlrpc(query={'pk__in': self.plan_pks}) self.assertEqual(len(result), 2) # Verify fields sample_testplan = result[0] sample_fields = set(name for name in sample_testplan.keys()) test_fields = set(self.test_fields) test_result = list(sample_fields ^ test_fields) self.assertEqual(test_result, []) result = dict([(item['plan_id'], item) for item in result]) plan = result[self.plan_pks[0]] sample_plan = TestPlan.objects.get(pk=self.plan_pks[0]) self.assertEqual(plan['name'], sample_plan.name) self.assertEqual(plan['is_active'], sample_plan.is_active) self.assertEqual(plan['product_version_id'], sample_plan.product_version_id) components = plan['component'] components.sort() sample_components = [item.pk for item in sample_plan.component.all()] sample_components.sort() self.assertEqual(components, sample_components) plan = result[self.plan_pks[1]] sample_plan = TestPlan.objects.get(pk=self.plan_pks[1]) self.assertEqual(plan['name'], sample_plan.name) self.assertEqual(plan['is_active'], sample_plan.is_active) self.assertEqual(plan['product_version_id'], sample_plan.product_version_id)
def filter(query=None): # pylint: disable=redefined-builtin """ .. function:: XML-RPC TestPlan.filter(query) Perform a search and return the resulting list of test plans. :param query: Field lookups for :class:`tcms.testplans.models.TestPlan` :type query: dict :return: List of serialized :class:`tcms.testplans.models.TestPlan` objects :rtype: list(dict) """ if query is None: query = {} return TestPlan.to_xmlrpc(query)
def get_plans(case_id): """ Description: Get the list of plans that this case is linked to. Params: $case_id - Integer/String: An integer representing the ID in the database Returns: Array: An array of test plan object hashes. Example: >>> TestCase.get_plans(12345) """ test_case = TestCase.objects.get(case_id=case_id) plan_ids = test_case.plan.values_list('plan_id', flat=True) query = {'plan_id__in': plan_ids} return TestPlan.to_xmlrpc(query)
def get_plans(request, case_id): """Get the list of plans that this case is linked to. :param case_id: case ID. :type case_id: int or str :return: a list of mappings of :class:`TestPlan`. :rtype: list[dict] Example:: >>> TestCase.get_plans(1) """ tc = TestCase.objects.get(case_id=case_id) plan_ids = tc.plan.values_list('plan_id', flat=True) query = {'plan_id__in': plan_ids} return TestPlan.to_xmlrpc(query)
def unlink_plan(case_id, plan_id): """ Description: Unlink a test case from the given plan. If only one plan is linked, this will delete the test case. Params: $case_id - Integer/String: An integer or alias representing the ID in the database. $plan_id - Integer: An integer representing the ID in the database. Returns: Array: Array of plans hash still linked if any, empty if not. Example: >>> TestCase.unlink_plan(12345, 137) """ TestCasePlan.objects.filter(case=case_id, plan=plan_id).delete() plan_pks = TestCasePlan.objects.filter(case=case_id).values_list('plan', flat=True) return TestPlan.to_xmlrpc(query={'pk__in': plan_pks})
def unlink_plan(requst, case_id, plan_id): """ Description: Unlink a test case from the given plan. If only one plan is linked, this will delete the test case. Params: $case_id - Integer/String: An integer or alias representing the ID in the database. $plan_id - Integer: An integer representing the ID in the database. Returns: Array: Array of plans hash still linked if any, empty if not. Example: >>> TestCase.unlink_plan(12345, 137) """ TestCasePlan.objects.filter(case=case_id, plan=plan_id).delete() plan_pks = TestCasePlan.objects.filter(case=case_id).values_list('plan', flat=True) return TestPlan.to_xmlrpc(query={'pk__in': plan_pks})
def get_plans(request, case_id): """Get the list of plans that this case is linked to. :param case_id: case ID. :type case_id: int or str :return: a list of mappings of :class:`TestPlan`. :rtype: list[dict] Example:: TestCase.get_plans(1) """ tc = TestCase.objects.get(case_id=case_id) plan_ids = tc.plan.values_list('plan_id', flat=True) query = {'plan_id__in': plan_ids} return TestPlan.to_xmlrpc(query)
def get_plans(request, case_id): """ Description: Get the list of plans that this case is linked to. Params: $case_id - Integer/String: An integer representing the ID in the database Returns: Array: An array of test plan object hashes. Example: >>> TestCase.get_plans(12345) """ try: tc = TestCase.objects.get(case_id=case_id) except: raise plan_ids = tc.plan.values_list('plan_id', flat=True) query = {'plan_id__in': plan_ids} return TestPlan.to_xmlrpc(query)
def get_plans(request, product): """Get the list of plans associated with this product. :param product: product ID or name. :type product: int or str :return: a list of mappings of :class:`TestPlan`. :rtype: list Example:: # Get with product id >>> Product.get_plans(61) # Get with product name >>> Product.get_plans('product name') """ from tcms.testplans.models import TestPlan p = pre_check_product(values=product) query = {'product': p} return TestPlan.to_xmlrpc(query)
def get_plans(request, product): """Get the list of plans associated with this product. :param product: product ID or name. :type product: int or str :return: a list of mappings of :class:`TestPlan`. :rtype: list Example:: # Get with product id >>> Product.get_plans(61) # Get with product name >>> Product.get_plans('product name') """ from tcms.testplans.models import TestPlan p = pre_check_product(values=product) query = {'product': p} return TestPlan.to_xmlrpc(query)
def get_plans(request, product): """ Description: Get the list of plans associated with this product. Params: $product - Integer/String Integer: product_id of the product in the Database String: Product name Returns: Array: Returns an array of Test Plan objects. Example: # Get with product id >>> Product.get_plans(61) # Get with product name >>> Product.get_plans('Red Hat Enterprise Linux 5') """ from tcms.testplans.models import TestPlan p = pre_check_product(values=product) query = {'product': p} return TestPlan.to_xmlrpc(query)
def get_plans(request, product): """ Description: Get the list of plans associated with this product. Params: $product - Integer/String Integer: product_id of the product in the Database String: Product name Returns: Array: Returns an array of Test Plan objects. Example: # Get with product id >>> Product.get_plans(61) # Get with product name >>> Product.get_plans('Red Hat Enterprise Linux 5') """ from tcms.testplans.models import TestPlan p = pre_check_product(values=product) query = {'product': p} return TestPlan.to_xmlrpc(query)
def unlink_plan(requst, case_id, plan_id): """ Description: Unlink a test case from the given plan. If only one plan is linked, this will delete the test case. Params: $case_ids - Integer/String: An integer or alias representing the ID in the database. $plan_id - Integer: An integer representing the ID in the database. Returns: Array: Array of plans hash still linked if any, empty if not. Example: >>> TestCase.unlink_plan(12345, 137) """ sql = 'DELETE FROM test_case_plans WHERE plan_id = %s and case_id = %s' cursor = connection.writer_cursor cursor.execute(sql, [int(plan_id), int(case_id)]) transaction.commit_unless_managed() plan_pks = TestCasePlan.objects.filter(case=case_id).values_list('plan', flat=True) return TestPlan.to_xmlrpc(query={'pk__in': plan_pks})
def unlink_plan(requst, case_id, plan_id): """ Unlink a test case from the given plan. If only one plan is linked, this will delete the test case. :param case_id: case ID. :type case_id: int or str :param int plan_id: plan ID from where to unlink the specified case. :return: a list of mappings of test plans that are still linked to the specified case. If there is no linked test plans, empty list will be returned. :rtype: list[dict] Example:: # Unlink case 100 from plan 10 >>> TestCase.unlink_plan(100, 10) """ TestCasePlan.objects.filter(case=case_id, plan=plan_id).delete() plan_pks = TestCasePlan.objects.filter(case=case_id).values_list('plan', flat=True) return TestPlan.to_xmlrpc(query={'pk__in': plan_pks})
def unlink_plan(requst, case_id, plan_id): """ Unlink a test case from the given plan. If only one plan is linked, this will delete the test case. :param case_id: case ID. :type case_id: int or str :param int plan_id: plan ID from where to unlink the specified case. :return: a list of mappings of test plans that are still linked to the specified case. If there is no linked test plans, empty list will be returned. :rtype: list[dict] Example:: # Unlink case 100 from plan 10 TestCase.unlink_plan(100, 10) """ TestCasePlan.objects.filter(case=case_id, plan=plan_id).delete() plan_pks = TestCasePlan.objects.filter(case=case_id).values_list('plan', flat=True) return TestPlan.to_xmlrpc(query={'pk__in': plan_pks})
def filter(request, values={}): """ Description: Performs a search and returns the resulting list of test plans. Params: $values - Hash: keys must match valid search fields. +------------------------------------------------------------+ | Plan Search Parameters | +----------------------------------------------------------+ | Key | Valid Values | | author | ForeignKey: Auth.User | | attachment | ForeignKey: Attachment | | case | ForeignKey: Test Case | | create_date | DateTime | | env_group | ForeignKey: Environment Group | | name | String | | plan_id | Integer | | product | ForeignKey: Product | | product_version | ForeignKey: Version | | tag | ForeignKey: Tag | | text | ForeignKey: Test Plan Text | | type | ForeignKey: Test Plan Type | +------------------------------------------------------------+ Returns: Array: Matching test plans are retuned in a list of plan object hashes. Example: # Get all of plans contain 'TCMS' in name >>> TestPlan.filter({'name__icontain': 'TCMS'}) # Get all of plans create by xkuang >>> TestPlan.filter({'author__username': '******'}) # Get all of plans the author name starts with x >>> TestPlan.filter({'author__username__startswith': 'x'}) # Get plans contain the case ID 12345, 23456, 34567 >>> TestPlan.filter({'case__case_id__in': [12345, 23456, 34567]}) """ return TestPlan.to_xmlrpc(values)
def filter(request, values={}): """ Description: Performs a search and returns the resulting list of test plans. Params: $values - Hash: keys must match valid search fields. +------------------------------------------------------------+ | Plan Search Parameters | +----------------------------------------------------------+ | Key | Valid Values | | author | ForeignKey: Auth.User | | attachment | ForeignKey: Attachment | | case | ForeignKey: Test Case | | create_date | DateTime | | env_group | ForeignKey: Environment Group | | name | String | | plan_id | Integer | | product | ForeignKey: Product | | product_version | ForeignKey: Version | | tag | ForeignKey: Tag | | text | ForeignKey: Test Plan Text | | type | ForeignKey: Test Plan Type | +------------------------------------------------------------+ Returns: Array: Matching test plans are retuned in a list of plan object hashes. Example: # Get all of plans contain 'TCMS' in name >>> TestPlan.filter({'name__icontain': 'TCMS'}) # Get all of plans create by xkuang >>> TestPlan.filter({'author__username': '******'}) # Get all of plans the author name starts with x >>> TestPlan.filter({'author__username__startswith': 'x'}) # Get plans contain the case ID 12345, 23456, 34567 >>> TestPlan.filter({'case__case_id__in': [12345, 23456, 34567]}) """ return TestPlan.to_xmlrpc(values)
def update(plan_ids, values): """ Description: Updates the fields of the selected test plan. Params: $plan_ids - Integer: A single (or list of) TestPlan ID. $values - Hash of keys matching TestPlan fields and the new values to set each field to. +---------------------------+----------------+--------------------------------------------+ | Field | Type | Description | +---------------------------+----------------+--------------------------------------------+ | product | Integer | ID of product | | name | String | | | type | Integer | ID of plan type | | product_version | Integer | ID of version, product_version(recommend), | | (default_product_version)| | default_product_version will be deprecated | | | | in future release. | | owner | String/Integer | user_name/user_id | | parent | Integer | Parent plan ID | | is_active | Boolean | True/False | | env_group | Integer | New environment group ID | +---------------------------+-------------------------------------------------------------+ Returns: Hash: The updated test plan object. Example: # Update product to 61 for plan 207 and 208 >>> TestPlan.update([207, 208], {'product': 61}) """ from tcms.xmlrpc.forms import EditPlanForm if values.get('default_product_version'): values['product_version'] = values.pop('default_product_version') form = EditPlanForm(values) if values.get('product_version') and not values.get('product'): raise ValueError('Field "product" is required by product_version') if values.get('product') and not values.get('product_version'): raise ValueError('Field "product_version" is required by product') if values.get('product_version') and values.get('product'): form.populate(product_id=values['product']) plan_ids = pre_process_ids(value=plan_ids) tps = TestPlan.objects.filter(pk__in=plan_ids) if form.is_valid(): _values = dict() if form.cleaned_data['name']: _values['name'] = form.cleaned_data['name'] if form.cleaned_data['type']: _values['type'] = form.cleaned_data['type'] if form.cleaned_data['product']: _values['product'] = form.cleaned_data['product'] if form.cleaned_data['product_version']: _values['product_version'] = form.cleaned_data[ 'product_version'] if form.cleaned_data['owner']: _values['owner'] = form.cleaned_data['owner'] if form.cleaned_data['parent']: _values['parent'] = form.cleaned_data['parent'] if not (values.get('is_active') is None): _values['is_active'] = form.cleaned_data['is_active'] tps.update(**_values) # requested to update environment group for selected test plans if form.cleaned_data['env_group']: # prepare the list of new objects to be inserted into DB new_objects = [ TCMSEnvPlanMap( plan_id=plan_pk, group_id=form.cleaned_data['env_group'].pk ) for plan_pk in plan_ids ] # first delete the old values (b/c many-to-many I presume ?) TCMSEnvPlanMap.objects.filter(plan__in=plan_ids).delete() # then create all objects with 1 INSERT TCMSEnvPlanMap.objects.bulk_create(new_objects) else: raise ValueError(form_errors_to_list(form)) query = {'pk__in': tps.values_list('pk', flat=True)} return TestPlan.to_xmlrpc(query)
def update(request, plan_ids, values): """ Description: Updates the fields of the selected test plan. Params: $plan_ids - Integer: A single (or list of) TestPlan ID. $values - Hash of keys matching TestPlan fields and the new values to set each field to. +-------------------------------------------+----------------+--------------------------------------------+ | Field | Type | Description | +-------------------------------------------+----------------+--------------------------------------------+ | product | Integer | ID of product | | name | String | | | type | Integer | ID of plan type | | product_version(default_product_version) | Integer | ID of version, product_version(recommend), | | | | default_product_version will be deprecated | | | | in future release. | | owner | String/Integer | user_name/user_id | | parent | Integer | Parent plan ID | | is_active | Boolean | True/False | | env_group | Integer | New environment group ID | +-------------------------+----------------+--------------------------------------------------------------+ Returns: Hash: The updated test plan object. Example: # Update product to 61 for plan 207 and 208 >>> TestPlan.update([207, 208], {'product': 61}) """ from tcms.core import forms from tcms.xmlrpc.forms import EditPlanForm if values.get('default_product_version'): values['product_version'] = values.pop('default_product_version') form = EditPlanForm(values) if values.get('product_version') and not values.get('product'): raise ValueError('Field "product" is required by product_version') if values.get('product') and not values.get('product_version'): raise ValueError('Field "product_version" is required by product') if values.get('product_version') and values.get('product'): form.populate(product_id=values['product']) plan_ids = pre_process_ids(value=plan_ids) tps = TestPlan.objects.filter(pk__in=plan_ids) if form.is_valid(): _values = dict() if form.cleaned_data['name']: _values['name'] = form.cleaned_data['name'] if form.cleaned_data['type']: _values['type'] = form.cleaned_data['type'] if form.cleaned_data['product']: _values['product'] = form.cleaned_data['product'] if form.cleaned_data['product_version']: _values['product_version'] = form.cleaned_data[ 'product_version'] if form.cleaned_data['owner']: _values['owner'] = form.cleaned_data['owner'] if form.cleaned_data['parent']: _values['parent'] = form.cleaned_data['parent'] if not (values.get('is_active') is None): _values['is_active'] = form.cleaned_data['is_active'] tps.update(**_values) # requested to update environment group for selected test plans if form.cleaned_data['env_group']: # prepare the list of new objects to be inserted into DB new_objects = [ TCMSEnvPlanMap( plan_id=plan_pk, group_id=form.cleaned_data['env_group'].pk ) for plan_pk in plan_ids ] # first delete the old values (b/c many-to-many I presume ?) TCMSEnvPlanMap.objects.filter(plan__in=plan_ids).delete() # then create all objects with 1 INSERT TCMSEnvPlanMap.objects.bulk_create(new_objects) else: raise ValueError(forms.errors_to_list(form)) query = {'pk__in': tps.values_list('pk', flat=True)} return TestPlan.to_xmlrpc(query)
def update(request, plan_ids, values): """ Description: Updates the fields of the selected test plan. Params: $plan_ids - Integer: A single TestPlan ID. $values - Hash of keys matching TestPlan fields and the new values to set each field to. +-------------------------------------------+----------------+--------------------------------------------+ | Field | Type | Description | +-------------------------------------------+----------------+--------------------------------------------+ | product | Integer | ID of product | | name | String | | | type | Integer | ID of plan type | | product_version(default_product_version) | Integer | ID of version, product_version(recommend), | | | | default_product_version will be deprecated | | | | in future release. | | owner | String/Integer | user_name/user_id | | parent | Integer | Parent plan ID | | is_active | Boolean | True/False | | env_group | Integer | | +-------------------------+----------------+--------------------------------------------------------------+ Returns: Hash: The updated test plan object. Example: # Update product to 61 for plan 207 and 208 >>> TestPlan.update([207, 208], {'product': 61}) """ from tcms.core import forms from tcms.xmlrpc.forms import EditPlanForm if values.get('default_product_version'): values['product_version'] = values.pop('default_product_version') form = EditPlanForm(values) if values.get('product_version') and not values.get('product'): raise ValueError('Field "product" is required by product_version') if values.get('product') and not values.get('product_version'): raise ValueError('Field "product_version" is required by product') if values.get('product_version') and values.get('product'): form.populate(product_id=values['product']) plan_ids = pre_process_ids(value=plan_ids) tps = TestPlan.objects.filter(pk__in=plan_ids) if form.is_valid(): _values = dict() if form.cleaned_data['name']: _values['name'] = form.cleaned_data['name'] if form.cleaned_data['type']: _values['type'] = form.cleaned_data['type'] if form.cleaned_data['product']: _values['product'] = form.cleaned_data['product'] if form.cleaned_data['product_version']: _values['product_version'] = form.cleaned_data['product_version'] if form.cleaned_data['owner']: _values['owner'] = form.cleaned_data['owner'] if form.cleaned_data['parent']: _values['parent'] = form.cleaned_data['parent'] if not (values.get('is_active') is None): _values['is_active'] = form.cleaned_data['is_active'] tps.update(**_values) if form.cleaned_data['env_group']: # NOTE: MyISAM does not support transaction, so no need to use # transaction.commit_on_success to control the commit. cursor = connection.writer_cursor in_condition = ','.join(itertools.repeat('%s', len(plan_ids))) del_env_group_sql = TP_CLEAR_ENV_GROUP % in_condition cursor.execute(del_env_group_sql, plan_ids) insert_values = ','.join( itertools.repeat('(%s, %s)', len(plan_ids))) insert_env_group_sql = TP_ADD_ENV_GROUP % insert_values args = list() for arg in itertools.izip( plan_ids, itertools.repeat(form.cleaned_data['env_group'].pk, len(plan_ids))): args.extend(arg) cursor.execute(insert_env_group_sql, args) transaction.commit_unless_managed() else: raise ValueError(forms.errors_to_list(form)) query = {'pk__in': tps.values_list('pk', flat=True)} return TestPlan.to_xmlrpc(query)
def update(request, plan_ids, values): """Updates the fields of the selected test plan. :param plan_ids: give one or more plan IDs. It could be an integer, a string containing comma separated IDs, or a list of int each of them is a plan ID. :type plan_ids: int, str or list :param dict values: a mapping containing these plan data to update * product: (int) ID of product * name: (str) * type: (int) ID of plan type * product_version: (int) ID of version * default_product_version: (int) alternative version ID. * owner: (str)/(int) user_name/user_id * parent: (int) Parent plan ID * is_active: bool True/False * env_group: (int) New environment group ID :return: a mapping of updated :class:`TestPlan`. :rtype: dict Example:: # Update product to 7 for plan 1 and 2 TestPlan.update([1, 2], {'product': 7}) .. deprecated:: x.y ``default_product_version`` is deprecated and will be removed. """ from tcms.core import forms from tcms.xmlrpc.forms import EditPlanForm if values.get('default_product_version'): values['product_version'] = values.pop('default_product_version') form = EditPlanForm(values) if values.get('product_version') and not values.get('product'): raise ValueError('Field "product" is required by product_version') if values.get('product') and not values.get('product_version'): raise ValueError('Field "product_version" is required by product') if values.get('product_version') and values.get('product'): form.populate(product_id=values['product']) plan_ids = pre_process_ids(value=plan_ids) tps = TestPlan.objects.filter(pk__in=plan_ids) if form.is_valid(): _values = dict() if form.cleaned_data['name']: _values['name'] = form.cleaned_data['name'] if form.cleaned_data['type']: _values['type'] = form.cleaned_data['type'] if form.cleaned_data['product']: _values['product'] = form.cleaned_data['product'] if form.cleaned_data['product_version']: _values['product_version'] = form.cleaned_data['product_version'] if form.cleaned_data['owner']: _values['owner'] = form.cleaned_data['owner'] if form.cleaned_data['parent']: _values['parent'] = form.cleaned_data['parent'] if not (values.get('is_active') is None): _values['is_active'] = form.cleaned_data['is_active'] tps.update(**_values) # requested to update environment group for selected test plans if form.cleaned_data['env_group']: # prepare the list of new objects to be inserted into DB new_objects = [ TCMSEnvPlanMap(plan_id=plan_pk, group_id=form.cleaned_data['env_group'].pk) for plan_pk in plan_ids ] # first delete the old values (b/c many-to-many I presume ?) TCMSEnvPlanMap.objects.filter(plan__in=plan_ids).delete() # then create all objects with 1 INSERT TCMSEnvPlanMap.objects.bulk_create(new_objects) else: raise ValueError(forms.errors_to_list(form)) query = {'pk__in': tps.values_list('pk', flat=True)} return TestPlan.to_xmlrpc(query)
def update(request, plan_ids, values): """ Description: Updates the fields of the selected test plan. Params: $plan_ids - Integer: A single TestPlan ID. $values - Hash of keys matching TestPlan fields and the new values to set each field to. +-------------------------------------------+----------------+--------------------------------------------+ | Field | Type | Description | +-------------------------------------------+----------------+--------------------------------------------+ | product | Integer | ID of product | | name | String | | | type | Integer | ID of plan type | | product_version(default_product_version) | Integer | ID of version, product_version(recommend), | | | | default_product_version will be deprecated | | | | in future release. | | owner | String/Integer | user_name/user_id | | parent | Integer | Parent plan ID | | is_active | Boolean | True/False | | env_group | Integer | | +-------------------------+----------------+--------------------------------------------------------------+ Returns: Hash: The updated test plan object. Example: # Update product to 61 for plan 207 and 208 >>> TestPlan.update([207, 208], {'product': 61}) """ from tcms.core import forms from tcms.xmlrpc.forms import EditPlanForm if values.get('default_product_version'): values['product_version'] = values.pop('default_product_version') form = EditPlanForm(values) if values.get('product_version') and not values.get('product'): raise ValueError('Field "product" is required by product_version') if values.get('product') and not values.get('product_version'): raise ValueError('Field "product_version" is required by product') if values.get('product_version') and values.get('product'): form.populate(product_id=values['product']) plan_ids = pre_process_ids(value=plan_ids) tps = TestPlan.objects.filter(pk__in=plan_ids) if form.is_valid(): _values = dict() if form.cleaned_data['name']: _values['name'] = form.cleaned_data['name'] if form.cleaned_data['type']: _values['type'] = form.cleaned_data['type'] if form.cleaned_data['product']: _values['product'] = form.cleaned_data['product'] if form.cleaned_data['product_version']: _values['product_version'] = form.cleaned_data[ 'product_version'] if form.cleaned_data['owner']: _values['owner'] = form.cleaned_data['owner'] if form.cleaned_data['parent']: _values['parent'] = form.cleaned_data['parent'] if not (values.get('is_active') is None): _values['is_active'] = form.cleaned_data['is_active'] tps.update(**_values) if form.cleaned_data['env_group']: # NOTE: MyISAM does not support transaction, so no need to use # transaction.commit_on_success to control the commit. cursor = connection.writer_cursor in_condition = ','.join(itertools.repeat('%s', len(plan_ids))) del_env_group_sql = TP_CLEAR_ENV_GROUP % in_condition cursor.execute(del_env_group_sql, plan_ids) insert_values = ','.join(itertools.repeat('(%s, %s)', len(plan_ids))) insert_env_group_sql = TP_ADD_ENV_GROUP % insert_values args = list() for arg in itertools.izip(plan_ids, itertools.repeat(form.cleaned_data['env_group'].pk, len(plan_ids))): args.extend(arg) cursor.execute(insert_env_group_sql, args) transaction.commit_unless_managed() else: raise ValueError(forms.errors_to_list(form)) query = {'pk__in': tps.values_list('pk', flat=True)} return TestPlan.to_xmlrpc(query)
def update(request, plan_ids, values): """Updates the fields of the selected test plan. :param plan_ids: give one or more plan IDs. It could be an integer, a string containing comma separated IDs, or a list of int each of them is a plan ID. :type plan_ids: int, str or list :param dict values: a mapping containing these plan data to update * product: (int) ID of product * name: (str) * type: (int) ID of plan type * product_version: (int) ID of version * default_product_version: (int) alternative version ID. * owner: (str)/(int) user_name/user_id * parent: (int) Parent plan ID * is_active: bool True/False * env_group: (int) New environment group ID :return: a mapping of updated :class:`TestPlan`. :rtype: dict Example:: # Update product to 7 for plan 1 and 2 >>> TestPlan.update([1, 2], {'product': 7}) .. deprecated:: x.y ``default_product_version`` is deprecated and will be removed. """ from tcms.core import forms from tcms.xmlrpc.forms import EditPlanForm if values.get('default_product_version'): values['product_version'] = values.pop('default_product_version') form = EditPlanForm(values) if values.get('product_version') and not values.get('product'): raise ValueError('Field "product" is required by product_version') if values.get('product') and not values.get('product_version'): raise ValueError('Field "product_version" is required by product') if values.get('product_version') and values.get('product'): form.populate(product_id=values['product']) plan_ids = pre_process_ids(value=plan_ids) tps = TestPlan.objects.filter(pk__in=plan_ids) if form.is_valid(): _values = dict() if form.cleaned_data['name']: _values['name'] = form.cleaned_data['name'] if form.cleaned_data['type']: _values['type'] = form.cleaned_data['type'] if form.cleaned_data['product']: _values['product'] = form.cleaned_data['product'] if form.cleaned_data['product_version']: _values['product_version'] = form.cleaned_data[ 'product_version'] if form.cleaned_data['owner']: _values['owner'] = form.cleaned_data['owner'] if form.cleaned_data['parent']: _values['parent'] = form.cleaned_data['parent'] if not (values.get('is_active') is None): _values['is_active'] = form.cleaned_data['is_active'] tps.update(**_values) # requested to update environment group for selected test plans if form.cleaned_data['env_group']: # prepare the list of new objects to be inserted into DB new_objects = [ TCMSEnvPlanMap( plan_id=plan_pk, group_id=form.cleaned_data['env_group'].pk ) for plan_pk in plan_ids ] # first delete the old values (b/c many-to-many I presume ?) TCMSEnvPlanMap.objects.filter(plan__in=plan_ids).delete() # then create all objects with 1 INSERT TCMSEnvPlanMap.objects.bulk_create(new_objects) else: raise ValueError(forms.errors_to_list(form)) query = {'pk__in': tps.values_list('pk', flat=True)} return TestPlan.to_xmlrpc(query)