def test_1_user_group_no_name(self): # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 group1 = UserGroup("test_group1", hallo1) perm4 = PermissionMask() group1.permission_mask = perm4 hallo1.add_user_group(group1) # Try to get permissions of non-existent user group try: data = self.perm_cont.find_permission_mask(["user_group=test_group2"], user1, chan1) assert False, "Find permission mask should have failed." except modules.PermissionControl.PermissionControlException as e: assert "error" in str(e).lower() assert "no user group exists by that name" in str(e).lower()
def load_from_xml(): try: doc = ElementTree.parse("config/config.xml") except (OSError, IOError): print("No current config, loading from default.") doc = ElementTree.parse("config/config-default.xml") new_hallo = Hallo() root = doc.getroot() new_hallo.default_nick = root.findtext("default_nick") new_hallo.default_prefix = Commons.string_from_file(root.findtext("default_prefix")) new_hallo.default_full_name = root.findtext("default_full_name") new_hallo.function_dispatcher = FunctionDispatcher.from_xml( ElementTree.tostring(root.find("function_dispatcher")), new_hallo) user_group_list_xml = root.find("user_group_list") for user_group_xml in user_group_list_xml.findall("user_group"): user_group_obj = UserGroup.from_xml(ElementTree.tostring(user_group_xml), new_hallo) new_hallo.add_user_group(user_group_obj) server_list_xml = root.find("server_list") for server_xml in server_list_xml.findall("server"): server_obj = new_hallo.server_factory.new_server_from_xml(ElementTree.tostring(server_xml)) new_hallo.add_server(server_obj) if root.find("permission_mask") is not None: new_hallo.permission_mask = PermissionMask.from_xml(ElementTree.tostring(root.find("permission_mask"))) api_key_list_xml = root.find("api_key_list") for api_key_xml in api_key_list_xml.findall("api_key"): api_key_name = api_key_xml.findtext("name") api_key_key = api_key_xml.findtext("key") new_hallo.add_api_key(api_key_name, api_key_key) return new_hallo
def test_1_user_group_name(self): # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 group1 = UserGroup("test_group1", hallo1) perm4 = PermissionMask() group1.permission_mask = perm4 hallo1.add_user_group(group1) # Get permissions of specified user group data = self.perm_cont.find_permission_mask(["user_group=test_group1"], user1, chan1) assert data == perm4, "Did not find the correct permission mask."
def __init__(self, parent, username): self._user = username self._group_model = Group() self._user_group_model = UserGroup() self._custom_goal = CustomGoal() self.data = self.read_data() self.proxyModel = SortFilterProxyModel() # make a proxy model object self.model = CustomTableModel(self.data) self.proxyModel.setSourceModel(self.model) self.ui = Ui_group(parent, self.proxyModel, self.data) #self.ui.group_table.setModel(self.proxyModel) # At initialisation, the proxyModel is the model itself self.checkGroupSearch() # method for searching the database self.ui.group_table.clicked.connect( partial(self.displayInQlineEdit )) # method for displaying text when row is clicked #self.ui.group_name_output.textEdited.connect(partial(self.addNewGroup)) # method for editing calorific value when portion changes self.ui.create_group_button.clicked.connect(partial( self.addNewGroup)) # method to add new food to database self.ui.join_group_button.clicked.connect(partial( self.joinNewGroup)) # method to add new food to database
class GroupPresenter: def __init__(self, parent, username): self._user = username self._group_model = Group() self._user_group_model = UserGroup() self._custom_goal = CustomGoal() self.data = self.read_data() self.proxyModel = SortFilterProxyModel() # make a proxy model object self.model = CustomTableModel(self.data) self.proxyModel.setSourceModel(self.model) self.ui = Ui_group(parent, self.proxyModel, self.data) #self.ui.group_table.setModel(self.proxyModel) # At initialisation, the proxyModel is the model itself self.checkGroupSearch() # method for searching the database self.ui.group_table.clicked.connect( partial(self.displayInQlineEdit )) # method for displaying text when row is clicked #self.ui.group_name_output.textEdited.connect(partial(self.addNewGroup)) # method for editing calorific value when portion changes self.ui.create_group_button.clicked.connect(partial( self.addNewGroup)) # method to add new food to database self.ui.join_group_button.clicked.connect(partial( self.joinNewGroup)) # method to add new food to database # self.ui.setupUi(self.group) # pass the diet widget to the ui def read_data(self): session = make_session() # query FoodDictionary to fetch all rows .with_entities(Group.groupName, CustomGoal.goal_description) data = session.query(Group).join(CustomGoal) \ .with_entities(Group.groupName, CustomGoal.goal_description, CustomGoal.date).all() session.close() return data def displayInQlineEdit(self): index = self.ui.group_table.currentIndex( ) # fetched index of row currently selected in the table groupName = self.ui.group_table.model().index(index.row(), 0) groupGoal = self.ui.group_table.model().index(index.row(), 1) self.ui.group_name_output.setText( groupName.data()) # display selected food name self.ui.group_goal_output.setText( groupGoal.data()) # display selected food name def checkGroupSearch(self): self.ui.search_input.textEdited.connect( partial(self.filterRegExpChanged) ) # signal to filter table based on search query def filterRegExpChanged(self): syntax = QtCore.QRegExp.PatternSyntax(QtCore.QRegExp.FixedString) regExp = QtCore.QRegExp(self.ui.search_input.text(), QtCore.Qt.CaseInsensitive, syntax) self.proxyModel.setFilterRegExp(regExp) def addNewGroup(self): if (self.ui.group_name_input.text() == '' or self.ui.group_goal_input.text() == ''): # disallow adding food with empty text print('not allowed') display_message('Input cannot be empty', 'Please enter valid group name and description') else: groupName = self.ui.group_name_input.text() groupType = self.ui.group_type.currentText().upper() groupGoal = self.ui.group_goal_input.text() completionDate = self.ui.completion_date.date().toPyDate() try: groupID = Group.createGroup(groupName, groupType, completionDate) self._user_group_model.createUserGroup(self._user, groupID) self._custom_goal.create_custom_goal(self._user, groupGoal, date.today(), group_id=groupID) self.data = self.read_data() self.model = CustomTableModel(self.data) # reset model self.proxyModel.setSourceModel( self.model ) # display added food without a need for refreshing self.ui.group_table.setModel(self.proxyModel) display_message('New group added', 'You have added a new group successfully', False) except Exception as e: display_message( 'Invalid input', 'Please enter a valid group name or description!') print(e) # function to put user into a group def joinNewGroup(self): groupName = self.ui.group_name_output.text() groupGoal = self.ui.group_goal_output.text() try: self._user_group_model.addUserGroup( self._user, self._group_model.get_id_by_name(groupName)) self.data = self.read_data() self.model = CustomTableModel(self.data) # reset model self.proxyModel.setSourceModel( self.model) # display added food without a need for refreshing self.ui.group_table.setModel(self.proxyModel) display_message('Joined group', 'You have joined a new group successfully', False) except Exception as e: display_message( 'Fail to join group', 'Error joining the group, please check your input and try again!' ) print(e) def page(self): return self.ui.scrollArea