def test_delete_activity_param(qtbot): """ Remove activity parameters. """ project_db_tab = ParameterDefinitionTab() qtbot.addWidget(project_db_tab) project_db_tab.build_tables() table = project_db_tab.activity_table rect = table.visualRect(table.proxy_model.index(0, 0)) qtbot.mouseClick(table.viewport(), QtCore.Qt.LeftButton, pos=rect.center()) group = table.get_current_group() # Now delete the parameter for the selected row. table.delete_parameter(table.currentIndex()) assert table.rowCount() == 2 assert ActivityParameter.select().count() == 2 assert Group.select().where(Group.name == group).exists() # And delete the other two parameters one by one. table.delete_parameter(table.proxy_model.index(0, 0)) table.delete_parameter(table.proxy_model.index(0, 0)) assert table.rowCount() == 0 assert ActivityParameter.select().count() == 0 # Group is automatically removed with the last parameter gone assert not Group.select().where(Group.name == group).exists()
def get_usable_parameters(self): """ Use the `key` set for the table to determine the database and group of the activity, using that information to constrain the usable parameters. """ project = ([k, v, "project"] for k, v in ProjectParameter.static().items()) if self.key is None: return project database = ([k, v, "database"] for k, v in DatabaseParameter.static(self.key[0]).items()) # Determine if the activity is already part of a parameter group. query = (Group.select().join( ActivityParameter, on=(Group.name == ActivityParameter.group)).where( ActivityParameter.database == self.key[0], ActivityParameter.code == self.key[1]).distinct()) if query.exists(): group = query.get() # First, build a list for parameters in the same group activity = ([p.name, p.amount, "activity"] for p in ActivityParameter.select().where( ActivityParameter.group == group.name)) # Then extend the list with parameters from groups in the `order` # field additions = ([p.name, p.amount, "activity"] for p in ActivityParameter.select().where( ActivityParameter.group << group.order)) activity = itertools.chain(activity, additions) else: activity = [] return itertools.chain(project, database, activity)