Skip to content

Commit

Permalink
remove duplicate MandatoryInputItemModel
Browse files Browse the repository at this point in the history
  • Loading branch information
diivi committed Sep 1, 2023
1 parent 5d71208 commit 15c87e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
7 changes: 5 additions & 2 deletions src/vorta/store/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def run_migrations(current_schema, db_connection):
enabled=True,
)
except pw.IntegrityError:
# Previously, users could add the same exclusion multiple times, since it was just a string.
# Now, we enforce uniqueness, so we need to catch the IntegrityError and ignore it.
pass

previous_exclude_if_present = profile.exclude_if_present.splitlines() if profile.exclude_if_present else []
Expand All @@ -298,6 +300,7 @@ def run_migrations(current_schema, db_connection):
enabled=True,
)
except pw.IntegrityError:
# Same as above
pass

profile.exclude_if_present = ''
Expand All @@ -309,15 +312,15 @@ def run_migrations(current_schema, db_connection):
ExclusionModel.create(
profile=profile,
name='*/node_modules',
enabled=True,
enabled=False,
)

if ExcludeIfPresentModel.select().count() == 0:
for profile in BackupProfileModel:
ExcludeIfPresentModel.create(
profile=profile,
name='.nobackup',
enabled=True,
enabled=False,
)


Expand Down
7 changes: 4 additions & 3 deletions src/vorta/views/exclude_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ class MandatoryInputItemModel(QStandardItemModel):
A model that prevents the user from adding an empty item to the list.
'''

def __init__(self, parent=None):
def __init__(self, parent=None, model=None):
super().__init__(parent)
self.model = model

def setData(self, index: QModelIndex, value, role: int = ...) -> bool:
# When a user-added item in edit mode has no text, remove it from the list.
if role == Qt.ItemDataRole.EditRole and value == '':
self.removeRow(index.row())
return True
if role == Qt.ItemDataRole.EditRole and ExclusionModel.get_or_none(ExclusionModel.name == value):
if role == Qt.ItemDataRole.EditRole and ExclusionModel.get_or_none(self.model.name == value):
QMessageBox.critical(
self.parent(),
'Error',
Expand All @@ -57,7 +58,7 @@ def __init__(self, profile, parent=None):

self.buttonBox.rejected.connect(self.close)

self.customExclusionsModel = MandatoryInputItemModel()
self.customExclusionsModel = MandatoryInputItemModel(model=ExclusionModel)
self.customExclusionsList.setModel(self.customExclusionsModel)
self.customExclusionsModel.itemChanged.connect(self.custom_item_changed)
self.customExclusionsList.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
Expand Down
33 changes: 4 additions & 29 deletions src/vorta/views/exclude_if_present_dialog.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,23 @@
from PyQt6 import uic
from PyQt6.QtCore import QModelIndex, QObject, Qt
from PyQt6.QtGui import QStandardItem, QStandardItemModel
from PyQt6.QtCore import QObject, Qt
from PyQt6.QtGui import QStandardItem
from PyQt6.QtWidgets import (
QAbstractItemView,
QApplication,
QMenu,
QMessageBox,
QStyledItemDelegate,
)

from vorta.i18n import translate
from vorta.store.models import ExcludeIfPresentModel
from vorta.utils import get_asset
from vorta.views.exclude_dialog import MandatoryInputItemModel
from vorta.views.utils import get_colored_icon

uifile = get_asset('UI/excludeifpresentdialog.ui')
ExcludeIfPresentDialogUi, ExcludeIfPresentDialogBase = uic.loadUiType(uifile)


class MandatoryInputItemModel(QStandardItemModel):
'''
A model that prevents the user from adding an empty item to the list.
'''

def __init__(self, parent=None):
super().__init__(parent)

def setData(self, index: QModelIndex, value, role: int = ...) -> bool:
# When a user-added item in edit mode has no text, remove it from the list.
if role == Qt.ItemDataRole.EditRole and value == '':
self.removeRow(index.row())
return True
if role == Qt.ItemDataRole.EditRole and ExcludeIfPresentModel.get_or_none(ExcludeIfPresentModel.name == value):
QMessageBox.critical(
self.parent(),
'Error',
'This exclusion already exists.',
)
self.removeRow(index.row())
return False

return super().setData(index, value, role)


class ExcludeIfPresentDialog(ExcludeIfPresentDialogBase, ExcludeIfPresentDialogUi):
def __init__(self, profile, parent=None):
super().__init__(parent)
Expand All @@ -52,7 +27,7 @@ def __init__(self, profile, parent=None):

self.buttonBox.rejected.connect(self.close)

self.customExclusionsModel = MandatoryInputItemModel()
self.customExclusionsModel = MandatoryInputItemModel(model=ExcludeIfPresentModel)
self.customExclusionsList.setModel(self.customExclusionsModel)
self.customExclusionsModel.itemChanged.connect(self.custom_item_changed)
self.customExclusionsList.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
Expand Down

0 comments on commit 15c87e6

Please sign in to comment.