Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion sardes/tables/tests/test_table_manual_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
# ---- Third party imports
import pandas as pd
import pytest
from qtpy.QtWidgets import QMessageBox
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QMessageBox, QApplication

# ---- Local imports
from sardes.utils.data_operations import are_values_equal
Expand Down Expand Up @@ -268,5 +269,48 @@ def test_unique_constraint(tablewidget, qtbot, mocker, dbaccessor):
assert tablemodel.data_edit_count() == 0


def test_import_from_clipboard(tablewidget, qtbot, mocker, dbaccessor):
"""
Test that importing water level measurements from the clipboard is
working as expected.
"""
import_tool = tablewidget._tools['import_from_clipboard']

# We need to patch the message box that appears to warn user when
# errors were encountered while importing the data.
qmsgbox_patcher = mocker.patch.object(
QMessageBox, 'warning', return_value=QMessageBox.Ok)

# Try importing manual measurements when the clipboard is empty.
QApplication.clipboard().clear()
qtbot.mouseClick(import_tool.toolbutton(), Qt.LeftButton)

assert qmsgbox_patcher.call_count == 1
assert tablewidget.visible_row_count() == 6

# Send new manual measurements data to the clipboard. Note that the
# second line is intentionnaly filled with bad data to trigger
# an error warning.
new_data = [
['03040002', '2023-04-23 4:25', 34.56, 'Imported from clipboard'],
['unknown', '2023-45-45 4:25', 'erreur', None]
]
pd.DataFrame(
new_data,
columns=["Well ID", "Date", "Water LEvel", "Notes"]
).to_clipboard(sep='\t')

# Import the data from the clipboard.
qtbot.mouseClick(import_tool.toolbutton(), Qt.LeftButton)

assert qmsgbox_patcher.call_count == 2
assert tablewidget.visible_row_count() == 6 + 2

assert tablewidget.get_data_for_row(6) == [
'03040002', '2023-04-23 04:25:00', '34.56', 'Imported from clipboard']
assert tablewidget.get_data_for_row(7) == [
'', '', '', '']


if __name__ == "__main__":
pytest.main(['-x', __file__, '-v', '-rw'])
25 changes: 14 additions & 11 deletions sardes/widgets/tableviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,31 @@ class ImportFromClipboardTool(SardesTool):
A tool to append the Clipboard contents to a Sardes table widget.
"""

def __init__(self, parent):
def __init__(self, table):
super().__init__(
parent,
table,
name='import_from_clipboard',
text=_('Import from Clipboard'),
icon='import_clipboard',
tip=_('Add the Clipboard contents to this table.')
)

def __triggered__(self):
new_data = pd.read_clipboard(sep='\t', dtype='str', header=None)
try:
new_data = pd.read_clipboard(sep='\t', dtype='str', header=None)
except pd.errors.EmptyDataError:
new_data = pd.DataFrame([])
if new_data.empty:
self.parent.show_message(
self.table.show_message(
title=_("Warning"),
message=_("Nothing was added to the table because the "
"Clipboard was empty."),
func='warning')
return

table_visible_columns = self.parent.tableview.visible_columns()
table_visible_columns = self.table.tableview.visible_columns()
if len(new_data.columns) > len(table_visible_columns):
self.parent.show_message(
self.table.show_message(
title=_("Warning"),
message=_("The Clipboard contents cannot be added to "
"the table because the number of columns of the "
Expand All @@ -79,7 +82,7 @@ def __triggered__(self):
return

column_names_headers_map = (
self.parent.model().column_names_headers_map())
self.table.model().column_names_headers_map())
table_visible_labels = [
column_names_headers_map[column].lower().replace(' ', '')
for column in table_visible_columns]
Expand Down Expand Up @@ -109,8 +112,8 @@ def __triggered__(self):

warning_messages = []
for column in new_data.columns:
delegate = self.parent.tableview.itemDelegateForColumn(
self.parent.model().column_names().index(column))
delegate = self.table.tableview.itemDelegateForColumn(
self.table.model().column_names().index(column))
new_data[column], warning_message = delegate.format_data(
new_data[column])
if warning_message is not None:
Expand All @@ -131,7 +134,7 @@ def __triggered__(self):
';</li><li>'.join(warning_messages)))
else:
values = new_data.to_dict(orient='records')
self.parent.tableview._append_row(values)
self.table.tableview._append_row(values)
if len(warning_messages):
formatted_message = _(
"The following error(s) occurred while adding the "
Expand All @@ -140,7 +143,7 @@ def __triggered__(self):
'<ul style="margin-left:-30px"><li>{}.</li></ul>'.format(
';</li><li>'.join(warning_messages)))
if formatted_message is not None:
self.parent.show_message(
self.table.show_message(
title=_("Warning"),
message=formatted_message,
func='warning')
Expand Down