Ejemplo n.º 1
0
    def recursively_delete_group_test(self, mocked_delete_file):
        """
        Test that recursively_delete_group() works
        """
        # GIVEN: An ImageGroups object and mocked functions
        ImageFilenames.group_id = 1
        ImageGroups.parent_id = 1
        self.media_item.manager = MagicMock()
        self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
        self.media_item.service_path = ''
        test_group = ImageGroups()
        test_group.id = 1

        # WHEN: recursively_delete_group() is called
        self.media_item.recursively_delete_group(test_group)

        # THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
        self.assertEquals(mocked_delete_file.call_count, 12,
                          'delete_file() should have been called 12 times')
        self.assertEquals(
            self.media_item.manager.delete_object.call_count, 7,
            'manager.delete_object() should be called exactly 7 times')

        # CLEANUP: Remove added attribute from Image Filenames and ImageGroups
        delattr(ImageFilenames, 'group_id')
        delattr(ImageGroups, 'parent_id')
Ejemplo n.º 2
0
    def recursively_delete_group_test(self):
        """
        Test that recursively_delete_group() works
        """
        # GIVEN: An ImageGroups object and mocked functions
        with patch('openlp.core.utils.delete_file') as mocked_delete_file:
            ImageFilenames.group_id = 1
            ImageGroups.parent_id = 1
            self.media_item.manager = MagicMock()
            self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
            self.media_item.servicePath = ""
            test_group = ImageGroups()
            test_group.id = 1

            # WHEN: recursively_delete_group() is called
            self.media_item.recursively_delete_group(test_group)

            # THEN:
            assert mocked_delete_file.call_count == 0, 'delete_file() should not be called'
            assert self.media_item.manager.delete_object.call_count == 7, \
                'manager.delete_object() should be called exactly 7 times'

            # CLEANUP: Remove added attribute from ImageFilenames and ImageGroups
            delattr(ImageFilenames, 'group_id')
            delattr(ImageGroups, 'parent_id')
Ejemplo n.º 3
0
 def on_add_group_click(self):
     """
     Called to add a new group
     """
     # Find out if a group must be pre-selected
     preselect_group = 0
     selected_items = self.list_view.selectedItems()
     if selected_items:
         selected_item = selected_items[0]
         if isinstance(selected_item.data(0, QtCore.Qt.UserRole), ImageFilenames):
             selected_item = selected_item.parent()
         if isinstance(selected_item, QtGui.QTreeWidgetItem):
             if isinstance(selected_item.data(0, QtCore.Qt.UserRole), ImageGroups):
                 preselect_group = selected_item.data(0, QtCore.Qt.UserRole).id
     # Show 'add group' dialog
     if self.add_group_form.exec_(show_top_level_group=True, selected_group=preselect_group):
         new_group = ImageGroups.populate(parent_id=self.add_group_form.parent_group_combobox.itemData(
             self.add_group_form.parent_group_combobox.currentIndex(), QtCore.Qt.UserRole),
             group_name=self.add_group_form.name_edit.text())
         if not self.check_group_exists(new_group):
             if self.manager.save_object(new_group):
                 self.load_full_list(self.manager.get_all_objects(
                     ImageFilenames, order_by_ref=ImageFilenames.filename))
                 self.expand_group(new_group.id)
                 self.fill_groups_combobox(self.choose_group_form.group_combobox)
                 self.fill_groups_combobox(self.add_group_form.parent_group_combobox)
             else:
                 critical_error_message_box(
                     message=translate('ImagePlugin.AddGroupForm', 'Could not add the new group.'))
         else:
             critical_error_message_box(message=translate('ImagePlugin.AddGroupForm', 'This group already exists.'))
Ejemplo n.º 4
0
    def test_recursively_delete_group(self, mocked_delete_file):
        """
        Test that recursively_delete_group() works
        """
        # GIVEN: An ImageGroups object and mocked functions
        ImageFilenames.group_id = 1
        ImageGroups.parent_id = 1
        self.media_item.manager = MagicMock()
        self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
        self.media_item.service_path = ''
        test_group = ImageGroups()
        test_group.id = 1

        # WHEN: recursively_delete_group() is called
        self.media_item.recursively_delete_group(test_group)

        # THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
        self.assertEquals(mocked_delete_file.call_count, 12, 'delete_file() should have been called 12 times')
        self.assertEquals(self.media_item.manager.delete_object.call_count, 7,
                          'manager.delete_object() should be called exactly 7 times')

        # CLEANUP: Remove added attribute from Image Filenames and ImageGroups
        delattr(ImageFilenames, 'group_id')
        delattr(ImageGroups, 'parent_id')
Ejemplo n.º 5
0
    def dnd_move_internal(self, target):
        """
        Handle drag-and-drop moving of images within the media manager

        :param target: This contains the QTreeWidget that is the target of the DnD action
        """
        items_to_move = self.list_view.selectedItems()
        # Determine group to move images to
        target_group = target
        if target_group is not None and isinstance(
                target_group.data(0, QtCore.Qt.UserRole), ImageFilenames):
            target_group = target.parent()
        # Move to toplevel
        if target_group is None:
            target_group = self.list_view.invisibleRootItem()
            target_group.setData(0, QtCore.Qt.UserRole, ImageGroups())
            target_group.data(0, QtCore.Qt.UserRole).id = 0
        # Move images in the treeview
        items_to_save = []
        for item in items_to_move:
            if isinstance(item.data(0, QtCore.Qt.UserRole), ImageFilenames):
                if isinstance(item.parent(), QtWidgets.QTreeWidgetItem):
                    item.parent().removeChild(item)
                else:
                    self.list_view.invisibleRootItem().removeChild(item)
                target_group.addChild(item)
                item.setSelected(True)
                item_data = item.data(0, QtCore.Qt.UserRole)
                item_data.group_id = target_group.data(0,
                                                       QtCore.Qt.UserRole).id
                items_to_save.append(item_data)
        target_group.setExpanded(True)
        # Update the group ID's of the images in the database
        self.manager.save_objects(items_to_save)
        # Sort the target group
        group_items = []
        image_items = []
        for item in target_group.takeChildren():
            if isinstance(item.data(0, QtCore.Qt.UserRole), ImageGroups):
                group_items.append(item)
            if isinstance(item.data(0, QtCore.Qt.UserRole), ImageFilenames):
                image_items.append(item)
        group_items.sort(key=lambda item: get_locale_key(item.text(0)))
        target_group.addChildren(group_items)
        image_items.sort(key=lambda item: get_locale_key(item.text(0)))
        target_group.addChildren(image_items)
Ejemplo n.º 6
0
 def _recursively_delete_group_side_effect(*args, **kwargs):
     """
     Side effect method that creates custom return values for the recursively_delete_group method
     """
     if args[1] == ImageFilenames and args[2]:
         # Create some fake objects that should be removed
         returned_object1 = ImageFilenames()
         returned_object1.id = 1
         returned_object1.filename = '/tmp/test_file_1.jpg'
         returned_object2 = ImageFilenames()
         returned_object2.id = 2
         returned_object2.filename = '/tmp/test_file_2.jpg'
         returned_object3 = ImageFilenames()
         returned_object3.id = 3
         returned_object3.filename = '/tmp/test_file_3.jpg'
         return [returned_object1, returned_object2, returned_object3]
     if args[1] == ImageGroups and args[2]:
         # Change the parent_id that is matched so we don't get into an endless loop
         ImageGroups.parent_id = 0
         # Create a fake group that will be used in the next run
         returned_object1 = ImageGroups()
         returned_object1.id = 1
         return [returned_object1]
     return []
Ejemplo n.º 7
0
    def load_list(self, images, target_group=None, initial_load=False):
        """
        Add new images to the database. This method is called when adding images using the Add button or DnD.

        :param images: A List of strings containing the filenames of the files to be loaded
        :param target_group: The QTreeWidgetItem of the group that will be the parent of the added files
        :param initial_load: When set to False, the busy cursor and progressbar will be shown while loading images
        """
        parent_group = None
        if target_group is None:
            # Find out if a group must be pre-selected
            preselect_group = None
            selected_items = self.list_view.selectedItems()
            if selected_items:
                selected_item = selected_items[0]
                if isinstance(selected_item.data(0, QtCore.Qt.UserRole),
                              ImageFilenames):
                    selected_item = selected_item.parent()
                if isinstance(selected_item, QtWidgets.QTreeWidgetItem):
                    if isinstance(selected_item.data(0, QtCore.Qt.UserRole),
                                  ImageGroups):
                        preselect_group = selected_item.data(
                            0, QtCore.Qt.UserRole).id
            # Enable and disable parts of the 'choose group' form
            if preselect_group is None:
                self.choose_group_form.nogroup_radio_button.setChecked(True)
                self.choose_group_form.nogroup_radio_button.setFocus()
                self.choose_group_form.existing_radio_button.setChecked(False)
                self.choose_group_form.new_radio_button.setChecked(False)
            else:
                self.choose_group_form.nogroup_radio_button.setChecked(False)
                self.choose_group_form.existing_radio_button.setChecked(True)
                self.choose_group_form.new_radio_button.setChecked(False)
                self.choose_group_form.group_combobox.setFocus()
            if self.manager.get_object_count(ImageGroups) == 0:
                self.choose_group_form.existing_radio_button.setDisabled(True)
                self.choose_group_form.group_combobox.setDisabled(True)
            else:
                self.choose_group_form.existing_radio_button.setDisabled(False)
                self.choose_group_form.group_combobox.setDisabled(False)
            # Ask which group the images should be saved in
            if self.choose_group_form.exec(selected_group=preselect_group):
                if self.choose_group_form.nogroup_radio_button.isChecked():
                    # User chose 'No group'
                    parent_group = ImageGroups()
                    parent_group.id = 0
                elif self.choose_group_form.existing_radio_button.isChecked():
                    # User chose 'Existing group'
                    group_id = self.choose_group_form.group_combobox.itemData(
                        self.choose_group_form.group_combobox.currentIndex(),
                        QtCore.Qt.UserRole)
                    parent_group = self.manager.get_object_filtered(
                        ImageGroups, ImageGroups.id == group_id)
                elif self.choose_group_form.new_radio_button.isChecked():
                    # User chose 'New group'
                    parent_group = ImageGroups()
                    parent_group.parent_id = 0
                    parent_group.group_name = self.choose_group_form.new_group_edit.text(
                    )
                    self.manager.save_object(parent_group)
                    self.fill_groups_combobox(
                        self.choose_group_form.group_combobox)
                    self.fill_groups_combobox(
                        self.add_group_form.parent_group_combobox)
        else:
            parent_group = target_group.data(0, QtCore.Qt.UserRole)
            if isinstance(parent_group, ImageFilenames):
                if parent_group.group_id == 0:
                    parent_group = ImageGroups()
                    parent_group.id = 0
                else:
                    parent_group = target_group.parent().data(
                        0, QtCore.Qt.UserRole)
        # If no valid parent group is found, do nothing
        if not isinstance(parent_group, ImageGroups):
            return
        # Initialize busy cursor and progress bar
        self.application.set_busy_cursor()
        self.main_window.display_progress_bar(len(images))
        # Save the new images in the database
        self.save_new_images_list(images,
                                  group_id=parent_group.id,
                                  reload_list=False)
        self.load_full_list(self.manager.get_all_objects(
            ImageFilenames, order_by_ref=ImageFilenames.filename),
                            initial_load=initial_load,
                            open_group=parent_group)
        self.application.set_normal_cursor()
Ejemplo n.º 8
0
    def load_list(self, images, target_group=None, initial_load=False):
        """
        Add new images to the database. This method is called when adding images using the Add button or DnD.

        :param images: A List of strings containing the filenames of the files to be loaded
        :param target_group: The QTreeWidgetItem of the group that will be the parent of the added files
        :param initial_load: When set to False, the busy cursor and progressbar will be shown while loading images
        """
        parent_group = None
        if target_group is None:
            # Find out if a group must be pre-selected
            preselect_group = None
            selected_items = self.list_view.selectedItems()
            if selected_items:
                selected_item = selected_items[0]
                if isinstance(selected_item.data(0, QtCore.Qt.UserRole), ImageFilenames):
                    selected_item = selected_item.parent()
                if isinstance(selected_item, QtGui.QTreeWidgetItem):
                    if isinstance(selected_item.data(0, QtCore.Qt.UserRole), ImageGroups):
                        preselect_group = selected_item.data(0, QtCore.Qt.UserRole).id
            # Enable and disable parts of the 'choose group' form
            if preselect_group is None:
                self.choose_group_form.nogroup_radio_button.setChecked(True)
                self.choose_group_form.nogroup_radio_button.setFocus()
                self.choose_group_form.existing_radio_button.setChecked(False)
                self.choose_group_form.new_radio_button.setChecked(False)
            else:
                self.choose_group_form.nogroup_radio_button.setChecked(False)
                self.choose_group_form.existing_radio_button.setChecked(True)
                self.choose_group_form.new_radio_button.setChecked(False)
                self.choose_group_form.group_combobox.setFocus()
            if self.manager.get_object_count(ImageGroups) == 0:
                self.choose_group_form.existing_radio_button.setDisabled(True)
                self.choose_group_form.group_combobox.setDisabled(True)
            else:
                self.choose_group_form.existing_radio_button.setDisabled(False)
                self.choose_group_form.group_combobox.setDisabled(False)
            # Ask which group the images should be saved in
            if self.choose_group_form.exec_(selected_group=preselect_group):
                if self.choose_group_form.nogroup_radio_button.isChecked():
                    # User chose 'No group'
                    parent_group = ImageGroups()
                    parent_group.id = 0
                elif self.choose_group_form.existing_radio_button.isChecked():
                    # User chose 'Existing group'
                    group_id = self.choose_group_form.group_combobox.itemData(
                        self.choose_group_form.group_combobox.currentIndex(), QtCore.Qt.UserRole)
                    parent_group = self.manager.get_object_filtered(ImageGroups, ImageGroups.id == group_id)
                elif self.choose_group_form.new_radio_button.isChecked():
                    # User chose 'New group'
                    parent_group = ImageGroups()
                    parent_group.parent_id = 0
                    parent_group.group_name = self.choose_group_form.new_group_edit.text()
                    self.manager.save_object(parent_group)
                    self.fill_groups_combobox(self.choose_group_form.group_combobox)
                    self.fill_groups_combobox(self.add_group_form.parent_group_combobox)
        else:
            parent_group = target_group.data(0, QtCore.Qt.UserRole)
            if isinstance(parent_group, ImageFilenames):
                if parent_group.group_id == 0:
                    parent_group = ImageGroups()
                    parent_group.id = 0
                else:
                    parent_group = target_group.parent().data(0, QtCore.Qt.UserRole)
        # If no valid parent group is found, do nothing
        if not isinstance(parent_group, ImageGroups):
            return
        # Initialize busy cursor and progress bar
        self.application.set_busy_cursor()
        self.main_window.display_progress_bar(len(images))
        # Save the new images in the database
        self.save_new_images_list(images, group_id=parent_group.id, reload_list=False)
        self.load_full_list(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename),
                            initial_load=initial_load, open_group=parent_group)
        self.application.set_normal_cursor()