|
| 1 | +from typing import List |
| 2 | + |
1 | 3 | from qtpy import QtCore, QtWidgets |
2 | 4 |
|
3 | 5 | import bw2data as bd |
|
12 | 14 |
|
13 | 15 | class DatabaseDelete(ABAction): |
14 | 16 | """ |
15 | | - Deletes a specified database from the project after user confirmation. |
| 17 | + Deletes one or more databases from the project after user confirmation. |
16 | 18 |
|
17 | 19 | This method performs the following steps: |
18 | | - - Displays a confirmation dialog to the user with the database name and record count. |
19 | | - - If the user confirms, deletes the database, its upstream exchanges, and associated parameters. |
20 | | - - Removes the database from the project settings. |
| 20 | + - Displays a confirmation dialog to the user with the database name(s) and total record count. |
| 21 | + - If the user confirms, deletes the database(s), their upstream exchanges, and associated parameters. |
| 22 | + - Removes the database(s) from the project settings. |
21 | 23 |
|
22 | 24 | Args: |
23 | | - db_name (str): The name of the database to be deleted. |
| 25 | + db_names (List[str]): The name(s) of the database(s) to be deleted. |
24 | 26 |
|
25 | 27 | Steps: |
26 | 28 | - Set the cursor to a waiting state while gathering data for large databases. |
27 | | - - Retrieve the record count for the specified database. |
28 | | - - Construct a warning message with the database name and record count. |
| 29 | + - Retrieve the record count for the specified database(s). |
| 30 | + - Construct a warning message with the database name(s) and record count. |
29 | 31 | - Display a confirmation dialog to the user. |
30 | 32 | - If the user cancels, exit the method. |
31 | 33 | - Set the cursor to a waiting state while performing the deletion. |
32 | | - - Delete upstream exchanges associated with the database. |
33 | | - - Remove the database from the Brightway2 project. |
| 34 | + - Delete upstream exchanges associated with the database(s). |
| 35 | + - Remove the database(s) from the Brightway2 project. |
34 | 36 | - Delete database parameters. |
35 | | - - Remove the database from the project settings. |
| 37 | + - Remove the database(s) from the project settings. |
36 | 38 | - Restore the cursor to its default state. |
37 | 39 | """ |
38 | 40 |
|
39 | 41 | icon = qicons.delete |
40 | | - text = "Delete database" |
41 | | - tool_tip = "Delete this database from the project" |
| 42 | + text = "Delete databases" |
| 43 | + tool_tip = "Delete database(s) from the project" |
42 | 44 |
|
43 | 45 | @staticmethod |
44 | 46 | @exception_dialogs |
45 | | - def run(db_name: str): |
| 47 | + def run(db_names: List[str]): |
46 | 48 | # gathering data will take time for large databases |
47 | 49 | QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) |
48 | 50 |
|
49 | | - # get the record count from the database controller |
50 | | - db_name = db_name |
51 | | - n_records = AB_metadata.dataframe[AB_metadata.dataframe["database"] == db_name].shape[0] |
| 51 | + # get the total record count from all databases |
| 52 | + total_records = 0 |
| 53 | + for db_name in db_names: |
| 54 | + n_records = AB_metadata.dataframe[AB_metadata.dataframe["database"] == db_name].shape[0] |
| 55 | + total_records += n_records |
52 | 56 |
|
53 | 57 | # construct warning text |
54 | | - text = f"Are you sure you want to delete database '{db_name}'?" |
55 | | - if n_records: |
56 | | - text += f" It contains {n_records} activities" |
| 58 | + if len(db_names) == 1: |
| 59 | + text = f"Are you sure you want to delete database <b>'{db_names[0]}'</b>?" |
| 60 | + if total_records: |
| 61 | + text += f" It contains {total_records} activities." |
| 62 | + else: |
| 63 | + text = f"Are you sure you want to delete {len(db_names)} databases?" |
| 64 | + if total_records: |
| 65 | + text += f" They contain {total_records} activities in total." |
57 | 66 |
|
58 | 67 | # ask the user for confirmation |
59 | 68 | QtWidgets.QApplication.restoreOverrideCursor() |
60 | 69 | response = QtWidgets.QMessageBox.question( |
61 | | - application.main_window, "Delete database?", text |
| 70 | + application.main_window, build_title(db_names), text |
62 | 71 | ) |
63 | 72 |
|
64 | 73 | # return if the user cancels |
65 | | - if response != response.Yes: |
| 74 | + if response != QtWidgets.QMessageBox.Yes: |
66 | 75 | return |
67 | 76 |
|
68 | 77 | # deleting data will take time for large databases |
69 | 78 | QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) |
70 | 79 |
|
71 | | - # delete upstream exchanges |
72 | | - ExchangeDataset.delete().where(ExchangeDataset.input_database == db_name).execute() |
| 80 | + for db_name in db_names: |
| 81 | + # delete upstream exchanges |
| 82 | + ExchangeDataset.delete().where(ExchangeDataset.input_database == db_name).execute() |
73 | 83 |
|
74 | | - # instruct the DatabaseController to delete the database from the project. |
75 | | - del bd.databases[db_name] |
| 84 | + # instruct the DatabaseController to delete the database from the project. |
| 85 | + del bd.databases[db_name] |
76 | 86 |
|
77 | | - # delete database parameters |
78 | | - Group.delete().where(Group.name == db_name).execute() |
| 87 | + # delete database parameters |
| 88 | + Group.delete().where(Group.name == db_name).execute() |
79 | 89 |
|
80 | | - # remove database from project settings |
81 | | - settings.project_settings.remove_db(db_name) |
| 90 | + # remove database from project settings |
| 91 | + settings.project_settings.remove_db(db_name) |
82 | 92 |
|
83 | 93 | QtWidgets.QApplication.restoreOverrideCursor() |
| 94 | + |
| 95 | + |
| 96 | +def build_title(db_names: List[str]) -> str: |
| 97 | + """Build an appropriate title for the confirmation dialog.""" |
| 98 | + if len(db_names) == 1: |
| 99 | + return "Delete database?" |
| 100 | + return "Delete databases?" |
0 commit comments